Page 1 of 1

Using the new WU API

Posted: Tue 26 Mar 2019 7:25 pm
by PaulMy
I have been using the old WU API to give me a current conditions on my webpage using my WU station's https://api.wunderground.com/weathersta ... format=XML. This included my station's data as well as some airport data such as visibility.
I had found some code on the forum and was able to incorporate in my page http://www.komokaweather.com/weather but today the data is not being shown.
Today it looks like the old API has come to an end for me.

I have been modifying the script for using my new WU API but have not been successful. I am obviously doing something wrong or missing something as the data from the json output is not showing.

My previous working script was

Code: Select all

<?php
$json_string = file_get_contents("http://api.wunderground.com/api/OLD API KEY/conditions/q/pws:IONTARIO226.json");
$parsed_json = json_decode($json_string);
$observation_time = $parsed_json->{'current_observation'}->{'observation_time'};
$weather = $parsed_json->{'current_observation'}->{'weather'};
$temp_c = $parsed_json->{'current_observation'}->{'temp_c'};
$wind_kph = $parsed_json->{'current_observation'}->{'wind_kph'};
$wind_dir = $parsed_json->{'current_observation'}->{'wind_dir'};
$wind_gust_kph = $parsed_json->{'current_observation'}->{'wind_gust_kph'};
$feelslike_c = $parsed_json->{'current_observation'}->{'feelslike_c'};
$heat_index_c = $parsed_json->{'current_observation'}->{'heat_index_c'};
$dewpoint_c = $parsed_json->{'current_observation'}->{'dewpoint_c'};
$windchill_c = $parsed_json->{'current_observation'}->{'windchill_c'};
$pressure_mb = $parsed_json->{'current_observation'}->{'pressure_mb'};
$pressure_trend = $parsed_json->{'current_observation'}->{'pressure_trend'};
$visibility_km = $parsed_json->{'current_observation'}->{'visibility_km'};
$solarradiation = $parsed_json->{'current_observation'}->{'solarradiation'};
$UV = $parsed_json->{'current_observation'}->{'UV'};
$precip_1hr_metric = $parsed_json->{'current_observation'}->{'precip_1hr_metric'};
$precip_today_metric = $parsed_json->{'current_observation'}->{'precip_today_metric'};
echo "The weather in Komoka, ${observation_time}:<br />
${weather}
${icon_url}
<br /> 
Temperature is ${temp_c} C° and feels like ${feelslike_c} C°.<br />
Heat index is ${heat_index_c} C° and the windchill ${windchill_c} C°.  The dewpoint is at ${dewpoint_c} C°.<br />
Wind is from the ${wind_dir} at ${wind_kph} with gusts of ${wind_gust_kph} kph.<br />
The barometric pressure is ${pressure_mb} MB and ${pressure_trend}. There  has been ${precip_1hr_metric} mm rain in the last hour, and ${precip_today_metric} mm today so far.<br />
Visibility is ${visibility_km} km. UV index is ${UV} and solar radiation ${solarradiation} W/m<sup>2</sup>.
\n";
?>
and this provided
The weather in Komoka, Last Updated on March 24, 9:40 AM EDT:
Mostly Cloudy
Temperature is 4 C° and feels like 2 C°.
Heat index is NA C° and the windchill 2 C°. The dewpoint is at -4 C°.
Wind is from the North at 8 with gusts of 12.9 kph.
The barometric pressure is 1022 MB and 0. There has been 0 mm rain in the last hour, and 0 mm today so far.
Visibility is 24.1 km. UV index is 0.7 and solar radiation 199 W/m2.

I don't have a sample of the previous json file but the current json output is

observations
0
stationID "IONTARIO226"
obsTimeUtc "2019-03-26T17:44:46Z"
obsTimeLocal "2019-03-26 13:44:46"
neighborhood "Komoka Village"
softwareType "Cumulus v1.9.4"
country "CA"
solarRadiation 816
lon -81.43488312
realtimeFrequency null
epoch 1553622286
lat 42.95424271
uv 4.7
winddir 360
humidity 23
qcStatus 1
metric
temp 6
heatIndex 6
dewpt -14
windChill 3
windSpeed 8
windGust 14
pressure 1031.36
precipRate 0
precipTotal 0
elev 243

