【问题标题】:Warning: DOMDocument::load(): Extra content at the end of the document in http://widget.stagram.com/rss/n警告:DOMDocument::load():http://widget.stagram.com/rss/n 中文档末尾的额外内容
【发布时间】:2014-09-07 08:48:14
【问题描述】:
【问题讨论】:
标签:
php
xml
wordpress
rss
instagram
【解决方案1】:
您需要使用 curl 并添加选项CURLOPT_USERAGENT。这就是它在浏览器上运行的原因,而不是简单的file_get_contents 或->load。考虑这个例子:
$url = ('http://widget.stagram.com/rss/tag/zee/');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$data = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
echo '<pre>';
print_r($xml);
Sample Output
【解决方案2】:
我遇到了同样的问题,发现以下解决方案不需要 CURL:
libxml_set_streams_context(
stream_context_create(
array(
'http' => array(
'user_agent' => 'php'
)
)
)
);
$dom = new DOMDocument;
$dom->load($xml);
多余的内容错误消失了,我推送的所有提要都运行良好。
非常感谢 Gordon 用这个答案回答了另一个问题,这让我尝试解决这个问题。