2011-02-24 81 views
0

我這樣做錯誤/警告解析RSS XML :(

<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 

And I co üld不解析任何東西:

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

有沒有人知道如何解決這個問題或解決它?

謝謝, 亞歷克斯

回答

6

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); 
+0

那固定的錯誤和警告。謝謝。但是我怎麼仍然沒有從echo $ yahoo_response-> rss-> channel-> item-> title返回任何東西; – Genadinik 2011-02-24 08:04:01

+0

我不確定(有一段時間沒有用過SimpleXML),但我相信'$ yahoo_response'指向''元素。嘗試打印'$ yahoo_response-> channel-> item-> title'。你也應該看看手冊(http://lv.php.net/SimpleXMLElement)。 – binaryLV 2011-02-24 08:12:34