2017-02-27 87 views
0

當我嘗試從我的雅虎天氣api提取描述時,我遇到了問題,我認爲問題可能是找到確切的路徑。無法從雅虎天氣api提取描述標籤

<?php 
 
\t $url ="https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(55844479)%20where%20text%3D%22Riga%2C%20Lv%22)&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"; 
 
\t $error = ""; 
 
\t $file = fopen($url, "r"); 
 
\t 
 
\t if($file == false) 
 
\t { 
 
\t \t $error = "File open error"; 
 
\t } 
 
\t else 
 
\t { 
 
\t \t $stringXML = ""; 
 
\t \t while (feof($file)==false) 
 
\t \t { 
 
\t \t \t $stringXML.= fread($file, 8192); 
 
\t \t } 
 
\t \t $xml = new SimpleXMLElement($stringXML); 
 
\t \t fclose($file); 
 

 
\t \t $description = $xml->results->link->description; 
 

 
\t } 
 
?> 
 

 

 
<html> 
 
\t <head> 
 
\t \t <title>Yahoo weather API</title> 
 
\t </head> 
 
\t <body> 
 
\t 
 
\t \t <h1>Yahoo weather api</h1> 
 
<?php 
 

 
\t if($error == "") 
 
\t { 
 
\t \t echo "<h3>Description:".$description."</h3>"; 
 
\t } 
 
\t else 
 
\t { 
 
\t echo "<p>".$error."</p>"; 
 
\t } 
 

 
?> \t 
 
\t </body> 
 
</html>

任何人的頭腦幫助我? :)

回答

2

該描述不在鏈接中。用途:

$description = $xml->results->channel->description; 

要使用CDATA獲得描述裏面的物品標籤,使用:

$description = $xml->results->channel->item->description; 
+0

謝謝!然而,我需要第二個描述標籤,其中包含cdata :) –

+1

它在*項目*,所以它會是$ description = $ xml-> results-> channel-> item-> description;查看編輯@RalfsR – eeetee

+0

非常感謝,現在工作很好! –