DOMDocument-加载XML RSS-无法打开流 [英] DOMDocument - Load xml rss - failed to open stream

查看:54
本文介绍了DOMDocument-加载XML RSS-无法打开流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从rss url获得链接。这是我的代码:

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

我已经将此代码用于其他多个URL,但所有这些URL均有效,但在此我得到:

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


警告:

DOMDocument :: load(< a href = http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x.y.xml rel = nofollow> http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x。 y.xml ):无法打开流:HTTP请求失败!

HTTP / 1.1 403在第14行的/home/xxxxxxx/domains/xxxxxxx/public_html/data.php中被禁止

警告:

DOMDocument :: load():I / O警告:无法加载外部实体 http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x.y.xml
第14行/home/xxxxxxx/domains/xxxxxxx/public_html/data.php中的


http://www.alef.ir/rssdx.gmyefy,ggeltshmci.62ay2x.y.xml

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

第14行是:

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

你能帮我吗?为什么这个请求给我一个错误?

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

谢谢

推荐答案

使用上面的代码对我来说失败了,这不是我评论的逗号引起的。我发现使用curl可以检索xml文件。

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;

}

这篇关于DOMDocument-加载XML RSS-无法打开流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