Simexml_Load_FILE问题 [英] simplexml_load_file problems

查看:31
本文介绍了Simexml_Load_FILE问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在一个XML文件中读入以下代码:

$xml2 = simplexml_load_file('http://www.facebook.com/feeds/page.php?format=rss20&id=334704593230758'); 
$item = $xml2->channel->item;

我在源代码中返回了以下内容:

<b>Warning</b>:  simplexml_load_file() [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: http://www.facebook.com/feeds/page.php?format=rss20&amp;id=334704593230758:11: parser error : xmlParseEntityRef: no name in <b>/home/content/49/8644249/html/test/_inc/footer.php</b> on line <b>110</b><br />


它继续这样继续,又有10行。XML代码有问题吗?

RSS源

好的,这有点奇怪,因为这是一个推荐答案提要,并不是设计成可直接供人阅读的,因此解决此问题的方法是您必须在请求中包含User-Agent:头。

当我在Chrome中加载URL以获取有效的XML文档时,当我运行您的代码时,我收到了与您相同的错误。仔细检查后,我发现当我运行您的代码时,实际上得到的是一个最小的HTML文档,而不是所需的XML-为了获得正确的结果,您必须传递有效的用户代理字符串,这意味着您不能使用simplexml_load_file(),因为它不支持流上下文。

此代码适用于我:

// User-Agent string from Chrome. I haven't tested anything else so I don't know
// what is actually required, but this works.
$context = stream_context_create(array(
  'http'=>array(
    'user_agent' => 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11'
   )
));

// Get data as a string
$xml2 = file_get_contents('http://www.facebook.com/feeds/page.php?format=rss20&id=334704593230758', FALSE, $context);

// Convert string to a SimpleXML object
$xml2 = simplexml_load_string($xml2);

$item = $xml2->channel->item;

这篇关于Simexml_Load_FILE问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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