Introducción
Muchas veces se ve que el clima de leer páginas web de un feed RSS en otro sitio, aquí malos mostrar cómo leer el clima en su sitio de Yahoo! Weathers RSS feed con PHP.
Dónde obtener el código XML de
Para leer el tiempo de Yahoo! tiempo, enviar una solicitud a Yahoo! que es como sigue:
http://weather.yahooapis.com/forecastrss?p=SFXX0044&u=c
donde p es el código de localización...En este tutorial estoy usando "Pretoria, Sudáfrica" (de donde soy), y como usted puede ver el código de ubicación es SFXX0044, tengo esto desde http://weather.yahoo.com y yo configuro para mi ubicación, y el código se encuentra entonces en la dirección...Alternativamente, si usted vive en los EE.UU. sólo puede utilizar el código postal donde está. A continuación, en "U" es la unidad en la que el tiempo se debe mostrar, elegí "C" para C, pero también puedes elegir la opción "f" para Fahrenheit.
Lo que el XML parece
He creado esta layut de lo que la respuesta XML parece que se volvió de Yahoo! El Tiempo:
<channel>
<title>
<link></link>
<description></description>
<language></language>
<lastBuildDate></lastBuildDate>
<ttl></ttl>
<yweather:location city region country />
<yweather:units temperature distance pressure speed />
<yweather:wind chill direction speed />
<yweather:atmosphere humidity visibility pressure rising />
<yweather:astronomy sunrise sunset />
<image>
<title></title>
<width></width>
<height></height>
<link></link>
<url></url>
</image>
<item>
<title></title>
<geo:lat></geo:lat>
<geo:long></geo:long>
<link></link>
<pubDate></pubDate>
<yweather:condition text code temp date>
<description></description>
<yweather:forecast day date low high text code />
<yweather:forecast day date low high text code />
<guid Ispermalink></guid>
</item>
</channel>
- <channel>
- <title>
- <link></link>
- <description></description>
- <language></language>
- <lastBuildDate></lastBuildDate>
- <ttl></ttl>
- <yweather:location city region country />
- <yweather:units temperature distance pressure speed />
- <yweather:wind chill direction speed />
- <yweather:atmosphere humidity visibility pressure rising />
- <yweather:astronomy sunrise sunset />
- <image>
- <title></title>
- <width></width>
- <height></height>
- <link></link>
- <url></url>
- </image>
- <item>
- <title></title>
- <geo:lat></geo:lat>
- <geo:long></geo:long>
- <link></link>
- <pubDate></pubDate>
- <yweather:condition text code temp date>
- <description></description>
- <yweather:forecast day date low high text code />
- <yweather:forecast day date low high text code />
- <guid Ispermalink></guid>
- </item>
- </channel>
Como puede ver, nuestro "root" elemento se denomina "canal", y debajo de eso tenemos también otros elementos, el único elemento que eant ir de aquí es la siguiente:
channel-> item-> description
Así que permite echar un vistazo al código que vamos a utilizar:
El código
Yo voy a mostrar el código con comentarios en el medio para que pueda ver lo que estoy haciendo
<?php
//I am using the DOM(Document Object Model) library to read the entire XML document into memory first.
$doc = new DOMDocument();
$doc->load('http://weather.yahooapis.com/forecastrss?p=SFXX0044&u=c');
//now I get all elements inside this document with the following name "channel", this is the 'root'
$channel = $doc->getElementsByTagName("channel");
//now I go through each item withing $channel
foreach($channel as $chnl)
{
//I then find the 'item' element inside that loop
$item = $chnl->getElementsByTagName("item");
foreach($item as $itemgotten)
{
//now I search within '$item' for the element "description"
$describe = $itemgotten->getElementsByTagName("description");
//once I find it I create a variable named "$description" and assign the value of the Element to it
$description = $describe->item(0)->nodeValue;
//and display it on-screen
echo $description;
}
}
?>
- <?php
- //I am using the DOM(Document Object Model) library to read the entire XML document into memory first.
- $doc = new DOMDocument();
- $doc->load('http://weather.yahooapis.com/forecastrss?p=SFXX0044&u=c');
- //now I get all elements inside this document with the following name "channel", this is the 'root'
- $channel = $doc->getElementsByTagName("channel");
- //now I go through each item withing $channel
- foreach($channel as $chnl)
- {
- //I then find the 'item' element inside that loop
- $item = $chnl->getElementsByTagName("item");
- foreach($item as $itemgotten)
- {
- //now I search within '$item' for the element "description"
- $describe = $itemgotten->getElementsByTagName("description");
- //once I find it I create a variable named "$description" and assign the value of the Element to it
- $description = $describe->item(0)->nodeValue;
- //and display it on-screen
- echo $description;
- }
- }
- ?>
http://ph81luc.net46.net/weather.phpHe probado también con otro sitio web y no recibir ningún resultado la ejecución de esta página. Que la razón por la que el lugar que "Hola palabra" texto en el archivo; para fines de ensayo;