// 014-CodigoPHP.php
// Versión 1.0 10/12/2017 Juan Manuel Cueva Lovelle. Universidad de Oviedo
$archivo = "./014-Oviedo.xml";
// Obtiene el archivo XML que se pasa como parámetro y se recibe como un string
$datos = file_get_contents($archivo);
if($datos==null) {
echo "<h3>Error en el archivo XML recibido</h3>";
}
else {
echo "<h3>XML recibido correctamente</h3>";
}
echo "<h2>XML</h2>";
$datos =preg_replace("/>\s*</",">\n<",$datos);
echo '<pre>', htmlentities($datos), '</pre>';
// Se convierte el string en un objeto XML
$xml = new SimpleXMLElement($datos);
echo "<h2>XML como objeto SimpleXMLElement</h2>";
echo '<pre>', print_r($xml), '</pre>';
echo "<h2>Datos</h2>";
$icono = $xml->weather['icon'];
$URLicono = "http://openweathermap.org/img/w/" . $icono . ".png";
$descripcion = $xml->weather['value'];
echo "<img src = '$URLicono' alt = '$descripcion' >";
echo "<p>Ciudad: {$xml->city['name']}</p>";
echo "<p>País: {$xml->city->country}</p>";
echo "<p>Coordenadas: Longitud {$xml->city->coord['lon']} grados</p>";
echo "<p>Coordenadas: Latitud {$xml->city->coord['lat']} grados</p>";
echo "<p>Temperatura: {$xml->temperature['value']} grados Celsius</p>";
echo "<p>Temperatura mínima: {$xml->temperature['min']} grados Celsius</p>";
echo "<p>Temperatura máxima: {$xml->temperature['max']} grados Celsius</p>";
echo "<p>Temperatura (unidades): {$xml->temperature['unit']} </p>";
echo "<p>Humedad: {$xml->humidity['value']} {$xml->humidity['unit']} </p>";
echo "<p>Presión: {$xml->pressure['value']} {$xml->pressure['unit']} </p>";
echo "<p>Velocidad del viento: {$xml->wind->speed['value']} metros/segundo</p>";
echo "<p>Tipo de viento: {$xml->wind->speed['name']} </p>";
echo "<p>Dirección del viento: {$xml->wind->direction['value']} grados</p>";
echo "<p>Orientación del viento: {$xml->wind->direction['code']} </p>";
echo "<p>Nombre del viento: {$xml->wind->direction['name']} </p>";
echo "<p>Nubosidad: {$xml->clouds['value']} %. Descripción: {$xml->clouds['name']} </p>";
echo "<p>Visibilidad: {$xml->visibility['value']} metros </p>";
echo "<p>Estado del tiempo: {$xml->weather['value']} </p>";
$fechaHoraAmanecer = new DateTime($xml->city->sun['rise'], new DateTimeZone('Europe/Madrid'));
echo "<p>Amanece: " . $fechaHoraAmanecer->format("d/m/Y G:i:sP e") . "</p>";
$fechaHoraOscurecer = new DateTime($xml->city->sun['set'], new DateTimeZone('Europe/Madrid'));
echo "<p>Oscurece: " . $fechaHoraOscurecer->format("d/m/Y G:i:sP e") . "</p>";
$fechaHoraActualizacion = new DateTime($xml->lastupdate['value'], new DateTimeZone('Europe/Madrid'));
echo "<p>Última actualización: " .$fechaHoraActualizacion->format("d/m/Y G:i:sP e") ."</p>";
<?xml version="1.0" encoding="UTF-8"?> <current> <city id="3114711" name="Oviedo"> <coord lon="-5.84" lat="43.36"> </coord> <country>ES</country> <sun rise="2017-12-08T07:43:50" set="2017-12-08T16:47:22"> </sun> </city> <temperature value="9" min="9" max="9" unit="metric"> </temperature> <humidity value="100" unit="%"> </humidity> <pressure value="1029" unit="hPa"> </pressure> <wind> <speed value="0.25" name=""> </speed> <gusts> </gusts> <direction value="63.5068" code="ENE" name="East-northeast"> </direction> </wind> <clouds value="75" name="broken clouds"> </clouds> <visibility value="4000"> </visibility> <precipitation mode="no"> </precipitation> <weather number="300" value="llovizna ligera" icon="09n"> </weather> <lastupdate value="2017-12-08T17:30:00"> </lastupdate> </current>
SimpleXMLElement Object ( [city] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 3114711 [name] => Oviedo ) [coord] => SimpleXMLElement Object ( [@attributes] => Array ( [lon] => -5.84 [lat] => 43.36 ) [0] => ) [country] => ES [sun] => SimpleXMLElement Object ( [@attributes] => Array ( [rise] => 2017-12-08T07:43:50 [set] => 2017-12-08T16:47:22 ) [0] => ) ) [temperature] => SimpleXMLElement Object ( [@attributes] => Array ( [value] => 9 [min] => 9 [max] => 9 [unit] => metric ) [0] => ) [humidity] => SimpleXMLElement Object ( [@attributes] => Array ( [value] => 100 [unit] => % ) [0] => ) [pressure] => SimpleXMLElement Object ( [@attributes] => Array ( [value] => 1029 [unit] => hPa ) [0] => ) [wind] => SimpleXMLElement Object ( [speed] => SimpleXMLElement Object ( [@attributes] => Array ( [value] => 0.25 [name] => ) [0] => ) [gusts] => SimpleXMLElement Object ( [0] => ) [direction] => SimpleXMLElement Object ( [@attributes] => Array ( [value] => 63.5068 [code] => ENE [name] => East-northeast ) [0] => ) ) [clouds] => SimpleXMLElement Object ( [@attributes] => Array ( [value] => 75 [name] => broken clouds ) [0] => ) [visibility] => SimpleXMLElement Object ( [@attributes] => Array ( [value] => 4000 ) [0] => ) [precipitation] => SimpleXMLElement Object ( [@attributes] => Array ( [mode] => no ) [0] => ) [weather] => SimpleXMLElement Object ( [@attributes] => Array ( [number] => 300 [value] => llovizna ligera [icon] => 09n ) [0] => ) [lastupdate] => SimpleXMLElement Object ( [@attributes] => Array ( [value] => 2017-12-08T17:30:00 ) [0] => ) ) 1
Ciudad: Oviedo
País: ES
Coordenadas: Longitud -5.84 grados
Coordenadas: Latitud 43.36 grados
Temperatura: 9 grados Celsius
Temperatura mínima: 9 grados Celsius
Temperatura máxima: 9 grados Celsius
Temperatura (unidades): metric
Humedad: 100 %
Presión: 1029 hPa
Velocidad del viento: 0.25 metros/segundo
Tipo de viento:
Dirección del viento: 63.5068 grados
Orientación del viento: ENE
Nombre del viento: East-northeast
Nubosidad: 75 %. Descripción: broken clouds
Visibilidad: 4000 metros
Estado del tiempo: llovizna ligera
Amanece: 08/12/2017 7:43:50+01:00 Europe/Madrid
Oscurece: 08/12/2017 16:47:22+01:00 Europe/Madrid
Última actualización: 08/12/2017 17:30:00+01:00 Europe/Madrid