Skip to content

Instantly share code, notes, and snippets.

@thwarted
Created June 18, 2013 07:13
Show Gist options
  • Save thwarted/5803271 to your computer and use it in GitHub Desktop.
Save thwarted/5803271 to your computer and use it in GitHub Desktop.
Format some data as RSS that is currently only available as JSON
#!/usr/bin/php
<?php
date_default_timezone_set("GMT");
$type = "a type";
$key = "some identifier";
$json = "canonical url of this json stuff";
$entries = json_decode(file_get_contents("php://stdin"));
$len = sizeof($entries);
$rss = '';
$now = new DateTime();
if ($len) {
$rss = '<?xml version="1.0"?><rss version="2.0">';
$rss .= ' <channel><title>Site Name ' . $type . ': ' . $key . '</title>';
$rss .= ' <link>' . htmlentities ( $json ) . '</link>';
$rss .= ' <pubDate>' . $now->format(DATE_RSS ) . '</pubDate>';
for ($i=0; $i<$len; $i++) {
$sender = $entries[$i]->user->screen_name;
$payload = htmlentities ( $entries[$i]->text );
$created_at = new DateTime($entries[$i]->created_at);
$created_at = $created_at->format(DATE_RSS);
$rss .= "<item>\n<title>" . $sender . ": " . $payload . "</title>\n";
$rss .= " <author>" . $entries[$i]->user->name . " (@" . $sender . ")</author>\n";
$rss .= " <pubDate>" . $created_at . "</pubDate>\n";
$rss .= " <guid isPermaLink='false'>" . $entries[$i]->id_str . "</guid>\n";
$rss .= " <link>https://example.com/" . $sender . "/statuses/" . $entries[$i]->id_str . "</link>\n";
$rss .= " <description>" . $payload . "</description>\n";
$rss .= "</item>\n";
}
$rss .= "</channel></rss>";
}
print $rss;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment