Solved

Php get information from xml data

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with Syntax 9 26
What is the best PDF generator to use? 1 27
html input type 3 21
updating the date data 12 21
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

912 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

19 Experts available now in Live!

Get 1:1 Help Now