DOMDocument - Load xml rss - failed to open stream

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

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

回答1:

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;  } 


回答2:

Add this code before calling your feed, this will change user agent.

$opts = array(     'http' => array(         'user_agent' => 'PHP libxml agent',     ) );  $context = stream_context_create($opts); libxml_set_streams_context($context); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!