douyi1966 2012-09-04 15:22
浏览 34
已采纳

使用PHP的RSS解析什么都没有?

I am trying to parse a rss-feed with php but it gives me nothing. As you can see I am trying to echo both the $doc and the $itemRSS array and it gives me nothing and the "Success!" for when item 0 in the array is never reached.

I would be thrilled if someone could say "have you thought about [idiotic mistake]?", so please consider me a noob for this question. Thank you!

  $doc = new DOMDocument();
  $doc->load('http://pipes.yahoo.com/pipes/pipe.run?_id=566903fd393811762dc74aadc701badd&_render=rss');
  $arrFeeds = array();
  foreach ($doc->getElementsByTagName('item') as $node) {
    $itemRSS = array ( 
      'guid' => $node->getElementsByTagName('guid')->item(0)->nodeValue
      );
    array_push($arrFeeds, $itemRSS);
  }

    if ($itemRSS[0] != NULL) {

        echo 'Success!';

    }

echo $itemRSS;
echo $doc; 

And by this I mean that the page is completely blank. No error, nothing.

Update: Apprently my webhost has allow_url_fopen deactivated, so I have to find another way to do this. sigh

  • 写回答

3条回答 默认 最新

  • dongzhou1865 2012-09-04 16:14
    关注

    As mentioned in the other answers, you're doing a couple of things wrong, such as trying to echo arrays and objects. For some reason you're not getting any results in $arrFeeds either, although you should.

    A simpler way to do this is change the render method of the feed to JSON: http://pipes.yahoo.com/pipes/pipe.run?_id=566903fd393811762dc74aadc701badd&_render=json

    Then you can use json_decode() to get an array of all items:

    $contents = json_decode(file_get_contents('http://pipes.yahoo.com/pipes/pipe.run?_id=566903fd393811762dc74aadc701badd&_render=json'));
    foreach($contents['items'] as $item) {
      // use $item['title'], $item['description'] etc... 
    }
    

    Note you can only echo strings, ints, etc. not structured data such as arrays or objects.

    To make it easier, you can open that URL in your browser and analyze the JSON contents @ http://json.parser.online.fr/ - you'll see how your array is structured then.

    JSON is a much easier format to work with IMO.

    Edit:

    Since file_get_contents() is disabled you can use cURL (which should be installed on most servers, especially with allow_url_fopen disabled):

    function file_get_contents_curl($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
        curl_setopt($ch, CURLOPT_URL, $url);
        $data = curl_exec($ch);
        curl_close($ch);
    
        return $data;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器