无法从yahoo weather api中提取描述标记

时间:2017-02-27 19:30:45

标签: php xml api

我尝试使用来自我的yahoo weather api的cdata提取描述时遇到了问题,我认为问题可能是找到确切的路径。

<?php
	$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";
	$error = "";
	$file = fopen($url, "r");
	
	if($file == false)
	{
		$error = "File open error";
	}
	else
	{
		$stringXML = "";
		while (feof($file)==false)
		{
			$stringXML.= fread($file, 8192);
		}
		$xml = new SimpleXMLElement($stringXML);
		fclose($file);

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

	}
?>


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

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

?>	
	</body>
</html>

有人介意帮助我吗? :)

1 个答案:

答案 0 :(得分:2)

说明不在链接中。使用:

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

要在item标记内获取CDATA的描述,请使用:

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