php
2015-07-10 41 views 2 likes 
2

我使用雅虎天氣API來獲取在PHP雅虎天氣API JSON響應,我需要在我的網站怎麼

當前溫度=(攝氏溫度)(即[項目] [「條件」,顯示如下信息] [ '溫度'])

多雲(即[項目] [ '狀態'] [ '文本'])

<?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響應

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 
             ) 

           ) 

         ) 

       ) 

     ) 

) 

回答

5

達到你想呼應

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

2

這將返回今天的everage臨時對象和條件

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); 
+0

我需要在攝氏 –

+1

現在你得到了當前溫度是:) – Vanojx1

+1

ü也可以使用這個來獲得當前的條件$ phpObj-> query-> results-> channel-> item-> condition-> text/$ phpObj-> query-> results-> channel-> item-> condition-> temp獲取當前溫度....我的解決方案使用日平均條件 – Vanojx1

相關問題