1

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>");
 }
Michael Doye
  • 8,063
  • 5
  • 40
  • 56
  • 1
    So what is not working? – WizKid Mar 20 '15 at 18:29
  • Be sure that doing this is not a violation of Facebook's Terms of Service. They may be preventing this method for that very reason. – Mike Lyons Mar 20 '15 at 18:50
  • Have you at least tried some very basic debugging steps to see where the error occurs? Do you have PHP’s `error_reporting` set to a sensible development value? – CBroe Mar 20 '15 at 21:13
  • Thanks for the comments guys ... WizKid -> I get no content from the feed, there is no data. CBroe -> There are no errors, the code works perfectly well on other sources - I just get a blank from FB. Mike Lyons -> You may be right but why do 3rd party widgets work? M.Doye -> Thanks for the code but it is no better. – Richard Harris Mar 23 '15 at 10:21

1 Answers1

0

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();
?>
Community
  • 1
  • 1
Michael Doye
  • 8,063
  • 5
  • 40
  • 56