0

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'] . '&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>';

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.

1
  • Why not use simplexml_load_string? Commented Nov 4, 2011 at 15:45

2 Answers 2

2

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'] . '&deg;</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)

Sign up to request clarification or add additional context in comments.

2 Comments

cURL supports proxy, just in case.
Thanks. I'm not sure it helped in decreasing the load speed for the actual feeds but using the array really cleaned up the code.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.