My modified script with the new json data is:

Code: Select all

<?php
$json_string = file_get_contents("https://api.weather.com/v2/pws/observations/current?stationId=IONTARIO226&format=json&units=m&apiKey=NEW API KEY");
$parsed_json = json_decode($json_string);
//observations
$observation_stationID = $parsed_json->{'observations'}->{'0'}->{'stationID'};
$observation_obsTimeUtc = $parsed_json->{'observations'}->{'0'}->{'obsTimeUtc'};
$observation_TimeLocal = $parsed_json->{'observations'}->{'obsTimeLocal'};
$observation_neighborhood = $parsed_json->{'observations'}->{'neighborhood'};
$observation_softwareType = $parsed_json->{'observations'}->{'softwareType'};
$observation_country = $parsed_json->{'observations'}->{'country'};
$observation_solarRadiation = $parsed_json->{'observations'}->{'solarRadiation'};
$observation_lon = $parsed_json->{'observations'}->{'lon'};
$observation_realtimeFrequency = $parsed_json->{'observations'}->{'realtimeFrequency'};
$observation_epoch = $parsed_json->{'observations'}->{'epoch'};
$observation_lat = $parsed_json->{'observations'}->{'lat'};
$observation_uv = $parsed_json->{'observations'}->{'uv'};
$observation_winddir = $parsed_json->{'observations'}->{'winddir'};
$observation_humidity = $parsed_json->{'observations'}->{'hunidity'};
$observation_qcStatus = $parsed_json->{'observations'}->{'qcStatus'};
//metric
$observation_temp = $parsed_json->{'observations'}->{'metric'}->{'temp'};
$observation_heatIndex = $parsed_json->{'observations'}->{'metric'}->{'heatIndex'};
$observation_dewpt = $parsed_json->{'observations'}->{'metric'}->{'dewpt'};
$observation_windChill = $parsed_json->{'observations'}->{'metric'}->{'windChill'};
$observation_windSpeed = $parsed_json->{'observations'}->{'metric'}->{'windSpeed'};
$observation_windGust = $parsed_json->{'observations'}->{'metric'}->{'windGust'};
$observation_pressure = $parsed_json->{'observations'}->{'metric'}->{'pressure'};
$observation_precipRate = $parsed_json->{'observations'}->{'metric'}->{'precipRate'};
$observation_prectpTotal = $parsed_json->{'observations'}->{'metric'}->{'precipTotal'};
echo "The weather at Station ${observation_stationID} ${observation_neighborhood} Ontario, Canada:<br />
${weather}
${icon_url}
<br /> 
Temperature is ${observation_TimeLocal} C°<br />
The heat index is ${observation_heatIndex} C° and the windchill ${observation_windChill} C°.  The dewpoint is at ${observation_dewpt} C°.<br />
Wind is from ${observation_winddir} degrees at ${observation_windSpeed} kph with gusts of ${observation_windGust} kph.<br />
The barometric pressure is ${observation_pressure} MB. There  has been ${observation_precipTotal} mm rain so far.<br />
The solar radiation is ${observation_solarRadiation} W/m<sup>2</sup> and UV index is ${observation_uv}.
\n";
?>
Since the new API does not appear to include any external data such as visibility I may just abandon the script. However I wanted to give it a try as that may be helpful if I wanted to use any of the other information that is available with the new API.

Any pointers appreciated,
Paul

Re: Using the new WU API

Posted: Tue 26 Mar 2019 7:39 pm
by ExperiMentor
I've no experience with the API, but the whole new look interface at WU this week seems to be buggy. Amongst errors I've noted are:
* Units wrong
. Temps in Fahrenheit marked as C
Capture.JPG
. Elevation in ft marked as m or no units at all
. Not using my default units
* Search for locations doesn't work for places that are not a single word

Probably more if I looked harder.
Hopefully just teething problems that will get sorted out!

