PHP: загрузка нескольких XML-каналов с помощью simple_xml_load_file - PullRequest
0 голосов
/ 04 ноября 2011

Я получаю несколько прогнозов погоды через API погоды Yahoo -

$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>';

Существует ли более эффективный способ загрузки этих каналов, возможно, вместе?Время отклика довольно минимальное, но если есть способ оптимизировать его, я хотел бы знать.

Ответы [ 2 ]

2 голосов
/ 04 ноября 2011

Это то же самое, но выглядит более элегантно

<?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>';
}

?>

(поскольку я сейчас нахожусь за прокси-сервером, я не смог его проверить, извините, но это может помочь)

0 голосов
/ 04 ноября 2011

Хорошо, если вы сделали что-то вроде:

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, не являются ненужными. В противном случае весь ваш разбор не удастся.

...