Wednesday, 30 July 2014

Getting values from Wunderground API with SimpleXML in PHP



Have been experimenting with a number of weather xml api's, have followed same process/logic for each and worked fine with a number of them but Wunderground API has me stumped.



$XML_WUNDER = simplexml_load_string('mylocalcopyofapiresponse.xml', null, LIBXML_NOCDATA)
print_r($xml_wunder);


Results in:



SimpleXMLElement Object
(
[version] => 0.1
[termsofService] => http://ift.tt/1c1wgoz
[features] => SimpleXMLElement Object
(
[feature] => forecast10day




)

[forecast] => SimpleXMLElement Object
(
[txt_forecast] => SimpleXMLElement Object
(
[date] => 11:20 PM NZST
[forecastdays] => SimpleXMLElement Object
(
[forecastday] => Array
(
[0] => SimpleXMLElement Object
(
[period] => 0
[icon] => sleet
[icon_url] => http://ift.tt/XfFTAM
[title] => Thursday
[fcttext] => A mix of wintry precipitation in the morning. Then periods of rain expected in the afternoon. High 37F. Winds N at 5 to 10 mph. Chance of precip 100%. Snow and ice accumulating 1 to 3 inches.
[fcttext_metric] => A mix of wintry precipitation in the morning. Then periods of rain expected in the afternoon. High 3C. Winds N at 10 to 15 km/h. Chance of precip 100%. 3-7cm of snow and ice expected.
[pop] => 100
)

[1] => SimpleXMLElement Object
(
[period] => 1

.....trimmed for brevity


The original response is as shown in the apigee console at the bottom of this page http://ift.tt/ULw8sn


I would expect



$xml_wunder->forecast->txt_forecast->forecastdays->forecastday[0]->fcttext_metric


To return:



A mix of wintry precipitation...........cip 100%. 3-7cm of snow and ice expected.


But get no response at all. This is on a page (xampp localhost) with two other weather xml api responses from different sources, all use same cache function and are identical in mark up, the others work as expected.


I suspect something to do with how the array is formatted as is not consistent with other api responses, leading to an error in my formatting. As such there's potentially a suitable answer out there, I'm just not aware of the terms to search for in order to get there.


Any pointers gratefully received.


No comments:

Post a Comment