【发布时间】:2015-03-12 13:06:28
【问题描述】:
使用以下代码时不断出现命名空间错误
<?php
// error_reporting(0);
$BASE_URL = "http://query.yahooapis.com/v1/public/yql";
$yql_query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="austin, tx")';
$result = file_get_contents($BASE_URL . "?q=" . urlencode($yql_query) . "&format=xml");
if ($result == true) {
$xml = simplexml_load_string($result);
$xml->registerXPathNamespace('yweather', 'http://xml.weather.yahoo.com/ns/rss/1.0');
$location = $xml->results->channel;
if(!empty($location)){
foreach($xml->results->channel->item as $item){
$current = $item->xpath('yweather:condition');
$temp = $current['temp'];
echo $temp;
}
}
else{
echo '<h1>No weather for today</h1>';
}
}else{
echo '<p>Weather service is down</p>';
}
?>
【问题讨论】:
-
很高兴知道 xml 的样子。
-
旁注:将
&format=xml更改为&format=json,然后只更改json_decode结果并使用简单的PHP 数组! -
那是因为你可能在错误的命名空间中。在文件的最顶部定义您正在使用的命名空间,或包含此脚本需要运行的文件(包括名称空间)。
-
无法粘贴xml..使用url查看xml query.yahooapis.com/v1/public/…
-
@jszobody 你介意展示和举例吗?