Skip to content

Instantly share code, notes, and snippets.

@q1x
Created October 26, 2015 15:58
Show Gist options
  • Save q1x/0a78a3a158e37ab6895b to your computer and use it in GitHub Desktop.
Save q1x/0a78a3a158e37ab6895b to your computer and use it in GitHub Desktop.
<?php
// define the namespaces that we are interested in
$ns = array
(
'content' => 'http://purl.org/rss/1.0/modules/content/',
'wfw' => 'http://wellformedweb.org/CommentAPI/',
'dc' => 'http://purl.org/dc/elements/1.1/'
);
// obtain the articles in the feeds, and construct an array of articles
$articles = array();
// step 1: get the feed
if($_GET['url']) {
$blogs_urls = explode(",", $_GET['url']);
$blog_url = $blogs_urls[array_rand($blogs_urls)];
} else {
$blog_url = 'http://devopsreactions.tumblr.com/rss';
}
$rawFeed = file_get_contents($blog_url);
$xml = new SimpleXmlElement($rawFeed);
// step 2: extract the channel metadata
$channel = array();
$channel['title'] = $xml->channel->title;
$channel['link'] = $xml->channel->link;
$channel['description'] = $xml->channel->description;
$channel['pubDate'] = $xml->pubDate;
$channel['timestamp'] = strtotime($xml->pubDate);
$channel['generator'] = $xml->generator;
$channel['language'] = $xml->language;
// step 3: extract the articles
foreach ($xml->channel->item as $item)
{
$article = array();
// $article['channel'] = $blog;
$article['title'] = $item->title;
$article['link'] = $item->link;
$article['comments'] = $item->comments;
$article['pubDate'] = $item->pubDate;
$article['timestamp'] = strtotime($item->pubDate);
$article['description'] = (string) trim($item->description);
$article['isPermaLink'] = $item->guid['isPermaLink'];
// get data held in namespaces
$content = $item->children($ns['content']);
$dc = $item->children($ns['dc']);
$wfw = $item->children($ns['wfw']);
$article['creator'] = (string) $dc->creator;
foreach ($dc->subject as $subject)
$article['subject'][] = (string)$subject;
$article['content'] = (string)trim($content->encoded);
$article['commentRss'] = $wfw->commentRss;
// add this article to the list
$articles[$article['timestamp']] = $article;
}
// at this point, $channel contains all the metadata about the RSS feed,
// and $articles contains an array of articles for us to repurpose
echo "<HTML><BODY><CENTER><H3>";
echo $channel['title'][0];
echo "</H3>";
$randomkey = array_rand($articles);
echo $articles[$randomkey]['title'][0];
echo "<br>";
echo $articles[$randomkey]['description'];
echo "</CENTER></BODY></HTML>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment