dongyimo1293 2015-04-27 12:41
浏览 34
已采纳

DOMDocument - 加载xml rss - 无法打开流

I want to get links from a rss url . This is my code :

$doc = new DOMDocument();
$doc->load("http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x.y.xml");
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {

    $title = $node->getElementsByTagName('title')->item(0)->nodeValue;
    $title=strip_tags($title);
    $link=$node->getElementsByTagName('link')->item(0)->nodeValue;
}

I've used this code for several others URLs and all of them worked but on this one I get:

Warning:
DOMDocument::load(http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x.y.xml): failed to open stream: HTTP request failed!
HTTP/1.1 403 Forbidden in /home/xxxxxxx/domains/xxxxxxx/public_html/data.php on line 14
Warning:
DOMDocument::load(): I/O warning: failed to load external entity "http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x.y.xml"
in /home/xxxxxxx/domains/xxxxxxx/public_html/data.php on line 14
http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x.y.xml

Line 14 is:

$doc->load("http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x.y.xml");

Could you help me? Why does this request give me an error?

Thanks

  • 写回答

2条回答 默认 最新

  • drwxg62286 2015-04-27 12:50
    关注

    Using the code above failed for me and it was not due to the comma as I commented. I found that, using curl, I was able to retrieve the xml file.

    $c=curl_init('http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x.y.xml');
    curl_setopt( $c, CURLOPT_USERAGENT,'nginx-curl-blahblahblah' );
    curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
    $r=curl_exec( $c );
    curl_close( $c );
    
    $doc = new DOMDocument();
    $doc->loadxml($r);
    $arrFeeds = array();
    
    foreach ($doc->getElementsByTagName('item') as $node) {
    
        $title=$node->getElementsByTagName('title')->item(0)->nodeValue;
        $title=strip_tags($title);
        $link=$node->getElementsByTagName('link')->item(0)->nodeValue;
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程