To access the value in XML you must call the tag name and access the value using foo["value"].
For example, for:
<city id="3469115" name="Armazém">
Stays:
$xml->city["name"];
To read the XML
I used the curl
as follows:
<?php
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/xml; charset=ISO-8859-1"));
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$xml = get_data("http://api.openweathermap.org/data/2.5/weather?q=Armazem,SC&mode=xml");
$xml = simplexml_load_string($xml);
echo $xml->temperature["value"];
?>
Thank you for the answer, solved my problem!
– Tiago Boeing
Could you just quote how I can get country for example in this xml? I did it like this: || echo $xml->country; || but it doesn’t work.
– Tiago Boeing
Note that
<country>
is inside<city>
then stay$xml->city->country;
.– Lucas Lima
Thanks, it worked!
– Tiago Boeing