1

我正在使用simplexml_load_file加载 BBC Weather RSS 提要,它随机给出以下错误:

Warning: simplexml_load_file() [function.simplexml-load-file]:  :1: parser error : Start tag expected, '<' not found in

它似乎随机失败。我的代码不是动态变化的,所以我不知道为什么它有时会失败。

如果我抓取“据说”缺少<标签的 rss 文件并将其存储在我的计算机上并将其指向该simplexml_load_file位置,它工作正常。

任何最受赞赏的建议,因为这个小问题让我发疯。

4

1 回答 1

0

试试这个卷发

<?php
$k = 'http://open.live.bbc.co.uk/weather/feeds/en/2656173/3dayforecast.rss';
$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $k);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $rss = curl_exec($ch);
    curl_close($ch);

    $xml = simplexml_load_string($rss, 'SimpleXMLElement', LIBXML_NOCDATA);
    echo "<pre>";
    print_r($xml);
    echo "</pre>";

    // if you want all items

    //$xml->channel->item item is a array

    //So 

    foreach($$xml->channel->item as $item){
    echo $item->title;  // you can get all results here
    }
?>
于 2013-09-05T08:11:25.377 回答