// create a new cURL resource
$ch = curl_init();
$url = “http://witslog.com/wiki/?feed=rss2″;
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url); // you have to set the url of the website of which you want to get the rss data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
$data = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
//Now work with SimpleXmlElement. This is core php xml class. The SimpleXML extension provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators.
$xml = new SimpleXmlElement($data, LIBXML_NOCDATA);
//check either it is RSS feeder or ATOM Feeder. Rss feeder have channel node while in Atom feeder you see entry node. Generally each feeder have their defined channel.
if(isset($xml->channel))
{
parseRSS($xml);
}
if(isset($xml->entry))
{
parseAtom($xml);
}
//This is for rss Feeder
function parseRSS($xml)
{
echo “<strong>”.$xml->channel->title.”</strong>”;
$cnt = count($xml->channel->item);
for($i=0; $i<$cnt; $i++)
{
$url = $xml->channel->item[$i]->link;
$title = $xml->channel->item[$i]->title;
$desc = $xml->channel->item[$i]->description;
echo ‘<a href=”‘.$url.’”>’.$title.’</a>’.$desc.”;
}
}
//This is for XML Feeder
function parseAtom($xml)
{
echo “<strong>”.$xml->author->name.”</strong>”;
$cnt = count($xml->entry);
for($i=0; $i<$cnt; $i++)
{
$urlAtt = $xml->entry->link[$i]->attributes();
$url = $urlAtt['href'];
$title = $xml->entry->title;
$desc = strip_tags($xml->entry->content);
echo ‘<a href=”‘.$url.’”>’.$title.’</a>’.$desc.”;
}
}