I am trying to take an RSS feed from a client's Facebook page, rework the contents (posts), edit and present the resulting data on their website. I don't want to use a widget from a 3rd party and neither do I want to use the Facebook API's as they do not give me access to the raw data.
I have taken feeds before and the code I have provided works perfectly well with NBC, Google etc. but I get nothing from Facebook. The URL I am using works fine in the browser but not in my code and I am hoping that someone here can point to my no doubt obvious error.
$xml=("<http://www.facebook.com/feeds/page.php?id=xxxxxxxxxxxxxx&format=rss20>")
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
//get elements from "<channel>"
$channel=$xmlDoc->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$channel_link = $channel->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;
//output elements from "<channel>"
echo("<p><a href='" . $channel_link
. "'>" . $channel_title . "</a>");
echo("<br>");
echo($channel_desc . "</p>");
//get and output "<item>" elements
$x=$xmlDoc->getElementsByTagName('item');
for ($i=0; $i<=2; $i++) {
$item_title=$x->item($i)->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$item_link=$x->item($i)->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$item_desc=$x->item($i)->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;
echo ("<p><a href='" . $item_link
. "'>" . $item_title . "</a>");
echo ("<br>");
echo ($item_desc . "</p>");
}
You could use simplexml_load_file
if you are just doing something pretty simple, there are a few differences between this and DOM, which you can read more about here.
Here is a code example using simplexml_load_file
:
<?php
/**
* Facebook Page Feed Parser
*/
function fb_parse_feed( $page_id, $no = 5 ) {
// You need to query the feed as a browser.
ini_set('user_agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');
// Facebook Page id
$page_id = 'xxxxxxxxxxxxxxx';
// URL to the Facebook page's RSS feed.
$rss_url = 'http://www.facebook.com/feeds/page.php?id=' . $page_id . '&format=rss20';
$xml = simplexml_load_file( $rss_url );
$out = '';
$i = 1;
foreach( $xml->channel->item as $item ){
$out .= '<div class="entry">';
$out .= '<h3 class="title"><a href="' . $item->link . '">' . $item->title . '</a></h3>';
$out .= '<div class="meta">' . $item->pubDate . ' by '. $item->author .'</div>';
$out .= '<div class="content">' . $item->description . '</div></div>';
if( $i == $no ) break;
$i++;
}
echo $out;
}
// This will print the above
fb_parse_feed();
?>