如何在PHP中获取雅虎天气api json响应

时间:2015-07-10 09:40:50

标签: php

我正在使用yahoo weather API,我需要在我的网站上显示以下信息

当前温度=(以摄氏度为单位)(即[item] ['condition'] ['temp'])

多云(即[item] ['condition'] ['text'])

<?php
    $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="amaravathi")';
    $yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json";

    // Make call with cURL
    $session = curl_init($yql_query_url);
    curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
    $json = curl_exec($session);
    // Convert JSON to PHP object
    $phpObj =  json_decode($json);
    echo '<pre>';print_r($phpObj).'<pre>';



?>

这是Json Response

stdClass Object
(
    [query] => stdClass Object
        (
            [count] => 1
            [created] => 2015-07-10T09:34:14Z
            [lang] => en-US
            [results] => stdClass Object
                (
                    [channel] => stdClass Object
                        (
                            [title] => Yahoo! Weather - Amaravathi, IN
                            [link] => http://us.rd.yahoo.com/dailynews/rss/weather/Amaravathi__IN/*http://weather.yahoo.com/forecast/INXX0171_f.html
                            [description] => Yahoo! Weather for Amaravathi, IN
                            [language] => en-us
                            [lastBuildDate] => Fri, 10 Jul 2015 11:30 am IST
                            [ttl] => 60
                            [location] => stdClass Object
                                (
                                    [city] => Amaravathi
                                    [country] => India
                                    [region] => KA
                                )

                            [units] => stdClass Object
                                (
                                    [distance] => mi
                                    [pressure] => in
                                    [speed] => mph
                                    [temperature] => F
                                )

                            [wind] => stdClass Object
                                (
                                    [chill] => 81
                                    [direction] => 230
                                    [speed] => 7
                                )

                            [atmosphere] => stdClass Object
                                (
                                    [humidity] => 70
                                    [pressure] => 27.59
                                    [rising] => 0
                                    [visibility] => 6.21
                                )

                            [astronomy] => stdClass Object
                                (
                                    [sunrise] => 6:03 am
                                    [sunset] => 7:02 pm
                                )

                            [image] => stdClass Object
                                (
                                    [title] => Yahoo! Weather
                                    [width] => 142
                                    [height] => 18
                                    [link] => http://weather.yahoo.com
                                    [url] => http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif
                                )

                            [item] => stdClass Object
                                (
                                    [title] => Conditions for Amaravathi, IN at 11:30 am IST
                                    [lat] => 15.26
                                    [long] => 76.35
                                    [link] => http://us.rd.yahoo.com/dailynews/rss/weather/Amaravathi__IN/*http://weather.yahoo.com/forecast/INXX0171_f.html
                                    [pubDate] => Fri, 10 Jul 2015 11:30 am IST
                                    [condition] => stdClass Object
                                        (
                                            [code] => 28
                                            [date] => Fri, 10 Jul 2015 11:30 am IST
                                            [temp] => 81
                                            [text] => Mostly Cloudy
                                        )

                                    [description] => 


Current Conditions:

Mostly Cloudy, 81 F


Forecast:

Fri - Cloudy. High: 83 Low: 71

Sat - AM Showers. High: 85 Low: 70

Sun - Mostly Cloudy. High: 87 Low: 70

Mon - Mostly Cloudy. High: 87 Low: 70

Tue - AM Showers. High: 88 Low: 70



Full Forecast at Yahoo! Weather


(provided by The Weather Channel)


                                    [forecast] => Array
                                        (
                                            [0] => stdClass Object
                                                (
                                                    [code] => 26
                                                    [date] => 10 Jul 2015
                                                    [day] => Fri
                                                    [high] => 83
                                                    [low] => 71
                                                    [text] => Cloudy
                                                )

                                            [1] => stdClass Object
                                                (
                                                    [code] => 39
                                                    [date] => 11 Jul 2015
                                                    [day] => Sat
                                                    [high] => 85
                                                    [low] => 70
                                                    [text] => AM Showers
                                                )

                                            [2] => stdClass Object
                                                (
                                                    [code] => 28
                                                    [date] => 12 Jul 2015
                                                    [day] => Sun
                                                    [high] => 87
                                                    [low] => 70
                                                    [text] => Mostly Cloudy
                                                )

                                            [3] => stdClass Object
                                                (
                                                    [code] => 28
                                                    [date] => 13 Jul 2015
                                                    [day] => Mon
                                                    [high] => 87
                                                    [low] => 70
                                                    [text] => Mostly Cloudy
                                                )

                                            [4] => stdClass Object
                                                (
                                                    [code] => 39
                                                    [date] => 14 Jul 2015
                                                    [day] => Tue
                                                    [high] => 88
                                                    [low] => 70
                                                    [text] => AM Showers
                                                )

                                        )

                                    [guid] => stdClass Object
                                        (
                                            [isPermaLink] => false
                                            [content] => INXX0171_2015_07_14_7_00_IST
                                        )

                                )

                        )

                )

        )

)

2 个答案:

答案 0 :(得分:4)

到达想要回复的对象

echo $phpObj->query->results->channel->item->condition->text

答案 1 :(得分:2)

这将返回今天的平均温度和条件

$scope.$destroy()