Re: Using the new WU API

Posted: Tue 26 Mar 2019 7:50 pm
by mcrossley
Paul,

Looking at the WU API documentation, the current conditions JSON looks like this (formatted :) )...

Code: Select all

{
    observations: [
        {
            stationID: "KNCCARY89",
            obsTimeUtc: "2019-02-04T14:53:14Z",
            obsTimeLocal: "2019-02-04 09:53:14",
            neighborhood: "Highcroft Village",
            softwareType: "GoWunder 1337.9041ac1",
            country: "US",
            solarRadiation: 436.0,
            lon: -78.8759613,
            realtimeFrequency: null,
            epoch: 1549291994,
            lat: 35.80221176,
            uv: 1.2,
            winddir: 329,
            humidity: 71,
            qcStatus: 1,
            imperial: {
                temp: 53,
                heatIndex: 53,
                dewpt: 44,
                windChill: 53,
                windSpeed: 2,
                windGust: null,
                pressure: 30.09,
                precipRate: 0.0,
                precipTotal: 0.0,
                elev: 413
            }
        }
    ]
}
So your code looks right to me as far as I can see from a quick scan. What error are you getting on the page or in your PHP/web server logs?

Re: Using the new WU API

Posted: Tue 26 Mar 2019 8:59 pm
by f4phlyer
Forgive me if this is a rehash of any other like thread, but I needed a place to vent. :bash: I, like the prolific group of WU contributors and users, have been excluded by the new owners of WU, IBM. They offered a token api replacement to PWS contributors that regurgitates the data that we have provided to them and a truncated forecast and no current observations from which we have received the icons to dress up our sites. There is a substantial discussion on the WU API forum. Anyway I notice that Mark has resorted to the Met Office for his forecast and current observations. Really nice display. Unfortunately the Met Office does not offer data outside of the UK. After looking around I've decided on NOAA's weather API. I've been trying to adapt the WU api call and cache but it's been a struggle, that's why I came to the forum today to see if anyone else has been successful. NOAA is using a JSON format called JSON-LD. Another promising provider is BlackSky. My 2¢!

Re: Using the new WU API

Posted: Tue 26 Mar 2019 9:26 pm
by PaulMy
Thanks Mark,
You've done a much better formatting presentation of the json :oops:

Lots and lots of errors now with the old api and the same for the new.

For the old API script it is giving
[26-Mar-2019 21:07:51 UTC] PHP Notice: Undefined property: stdClass::$current_observation in /home/psoykkrhjuz3/public_html/weather/index.php on line 414
[26-Mar-2019 21:07:51 UTC] PHP Notice: Trying to get property of non-object in /home/psoykkrhjuz3/public_html/weather/index.php on line 414
[26-Mar-2019 21:07:51 UTC] PHP Notice: Undefined property: stdClass::$current_observation in /home/psoykkrhjuz3/public_html/weather/index.php on line 415
[26-Mar-2019 21:07:51 UTC] PHP Notice: Trying to get property of non-object in /home/psoykkrhjuz3/public_html/weather/index.php on line 415
for each of the $parsed_json lines and that is likely because the old API is no longer valid.

For the new API is is giving
[26-Mar-2019 21:07:51 UTC] PHP Notice: Trying to get property of non-object in /home/psoykkrhjuz3/public_html/weather/index.php on line 464
[26-Mar-2019 21:07:51 UTC] PHP Notice: Trying to get property of non-object in /home/psoykkrhjuz3/public_html/weather/index.php on line 464
[26-Mar-2019 21:07:51 UTC] PHP Notice: Trying to get property of non-object in /home/psoykkrhjuz3/public_html/weather/index.php on line 465
[26-Mar-2019 21:07:51 UTC] PHP Notice: Trying to get property of non-object in /home/psoykkrhjuz3/public_html/weather/index.php on line 465
for each of the $parsed_json lines.
The new API key should be correct as I get the full json data list through the URL with this API key.

Enjoy,
Paul

Re: Using the new WU API

