PHP:使用 simple_xml_load_file 加载多个 XML feed

发布于 2024-12-13 11:25:33 字数 2053 浏览 0 评论 0原文

我正在通过雅虎的天气 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>';

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

I'm retrieving multiple weather forecasts through Yahoo's weather 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'] . '°</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'] . '°</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'] . '°</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'] . '°</h2><p><strong>Torrèvieja</strong></p></li>';

Is there a more effective way to load these feeds, possibly together? The response time is fairly minimal but if there's any way this could be optimized I'd like to know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱格式化 2024-12-20 11:25:33

这做同样的事情,但看起来更优雅

<?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'] . '°</h2><p><strong>' .$city. '</strong></p></li>';
}

?>

(因为我现在在代理后面,我无法测试它,抱歉,但它可能有帮助)

This does the same, but looks a bit more elegeant

<?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'] . '°</h2><p><strong>' .$city. '</strong></p></li>';
}

?>

(since I'm behind a proxy right now, I wasn't able to test it, sorry, but it may helps)

胡大本事 2024-12-20 11:25:33

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

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 的数据不是垃圾。否则你的整个解析将会失败。

Well if you did something like:

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);

Might work. You need to make sure that the data you add to $buffer is not junk though. Or otherwise your entire parsing will fail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文