DOMDocument - 加载 xml rss - 无法打开流
DOMDocument - Load xml rss - failed to open stream
我想从 rss url 获取链接。这是我的代码:
$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 上我得到:
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");
你能帮帮我吗?为什么这个请求给我一个错误?
谢谢
使用上面的代码对我来说失败了,这不是因为我评论的逗号。我发现,使用 curl,我能够检索 xml 文件。
$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;
}
在调用您的供稿之前添加此代码,这将更改用户代理。
$opts = array(
'http' => array(
'user_agent' => 'PHP libxml agent',
)
);
$context = stream_context_create($opts);
libxml_set_streams_context($context);
我想从 rss url 获取链接。这是我的代码:
$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 上我得到:
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");
你能帮帮我吗?为什么这个请求给我一个错误?
谢谢
使用上面的代码对我来说失败了,这不是因为我评论的逗号。我发现,使用 curl,我能够检索 xml 文件。
$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;
}
在调用您的供稿之前添加此代码,这将更改用户代理。
$opts = array(
'http' => array(
'user_agent' => 'PHP libxml agent',
)
);
$context = stream_context_create($opts);
libxml_set_streams_context($context);