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

当前温度=(摄氏度)(即[项目] ['条件'] ['温度'])

大部分多云(即[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>';



?>


这是杰森的回应

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
                                        )

                                )

                        )

                )

        )

)

最佳答案

到达要回显的对象

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

关于php - 如何在PHP中获取Yahoo Weather API json响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31337535/

10-16 22:06