PHP:使用 simple_xml_load_file 加载多个 XML feed
我正在通过雅虎的天气 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>';
是否有更有效的方法来加载这些提要(可能一起加载)?响应时间相当短,但如果有任何方法可以优化,我想知道。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这做同样的事情,但看起来更优雅
(因为我现在在代理后面,我无法测试它,抱歉,但它可能有帮助)
This does the same, but looks a bit more elegeant
(since I'm behind a proxy right now, I wasn't able to test it, sorry, but it may helps)
好吧,如果你做了类似的事情:
可能会起作用。不过,您需要确保添加到 $buffer 的数据不是垃圾。否则你的整个解析将会失败。
Well if you did something like:
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.