Solved

Php get information from xml data

Posted on 2012-04-11
1
442 Views
Last Modified: 2012-04-12
Hi E's, I want use Goggle Weather Api in php.
For make a query to the API I use this URL: http://www.google.com/ig/api?weather= + the name of the city.
The result was:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<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="Ayamonte, Andalusia"/>
<postal_code data="ayamonte"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2012-04-11"/>
<current_date_time data="1970-01-01 00:00:00 +0000"/>
<unit_system data="SI"/>
</forecast_information>
<current_conditions>
<condition data="Limpo"/>
<temp_f data="61"/>
<temp_c data="16"/>
<humidity data="Humidade: 68%"/>
<icon data="/ig/images/weather/sunny.gif"/>
<wind_condition data="Vento: O a 24 km/h"/>
</current_conditions>
<forecast_conditions>
<day_of_week data="qua"/>
<low data="10"/>
<high data="23"/>
<icon data="/ig/images/weather/sunny.gif"/>
<condition data="Limpo"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="qui"/>
<low data="8"/>
<high data="20"/>
<icon data="/ig/images/weather/chance_of_rain.gif"/>
<condition data="Possibilidade de chuva"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="sex"/>
<low data="10"/>
<high data="22"/>
<icon data="/ig/images/weather/sunny.gif"/>
<condition data="Limpo"/>
</forecast_conditions>
<forecast_conditions>
<day_of_week data="sáb"/>
<low data="10"/>
<high data="21"/>
<icon data="/ig/images/weather/mostly_sunny.gif"/>
<condition data="Parcialmente nublado"/>
</forecast_conditions>
</weather>
</xml_api_reply>

Open in new window


If I want get from xml result all "<low data"  and all "<high data" and put them in the arrays $lowdata and highdata, how I do?

The best regards, JC
0
Comment
Question by:Pedro Chagas
1 Comment
 
LVL 1

Accepted Solution

by:
chrisroch earned 500 total points
ID: 37836082
You can use the SimpleXMLElement implementation to develop the functionality you need. See my sample code below:

<?php
	$low_data = array();
	$high_data = array();
	
	$xml_element = simplexml_load_file("http://www.google.com/ig/api?weather=miami");
	
	foreach($xml_element->weather->forecast_conditions as $condition){
		$low_data[(string)$condition->day_of_week['data']] = (string)$condition->low['data'];
		$high_data[(string)$condition->day_of_week['data']] = (string)$condition->high['data'];
	}
	
	echo '<pre>';
	
	echo 'Low Data -> '; print_r($low_data);
	echo '<br><br>';
	
	echo 'High Data -> '; print_r($high_data);
?>

Open in new window


The corresponding output from the above code will be as follows:

Low Data -> Array
(
    [Wed] => 68
    [Thu] => 70
    [Fri] => 68
    [Sat] => 70
)


High Data -> Array
(
    [Wed] => 84
    [Thu] => 84
    [Fri] => 81
    [Sat] => 79
)

Open in new window


As shown above the $low_data and $high_data variables are loaded with the required temperatures, indexed by their respective days.

Hope this helps...
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now