Skip to content

Instantly share code, notes, and snippets.

@pumamammal
Forked from betweenbrain/gist:5405671
Last active June 22, 2016 22:59
Show Gist options
  • Save pumamammal/63da4e90bf31e615c2c07aba66819e40 to your computer and use it in GitHub Desktop.
Save pumamammal/63da4e90bf31e615c2c07aba66819e40 to your computer and use it in GitHub Desktop.
Use cURL and SimpleXML to retrieve and parse Wordpress RSS feed
<?php
$site = array('www.nbcnewyork.com/rss', 'http://www.nairaland.com/feed', 'www.nbc12.com/category/213869/rss-feeds');
foreach ($site as $i ) {
$curl = curl_init();
curl_setopt_array($curl, Array(
CURLOPT_URL => $i,
CURLOPT_USERAGENT => 'spider',
CURLOPT_TIMEOUT => 120,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_ENCODING => 'UTF-8'
));
$data = curl_exec($curl);
curl_close($curl);
$xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
//die('<pre>' . print_r($xml], TRUE) . '</pre>');
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<?php
$c_t = explode('/', $i);
print_r('<h2>' . $c_t[2] . '</h2>');
foreach ($xml->channel->item as $item) {
$creator = $item->children('dc', TRUE);
echo '<h2>' . $item->title . '</h2>';
echo '<p>Created: ' . $item->pubDate . '</p>';
echo '<p>Author: ' . $creator . '</p>';
echo '<p>' . $item->description . '</p>';
echo '<p><a href="' . $item->link . '">Read more: ' . $item->title . '</a></p>';
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment