Solved

Php get information from xml data

Posted on 2012-04-11
1
464 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 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

739 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