Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用yahoo weather查询api时未定义名称空间前缀_Php_Api_Yahoo_Weather - Fatal编程技术网

Php 使用yahoo weather查询api时未定义名称空间前缀

Php 使用yahoo weather查询api时未定义名称空间前缀,php,api,yahoo,weather,Php,Api,Yahoo,Weather,使用下面的代码时,不断获取命名空间错误 <?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")'; $re

使用下面的代码时,不断获取命名空间错误

<?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>';
    }
?>

根据要求,这里有一个使用JSON而不是XML的解决方案:

$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 = json_decode(file_get_contents($BASE_URL . "?q=" . urlencode($yql_query) . "&format=json"), true);

if(is_array($result)) {
    $location = $result['query']['results']['channel'];

    if(!empty($location)) {
        $temp = $result['query']['results']['channel']['item']['condition']['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解码结果并使用简单的PHP数组!这是因为您可能使用了错误的名称空间。在文件顶部定义您正在使用的名称空间,或包含此脚本运行名称空间所需的文件。无法粘贴xml。。使用url查看xml@jszobody,您是否介意显示和示例?