【问题标题】:Error/Warning parsing RSS XML :(解析 RSS XML 的错误/警告 :(
【发布时间】:2011-02-24 07:53:45
【问题描述】:

我做到了

<blink>
$xml = file_get_contents(http://weather.yahooapis.com/forecastrss?w=12797541);
$yahoo_response = new SimpleXMLElement($xml , 0, true);
</blink>

我收到了这样的 XML 解析警告:

PHP Warning:  SimpleXMLElement::__construct()
[<a href='simplexmlelement.--construct'>simplexmlelement.--construct</a>]:
I/O warning : failed to load external entity &quot;&lt;?xml version=&quot;1.0&quot; 

.....

消息的重要部分是这样的:

I/O warning : failed to load external entity 

我无法用这一行解析任何内容:

echo (string) $yahoo_response->rss->channel->item->title;

有谁知道如何解决或绕过它?

谢谢, 亚历克斯

【问题讨论】:

    标签: xml rss simplexml php


    【解决方案1】:

    SimpleXMLElement() 的第三个参数指定$data 是否为 URL。你应该这样做

    $xml = file_get_contents('http://weather.yahooapis.com/forecastrss?w=12797541');
    $yahoo_response = new SimpleXMLElement($xml , 0, false); // false, not true
    

    $xml = 'http://weather.yahooapis.com/forecastrss?w=12797541'; // url, not contents
    $yahoo_response = new SimpleXMLElement($xml , 0, true);
    

    【讨论】:

    • 修复了错误和警告。谢谢你。但是为什么我仍然没有从 echo $yahoo_response->rss->channel->item->title; 得到任何返回;
    • 我不确定(有一段时间没有使用 SimpleXML),但我相信 $yahoo_response 指向 &lt;rss/&gt; 元素。尝试打印$yahoo_response-&gt;channel-&gt;item-&gt;title。您还应该看看手册 (lv.php.net/SimpleXMLElement)。
    猜你喜欢
    • 2012-05-03
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    相关资源
    最近更新 更多