douhu8851 2015-03-20 18:20
浏览 46

从Facebook页面获取手动RSS源 - 我错过了什么吗?

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>");
 }
  • 写回答

1条回答 默认 最新

  • duanchui1279 2015-03-20 19:00
    关注

    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();
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行