-2

我正在使用来自 wunderground.com 的 API 密钥显示 3 天的天气预报。样机设计包括:日期、当前状况(即阴天)以及每天的高温和低温。我已经想出了如何显示当前日期和温度,但我绞尽脑汁想找到接下来两天的天气……有人能帮忙吗?

我做了一个var_dump($parsed_json),我只是在提取今天的天气(日期、当前温度、当前状况等)

<?php 
$json_string = file_get_contents("api.wunderground.com/api/[key]/conditions/q/TN/…); 
$parsed_json = json_decode($json_string); 
$date = $parsed_json
                    ->{'current_observation'}
                    ->{'observation_time_rfc822'}; 
$temp_f = $parsed_json->{'current_observation'}->{'temp_f'}; 
$feels_like = $parsed_json->{'current_observation'}->{'feelslike_f'}; 
$weather = $parsed_json->{'current_observation'}->{'weather'}; 
echo "${date}\n"; 
echo "${temp_f}\n"; 
echo "Feels like... ${feels_like}\n"; 
echo "${weather}\n"; 
4

1 回答 1

0

参考您必须使用的Wunderground API 文档:

http://api.wunderground.com/api/Your_Key/forecast/q/TN/XXX.json

在你的file_get_contents().

这将返回一个响应,其中包含一个forecast对象,在该对象中,您将一周的预测作为文本(txt_forecast对象)和仅数据版本(simpleforecast对象):

{  
   "response":{  
      ...
   },
   "forecast":{  
      "txt_forecast":{  
         "date":"2:00 PM PDT",
         "forecastday":[  
            {  
               "period":0,
               "icon":"partlycloudy",
               "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
               "title":"Tuesday",
               "fcttext":"Partly cloudy in the morning, then clear. High of 68F. Breezy. Winds from the West at 10 to 25 mph.",
               "fcttext_metric":"Partly cloudy in the morning, then clear. High of 20C. Windy. Winds from the West at 20 to 35 km/h.",
               "pop":"0"
            },
            {  
               "period":1,
               "icon":"partlycloudy",
               "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
               "title":"Tuesday Night",
               "fcttext":"Mostly cloudy. Fog overnight. Low of 50F. Winds from the WSW at 5 to 15 mph.",
               "fcttext_metric":"Mostly cloudy. Fog overnight. Low of 10C. Breezy. Winds from the WSW at 10 to 20 km/h.",
               "pop":"0"
            },
            {
             ...
            }
         ]
      },
      "simpleforecast":{  
         "forecastday":[  
            {  
               "date":{  
                  "epoch":"1340776800",
                  "pretty":"11:00 PM PDT on June 26, 2012",
                  "day":26,
                  "month":6,
                  "year":2012,
                  "yday":177,
                  "hour":23,
                  "min":"00",
                  "sec":0,
                  "isdst":"1",
                  "monthname":"June",
                  "weekday_short":"Tue",
                  "weekday":"Tuesday",
                  "ampm":"PM",
                  "tz_short":"PDT",
                  "tz_long":"America/Los_Angeles"
               },
               "period":1,
               "high":{  
                  "fahrenheit":"68",
                  "celsius":"20"
               },
               "low":{  
                  "fahrenheit":"50",
                  "celsius":"10"
               },
               "conditions":"Partly Cloudy",
               "icon":"partlycloudy",
               "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
               "skyicon":"mostlysunny",
               "pop":0,
               "qpf_allday":{  
                  "in":0.00,
                  "mm":0.0
               },
               "qpf_day":{  
                  "in":0.00,
                  "mm":0.0
               },
               "qpf_night":{  
                  "in":0.00,
                  "mm":0.0
               },
               "snow_allday":{  
                  "in":0,
                  "cm":0
               },
               "snow_day":{  
                  "in":0,
                  "cm":0
               },
               "snow_night":{  
                  "in":0,
                  "cm":0
               },
               "maxwind":{  
                  "mph":21,
                  "kph":34,
                  "dir":"West",
                  "degrees":272
               },
               "avewind":{  
                  "mph":17,
                  "kph":27,
                  "dir":"West",
                  "degrees":272
               },
               "avehumidity":72,
               "maxhumidity":94,
               "minhumidity":58
            },
            {
             ...                
            }
         ]
      }
   }
}
于 2016-05-02T15:45:34.960 回答