Skip to content

Instantly share code, notes, and snippets.

@marckohlbrugge
Last active November 5, 2022 08:27
Show Gist options
  • Save marckohlbrugge/15e6a54e2cffd31e743100e9506e9b24 to your computer and use it in GitHub Desktop.
Save marckohlbrugge/15e6a54e2cffd31e743100e9506e9b24 to your computer and use it in GitHub Desktop.
Use WIP's GraphQL API to create an RSS feed of your completed todos
<?php
$username = isset($_GET['username']) ? $_GET['username'] : 'marcano';
function graphql_query($endpoint, $query, $variables = [], $token = null) {
$headers = ['Content-Type: application/json', 'User-Agent: Minimal GraphQL client'];
if (null !== $token) {
$headers[] = "Authorization: bearer $token";
}
if (false === $data = @file_get_contents($endpoint, false, stream_context_create([
'http' => [
'method' => 'POST',
'header' => $headers,
'content' => json_encode(['query' => $query, 'variables' => $variables]),
]
]))) {
$error = error_get_last();
throw new \ErrorException($error['message'], $error['type']);
}
return json_decode($data, true);
}
$query = <<<'GRAPHQL'
query GetUser($user: String!) {
user (username: $user) {
todos(limit: 25, orderBy: {createdAt: desc}, completed:true) {
id
url
body
completed_at
attachments {
url
}
}
}
}
GRAPHQL;
$data = graphql_query('https://wip.co/graphql', $query, ['user' => $username]);
$todos = $data['data']['user']['todos'];
?>
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>WIP Todos</title>
<link>https://wip.co/@<?php echo $username ?></link>
<description>WIP todos</description>
<?php foreach($todos as $todo) { ?>
<item>
<guid><?php echo $todo['url']; ?></guid>
<pubDate><?php echo $todo['completed_at']; ?></pubDate>
<title><?php echo $todo['body']; ?></title>
<link><?php echo $todo['url']; ?></link>
<description>
<p><?php echo $todo['body']; ?></p>
<?php foreach($todo["attachments"] as $attachment) { ?>
<img src="<?php echo $attachment['url'] ?>" style="max-width: 500px; max-height: 500px;" />
<?php } ?>
</description>
</item>
<?php } ?>
</channel>
</rss>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment