-2

I'm displaying a 3-day weather forcast by using an API key from wunderground.com. The mockup design includes: date, current condition (ie. overcast), and high and low temperatures for each day. I've figured out how to display current date and temperature, but I'm racking my brain trying to find the next two day's weather... can anyone help?

I did a var_dump($parsed_json) and I'm only pulling in today's weather (date, current temp, current condition, ect.)

<?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"; 
BryanH
  • 5,826
  • 3
  • 34
  • 47
J.T.
  • 1
  • 1
  • Please dont add code in a comment **its totally unreadable** I have edited it into your question, which is what you should have done – RiggsFolly May 02 '16 at 15:29
  • 1
    You should **never, ever, ever** post your API key (or password or token or ...) in a public forum. You have just given everyone and their brother access to your key. I **strongly** suggest you immediately generate a new key. – BryanH May 02 '16 at 18:10

1 Answers1

0

Referring to the Wunderground API documentation you have to use :

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

in your file_get_contents().

This will return a response which has a forecast object within which you have the week's forecast as text (txt_forecast object) and a data only version (simpleforecast object):

{  
   "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
            },
            {
             ...                
            }
         ]
      }
   }
}
Omegadela
  • 13
  • 5