Reputation: 1397
I'm new to PHP/XML and I would like to add Google's XML Weather info. and add that info to my .PHP page.
I will use this URL as an example: https://www.google.com/ig/api?weather=12601&hl=en&referrer=googlecalendar
the output I get at that URL is:
<?xml version="1.0"?>
<xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" >
<forecast_information>
<city data="Poughkeepsie, NY"/>
<postal_code data="12601"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2013-08-15"/>
<current_date_time data="1970-01-01 00:00:00 +0000"/>
<unit_system data="US"/>
</forecast_information>
<current_conditions>
<condition data="Partly Cloudy"/>
<temp_f data="78"/>
<temp_c data="26"/>
<humidity data="Humidity: 44%"/>
<icon data="/ig/images/weather/partly_cloudy.gif"/>
<wind_condition data="Wind: N at 2 mph"/>
</current_conditions>
<forecast_conditions>
<day_of_week data="Thu"/>
<low data="52"/>
<high data="81"/>
<icon data="/ig/images/weather/sunny.gif"/>
<condition data="Clear"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Fri"/>
<low data="52"/>
<high data="82"/>
<icon data="/ig/images/weather/partly_cloudy.gif"/>
<condition data="Partly Cloudy"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Sat"/>
<low data="57"/>
<high data="84"/>
<icon data="/ig/images/weather/partly_cloudy.gif"/>
<condition data="Partly Cloudy"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="Sun"/>
<low data="59"/>
<high data="79"/>
<icon data="/ig/images/weather/cloudy.gif"/>
<condition data="Cloudy"/>
</forecast_conditions>
</weather>
</xml_api_reply>
I would just like to extract the following info. but I don't know how to do it, I would like to use the min. amount of code if possible.
Like it to display in .PHP page like this:
Currently: Partly Cloudy - 78*
Thu: Clear - Hi: 81* Lo: 52*
Fri: Partly Cloudy - Hi: 82* Lo: 52*
Sat: Partly Cloudy - Hi: 84* Lo: 57*
Sun: Cloudy - Hi: 79* Lo: 59*
Thanks.
Upvotes: 1
Views: 690
Reputation: 76646
Use SimpleXML
to parse the string and convert into an object.
$string = '...';
$xml = simplexml_load_string($string);
print_r($xml);
Demo: http://codepad.org/ig8PuWBQ
Upvotes: 3