【问题标题】:Retrieve YQL with PHP and json使用 PHP 和 json 检索 YQL
【发布时间】:2013-07-15 12:37:58
【问题描述】:

我想得到这个 YQL 查询的天气预报的代码元素,问题是这个返回我 null。

                        php
          var_dump(getResultFromYQL());

        function getResultFromYQL(  ) {

           $yql_query_url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D946738&format=json&diagnostics=true&callback=?";

            $session = curl_init($yql_query_url);
            curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

            $json = curl_exec($session);
            curl_close($session);

            return json_decode($json);
        }
        ?>

好的,我找到了解决方案 php

            $Jakarta = 946738;  /* Jakarta */


            /* Use cURL to query the API for some XML */
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, 'http://weather.yahooapis.com/forecastrss?w='.$Jakarta.'&u=f');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $weather_rss = curl_exec($ch);
            curl_close($ch);

            /* Create an object of the XML returned */
            $weather = new SimpleXMLElement($weather_rss);

            /*
            * Since I don't want to figure out an image system, I'll use Weather.com's (what Yahoo does)
            * by pulling the image directly out of the returned API request. This could be done better.
            */
            $weather_contents = $weather->channel->item->description;
            //  preg_match_all('/<img[^>]+>/i',$weather_contents, $img);
            //  $weather_img = $img[0][0];

            /* Get clean parts */

            $weather_cond = $weather->channel->item->xpath('yweather:condition');


            /* Function to convert Wind Direction from given degree units into Cardinal units */

            ?>


                             hp

                                         print $weather_cond[0]->attributes()->code;

                              ?>
                  </div>
                </div>
            </div>
            </div>

【问题讨论】:

    标签: php json yql


    【解决方案1】:

    删除callback=? 查询部分。它适用于 JavaScript 回调函数。

    工作网址应该是:$yql_query_url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D946738&amp;format=json&amp;diagnostics=true";

    更新

    工作代码:(在 PHP 5.3.27 和 5.4.17 上测试)

    <?php
    function getResultFromYQL() {
        $yql_query_url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%3D946738&format=json&diagnostics=true";
    
        $session = curl_init($yql_query_url);
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    
        $json = curl_exec($session);
        curl_close($session);
        return json_decode($json);
    }
    
    var_dump(getResultFromYQL());
    ?>
    

    【讨论】:

    • 即使使用 'callback=?' 我也再次变为 null已删除...有问题...
    • 不幸的是不工作我得到 NULL ,你得到什么?我什至复制粘贴你的代码...我在 php 5+ 服务器上测试过
    • OK 它的工作,但这不是我想要的 :(( ,,,基本上我需要一个带有当前天气代码的纯文本......
    • 删除diagnostics=true查询部分并从结果中得到你想要的。它将始终显示这些详细信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 2015-10-13
    • 1970-01-01
    相关资源
    最近更新 更多