Posted: Tue 26 Mar 2019 9:30 pm
by PaulMy
Forgive me if this is a rehash of any other like thread, but I needed a place to vent
I am not joining the WU bashing and just accept it for what it is. Hopefully they can get it back to what it was, and even better eventually.
Quite an active thread here http://www.wxforum.net/index.php?topic=33651.0

Enjoy,
Paul

Re: Using the new WU API

Posted: Tue 26 Mar 2019 10:26 pm
by saratogaWX
Here's a working version of your script with a few tweaks. Just insert your API key at the top.

Code: Select all

<?php
$apiKey = '--api-key-here--';
$station = 'IONTARIO226';
/*
stdClass Object
(
    [observations] => Array
        (
            [0] => stdClass Object
                (
                    [stationID] => IONTARIO226
                    [obsTimeUtc] => 2019-03-26T22:14:58Z
                    [obsTimeLocal] => 2019-03-26 18:14:58
                    [neighborhood] => Komoka Village
                    [softwareType] => Cumulus v1.9.4
                    [country] => CA
                    [solarRadiation] => 257
                    [lon] => -81.43488312
                    [realtimeFrequency] => 
                    [epoch] => 1553638498
                    [lat] => 42.95424271
                    [uv] => 0
                    [winddir] => 360
                    [humidity] => 35
                    [qcStatus] => 1
                    [metric] => stdClass Object
                        (
                            [temp] => 5
                            [heatIndex] => 5
                            [dewpt] => -9
                            [windChill] => 4
                            [windSpeed] => 5
                            [windGust] => 21
                            [pressure] => 1030.27
                            [precipRate] => 0
                            [precipTotal] => 0
                            [elev] => 243
                        )

                )

        )

)*/
   $STRopts = array(
	  'ssl'=>array(
	  'method'=>"GET",
	  'protocol_version' => 1.1,
    'verify_peer' => false,
	  'header'=>"Cache-Control: no-cache, must-revalidate\r\n" .
				"Cache-control: max-age=0\r\n" .
				"Connection: close\r\n" .
				"User-agent: Mozilla/5.0 (quake-json.php - saratoga-weather.org)\r\n" .
				"Accept: application/ld+json\r\n"
	  )
	);
	
$STRcontext = stream_context_create($STRopts);
$url = "https://api.weather.com/v2/pws/observations/current?stationId=$station&format=json&units=m&apiKey=$apiKey";

$json_string = file_get_contents($url,false,$STRcontext);
$parsed_json = json_decode($json_string);
//print_r($parsed_json);

