简体   繁体   中英

how to fetch yahoo weather api json response in php

I am using yahoo weather API , I need to display the following info in my website

Current Temperature = (in Celsius)(ie [item] ['condition'] ['temp'])

Mostly Cloudy (ie [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>';



?>

This is the 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
                                        )

                                )

                        )

                )

        )

)

到达要回显的对象

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

this will return the everage temp of today and the conditions

function getCurrentTemp($obj){
    return (($obj->query->results->channel->item->forecast[0]->high)+($obj->query->results->channel->item->forecast[0]->low))/2;
}

function getCurrentCondition($obj){
    return $obj->query->results->channel->item->forecast[0]->text;
}

function fahrenheit_to_celsius($given_value)
{
    $celsius=5/9*($given_value-32);
    return $celsius ;
}

echo fahrenheit_to_celsius(getCurrentTemp($phpObj))." ".getCurrentCondition($phpObj);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM