PHP:使用simple_xml_load_file加载多个XML提要

时间:2011-11-04 15:37:47

标签: php simplexml

我正在通过雅虎的天气API检索多个天气预报 -

$stockholm = simplexml_load_file("http://weather.yahooapis.com/forecastrss?w=906057&u=c");
$stockholm->registerXpathNamespace('yweather', 'http://xml.weather.yahoo.com/ns/rss/1.0'); 
$children = $stockholm->xpath('//channel/item/yweather:condition'); 
echo '<li><img class="c' . $children[0]['code'] . '" src="img/spacer.gif" alt=""><h2>' . $children[0]['temp'] . '&deg;</h2><p><strong>Stockholm</strong></p></li>';

$alicante = simplexml_load_file("http://weather.yahooapis.com/forecastrss?w=752101&u=c");
$alicante->registerXpathNamespace('yweather', 'http://xml.weather.yahoo.com/ns/rss/1.0'); 
$children = $alicante->xpath('//channel/item/yweather:condition'); 
echo '<li><img class="c' . $children[0]['code'] . '" src="img/spacer.gif" alt=""><h2>' . $children[0]['temp'] . '&deg;</h2><p><strong>Alicante</strong></p></li>';

$marbella = simplexml_load_file("http://weather.yahooapis.com/forecastrss?w=766537&u=c");
$marbella->registerXpathNamespace('yweather', 'http://xml.weather.yahoo.com/ns/rss/1.0'); 
$children = $marbella->xpath('//channel/item/yweather:condition'); 
echo '<li><img class="c' . $children[0]['code'] . '" src="img/spacer.gif" alt=""><h2>' . $children[0]['temp'] . '&deg;</h2><p><strong>Marbella</strong></p></li>';

$torrevieja = simplexml_load_file("http://weather.yahooapis.com/forecastrss?w=775868&u=c");
$torrevieja->registerXpathNamespace('yweather', 'http://xml.weather.yahoo.com/ns/rss/1.0'); 
$children = $torrevieja->xpath('//channel/item/yweather:condition'); 
echo '<li><img class="c' . $children[0]['code'] . '" src="img/spacer.gif" alt=""><h2>' . $children[0]['temp'] . '&deg;</h2><p><strong>Torrèvieja</strong></p></li>';

是否有更有效的方法来加载这些Feed,可能还在一起?响应时间相当短,但如果有任何方法可以优化,我想知道。

2 个答案:

答案 0 :(得分:2)

这样做相同,但看起来更优雅

<?php

$xml = array('stockholm' => 906057, 'alicante' => 752101, 'marbella' => 766537, 'torrevieja' => 775868);

foreach($xml as $city => $code) {
    $smplxml = simplexml_load_file('http://weather.yahooapis.com/forecastrss?w=' .$code. '&u=c');
    $smplxml->registerXpathNamespace('yweather', 'http://xml.weather.yahoo.com/ns/rss/1.0');
    $children = $smplxml->xpath('//channel/item/yweather:condition'); 
    echo '<li><img class="c' . $children[0]['code'] . '" src="img/spacer.gif" alt=""><h2>' . $children[0]['temp'] . '&deg;</h2><p><strong>' .$city. '</strong></p></li>';
}

?>

(因为我现在支持代理,我无法测试它,抱歉,但这可能会有所帮助)

答案 1 :(得分:0)

好吧,如果你做了类似的事情:

function curlGet( $url ) {
   $ch = curl_init(); 
   curl_setopt($ch, CURLOPT_URL, $url); 
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
   $output = curl_exec($ch); 

   curl_close($ch); 

   return $output;
}

$xml = array('stockholm' => 906057, 'alicante' => 752101, 'marbella' => 766537, 'torrevieja' => 775868);

$buffer = "<rsses>";

foreach($xml as $city => $code)
  $buffer .= curlGet('http://weather.yahooapis.com/forecastrss?w=' .$code. '&u=c');

$buffer .= "</rsses>";

$smplxml = simplexml_load_string($buffer);
$smplxml->registerXpathNamespace('yweather', 'http://xml.weather.yahoo.com/ns/rss/1.0');
$children = $smplxml->xpath('//rss/channel/item/yweather:condition'); 

print_r($children);

可能会工作。您需要确保添加到$ buffer的数据不是垃圾。否则你的整个解析都会失败。

相关问题