//observations
$observation_stationID = $parsed_json->{'observations'}[0]->{'stationID'};
$observation_obsTimeUtc = $parsed_json->{'observations'}[0]->{'obsTimeUtc'};
$observation_TimeLocal = $parsed_json->{'observations'}[0]->{'obsTimeLocal'};
$observation_neighborhood = $parsed_json->{'observations'}[0]->{'neighborhood'};
$observation_softwareType = $parsed_json->{'observations'}[0]->{'softwareType'};
$observation_country = $parsed_json->{'observations'}[0]->{'country'};
$observation_solarRadiation = $parsed_json->{'observations'}[0]->{'solarRadiation'};
$observation_lon = $parsed_json->{'observations'}[0]->{'lon'};
$observation_realtimeFrequency = $parsed_json->{'observations'}[0]->{'realtimeFrequency'};
$observation_epoch = $parsed_json->{'observations'}[0]->{'epoch'};
$observation_lat = $parsed_json->{'observations'}[0]->{'lat'};
$observation_uv = $parsed_json->{'observations'}[0]->{'uv'};
$observation_winddir = $parsed_json->{'observations'}[0]->{'winddir'};
$observation_humidity = $parsed_json->{'observations'}[0]->{'humidity'};
$observation_qcStatus = $parsed_json->{'observations'}[0]->{'qcStatus'};
//metric
$observation_temp = $parsed_json->{'observations'}[0]->{'metric'}->{'temp'};
$observation_heatIndex = $parsed_json->{'observations'}[0]->{'metric'}->{'heatIndex'};
$observation_dewpt = $parsed_json->{'observations'}[0]->{'metric'}->{'dewpt'};
$observation_windChill = $parsed_json->{'observations'}[0]->{'metric'}->{'windChill'};
$observation_windSpeed = $parsed_json->{'observations'}[0]->{'metric'}->{'windSpeed'};
$observation_windGust = $parsed_json->{'observations'}[0]->{'metric'}->{'windGust'};
$observation_pressure = $parsed_json->{'observations'}[0]->{'metric'}->{'pressure'};
$observation_precipRate = $parsed_json->{'observations'}[0]->{'metric'}->{'precipRate'};
$observation_precipTotal = $parsed_json->{'observations'}[0]->{'metric'}->{'precipTotal'};
echo "The weather at Station ${observation_stationID} ${observation_neighborhood} Ontario, Canada ${observation_TimeLocal}:<br />
Temperature is ${observation_temp} C&deg;<br />
The heat index is ${observation_heatIndex} C&deg; and the windchill ${observation_windChill} C&deg;.  The dewpoint is at ${observation_dewpt} C&deg;.<br />
Wind is from ${observation_winddir} degrees at ${observation_windSpeed} kph with gusts of ${observation_windGust} kph.<br />
The barometric pressure is ${observation_pressure} MB. There  has been ${observation_precipTotal} mm rain so far.<br />
The solar radiation is ${observation_solarRadiation} W/m<sup>2</sup> and UV index is ${observation_uv}.
\n";
?>
It produces this output
The weather at Station IONTARIO226 Komoka Village Ontario, Canada 2019-03-26 18:22:00:
Temperature is 4 C°
The heat index is 4 C° and the windchill 1 C°. The dewpoint is at -9 C°.
Wind is from 360 degrees at 18 kph with gusts of 18 kph.
The barometric pressure is 1030.34 MB. There has been 0 mm rain so far.
The solar radiation is 229 W/m2 and UV index is 0.
The main issue was how the array was addressed in the returned values.

Enjoy!
Best regards,
Ken

Re: Using the new WU API

Posted: Wed 27 Mar 2019 1:48 am
by PaulMy
Great, thanks very much Ken.

I see one obvious change from my earlier script. I presume the (0) addition is needed for when more than one range of data is provided, i.e. 5-day forecast. Can't say that I quite understand all of the other tweaks :oops: but time will tell.

p.s. I've had a bit of fun and now have added the 5-day forecast http://www.komokaweather.com
Now to see how to add some variables for the conditions image. Who knows where this will end :lol:

Enjoy,
Paul

Re: Using the new WU API

Posted: Wed 27 Mar 2019 1:56 am
by beteljuice
p.s. I've had a bit of fun and now have added the 5-day forecast http://www.komokaweather.com/weather
Now to see how to add some variables for the conditions image. Who knows where this will end :lol:
In tears ?

That link still shows your 'old' code ....

Re: Using the new WU API

Posted: Wed 27 Mar 2019 2:44 am
by PaulMy
beteljuice, you've just spotted another one of my weaknesses - I have too many versions of the same thing :oops:

Unfortunately I forgot that my /weather/index.htm (which is one of the Cumulus standard web files) is somewhat different to my /weather/index.php (which is processed and updated as an extra web file) and I need to use an index file with a .php extension for any PHP code in it to work. I should have used http://www.komokaweather.com/weather/index.php as the link to see the updated page or just http://www.komokaweather.com to see the full page.

Now corrected.

Enjoy,
Paul

Re: Using the new WU API

Posted: Wed 27 Mar 2019 7:39 pm
by f4phlyer
Thanks to you Paul and SaratogWX. It'll be challenging but rewarding to make the updates.

Re: Using the new WU API

Posted: Tue 09 Apr 2019 6:21 am
by vpokroglo
Is it possible to get data from more stations on one page?

Re: Using the new WU API

Posted: Tue 09 Apr 2019 1:24 pm
by PaulMy
If you have more than one WU station and associated API I can't see why you couldn't get both to display on the same page with proper coding to separate the stations and variables.

Enjoy,
Paul