View Full Version : Need Some PHP Help
crazyandy
25-04-2008, 17:57
OK I need some php help here.
I know this hasnt realy got much to do with Teamspeak, but I dont know who else to get help from but you brainy people here.
Ok I need to extract data from an XML file. I can get it into arrays but dont know how to extract it out of the array.
Here the PHP
<?php
$xml = simplexml_load_file("file.xml");
$result = $xml->xpath("weather");
print_r($result);
?>
The above prints out the following.
Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array (
[module_id] => 0 [tab_id] => 0 ) [current_conditions] =>
SimpleXMLElement Object ( [condition] => SimpleXMLElement Object (
[@attributes] => Array ( [data] => Cloudy ) ) [temp_f] =>
SimpleXMLElement Object ( [@attributes] => Array ( [data] => 55 ) )
[temp_c] => SimpleXMLElement Object ( [@attributes] => Array ( [data]
=> 13 ) ) [humidity] => SimpleXMLElement Object ( [@attributes] =>
Array ( [data] => Humidity: 82% ) ) [icon] => SimpleXMLElement Object (
[@attributes] => Array ( [data] => /images/weather/cloudy.gif ) )
[wind_condition] => SimpleXMLElement Object ( [@attributes] => Array (
[data] => Wind: S at 16 mph ) ) ) ) )
The part that I need the most is [data] => Cloudy.
If anyone can help I would be very greatful.
Thanks,
Andy
maxi1990
25-04-2008, 18:46
do the official php sites not provide any xml reader examples?
crazyandy
25-04-2008, 18:47
Yes that is were I got the bit of php code in my previous post, but I just want to know how to get the data out of the arrays.
crazyandy
30-04-2008, 20:50
Anyone got any solutions???
if you could post the xml file i could tell you how to get the data.
crazyandy
09-05-2008, 14:04
Sorry for not replying quicker been bussy with other issues.
The XML is as follows:
<xml_api_reply version="1">
−
<weather module_id="0" tab_id="0">
−
<forecast_information>
<city data=""/>
<postal_code data=""/>
<latitude_e6 data="54650001"/>
<longitude_e6 data="-6219999"/>
<forecast_date data="2008-05-09"/>
<current_date_time data="2008-05-09 07:50:00 +0000"/>
<unit_system data="US"/>
</forecast_information>
−
<current_conditions>
<condition data="Mostly Cloudy"/>
<temp_f data="59"/>
<temp_c data="15"/>
<humidity data="Humidity: 77%"/>
<icon data="/images/weather/mostly_cloudy.gif"/>
<wind_condition data="Wind: SE at 8 mph"/>
</current_conditions>
−
<forecast_conditions>
<day_of_week data="Today"/>
<low data="48"/>
<high data="64"/>
<icon data="/images/weather/mostly_cloudy.gif"/>
<condition data="Mostly Cloudy"/>
</forecast_conditions>
−
<forecast_conditions>
<day_of_week data="Sat"/>
<low data="46"/>
<high data="62"/>
<icon data="/images/weather/chance_of_rain.gif"/>
<condition data="Chance of Rain"/>
</forecast_conditions>
−
<forecast_conditions>
<day_of_week data="Sun"/>
<low data="50"/>
<high data="68"/>
<icon data="/images/weather/mostly_sunny.gif"/>
<condition data="Partly Sunny"/>
</forecast_conditions>
−
<forecast_conditions>
<day_of_week data="Mon"/>
<low data="50"/>
<high data="69"/>
<icon data="/images/weather/mostly_sunny.gif"/>
<condition data="Mostly Sunny"/>
</forecast_conditions>
</weather>
</xml_api_reply>
Thanks for trying to help me, much apretiated.
Andy
ok i developed a class to parse this thingie.
Actually a class is not needed but i thought it might be nice for extending it.
At the moment the class just parses the xml (which is loaded via URI) and returns all it contents as an associative array via "getall()"-method.
I tried to implement a "getone($which_one)"-method but didnt worked yet because the array is multidimensional. I ve just to work on on that xD
So there is the code for you (the information is currently loaded from "weather.xml" in the current working directory. If you want to change this, just pass on a filename to the constructor to load from
<?php
error_reporting(E_ALL);
class weatherxml {
public $version;
public $module_id;
public $tab_id;
public $city;
public $postal_code;
public $latitude_e6;
public $longitude_e6;
public $forecast_date;
public $forecast_date_timestamp;
public $current_date_time;
public $current_date_time_timestamp;
public $unit_system;
public $current;
public $forecast;
public function __construct($url = "weather.xml") {
if(($xml = simplexml_load_file($url)) === false)
die("XML Reading Error");
$this->version = (int) $xml->attributes()->version;
$this->module_id = (int) $xml->weather->attributes()->module_id;
$this->tab_id = (int) $xml->weather->attributes()->tab_id;
$shortcut = &$xml->weather->forecast_information;
$this->city = (string) $shortcut->city->attributes()->data;
$this->postal_code = (int) $shortcut->postal_code->attributes()->data;
$this->latitude_e6 = (int) $shortcut->latitude_e6->attributes()->data;
$this->longitude_e6 = (int) $shortcut->longitude_e6->attributes()->data;
$this->forecast_date = (string) $shortcut->forecast_date->attributes()->data;
$this->forecast_date_timestamp = strtotime($this->forecast_date);
$this->current_date_time = (string) $shortcut->current_date_time->attributes()->data;
$this->current_date_time_timestamp = (strtotime(substr($this->current_date_time, 0, 19)));
$this->unit_system = (string) $shortcut->unit_system->attributes()->data;
$shortcut = &$xml->weather->current_conditions;
$this->current = array();
$current = &$this->current;
$current["condition"] = (string) $shortcut->condition->attributes()->data;
$current["temp_f"] = (int) $shortcut->temp_f->attributes()->data;
$current["temp_c"] = (int) $shortcut->temp_c->attributes()->data;
$current["humidity"] = (int) $shortcut->humidity->attributes()->data;
$current["icon"] = (string) $shortcut->icon->attributes()->data;
$current["wind_condition"] = (string) substr($shortcut->wind_condition->attributes()->data, 6);
$forecast = &$this->forecast;
//We have 3 forecasts
$i = 0;
foreach($xml->weather->forecast_conditions as $input) {
//the key will be the timestamp for the day
//for which the forecast is
$c_fc = &$forecast[$i];
$c_fc["day_of_week"] = (string) $input->day_of_week->attributes()->data;
$c_fc["low"] = (int) $input->low->attributes()->data;
$c_fc["high"] = (int) $input->high->attributes()->data;
$c_fc["icon"] = (string) $input->icon->attributes()->data;
$c_fc["condition"] = (string) $input->condition->attributes()->data;
$i++;
}
}
function getall() {
return (array) $this;
}
}
$weather_parser = new weatherxml();
$weather_info = $weather_parser->getall();
?>
<pre>
<?php
print_r($weather_info);
?>
</pre>
Best regards Frank
crazyandy
16-05-2008, 11:04
Lovely I cant thank you enough for helping me out, Thanks sooo much.
I havent tried it yet as I am currently in uni, but ill be home tomo so ill run it then.
So all ill need to do is work out how to pull the exact piece of data i need from the array.
Emmm any ideas anyone on how to pull data from a multidimensional array?
Thanks soo much Ch3ck3r, Ill get back to you on how it works.
Andy
getting the data you want is really easy.
The first thing you need to do is to get the data which is really easy:
//Location of the xml file with the information
$url = "weather.xml";
//Parse the file
$parsed = new weatherxml($url);
//Get the data
$data = $parsed->getall();
now $data contains the following data:
Array
(
[version] => 1
[module_id] => 0
[tab_id] => 0
[city] => New York
[postal_code] => 0
[latitude_e6] => 54650001
[longitude_e6] => -6219999
[forecast_date] => 2008-05-09
[forecast_date_timestamp] => 1210284000
[current_date_time] => 2008-05-09 07:50:00 +0000
[current_date_time_timestamp] => 1210312200
[unit_system] => US
[current] => Array
(
[condition] => Mostly Cloudy
[temp_f] => 59
[temp_c] => 15
[humidity] => 0
[icon] => /images/weather/mostly_cloudy.gif
[wind_condition] => SE at 8 mph
)
[forecast] => Array
(
[0] => Array
(
[day_of_week] => Today
[low] => 48
[high] => 64
[icon] => /images/weather/mostly_cloudy.gif
[condition] => Mostly Cloudy
)
[1] => Array
(
[day_of_week] => Sat
[low] => 46
[high] => 62
[icon] => /images/weather/chance_of_rain.gif
[condition] => Chance of Rain
)
[2] => Array
(
[day_of_week] => Sun
[low] => 50
[high] => 68
[icon] => /images/weather/mostly_sunny.gif
[condition] => Partly Sunny
)
[3] => Array
(
[day_of_week] => Mon
[low] => 50
[high] => 69
[icon] => /images/weather/mostly_sunny.gif
[condition] => Mostly Sunny
)
)
)
so now, if you need (e.g.) the condition of the forecast in 3 days this would be the way
echo $data["forecast"][3]["condition"]
This would output "Mostly Sunny". I've made the corresponding line bold in the data above, so that you can find what is printed out.
I hope you are able to use this now ^^
crazyandy
17-05-2008, 12:37
Ch3ck3r your a genius, it works a charm. Thank you, Thank you.
I cant thank you enough for helpen me.
Andy
vBulletin® v3.7.0, Copyright ©2000-2008, Jelsoft Enterprises Ltd.