?
Solved

Php get information from xml data

Posted on 2012-04-11
1
Medium Priority
?
477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
chrisroch earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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, …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

719 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