Php DOMDocument-加载xml rss-无法打开流
我想从rss url获取链接。这是我的代码:Php DOMDocument-加载xml rss-无法打开流,php,xml,domdocument,Php,Xml,Domdocument,我想从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('t
$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都有效,但在这一个URL上我得到:
警告:DOMDocument::load():无法打开流:HTTP请求失败
HTTP/1.1 403第14行的/home/xxxxxxx/domains/xxxxxxx/public_html/data.php中禁止使用
警告:
DOMDocument::load():I/O警告:无法加载外部实体“”
在第14行的/home/xxxxxxx/domains/xxxxxxx/public_html/data.php中
第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);
URL中有一个逗号-也许这是抛出错误的原因?非常感谢您添加此答案。我在试图用一点第三方代码来解决这个问题时失去了理智,这就是它!