【问题标题】:simplexml_load_file() [function.simplexml-load-file]: Start tag expected, '<' not found insimplexml_load_file() [function.simplexml-load-file]:需要开始标记,'<' 未在
【发布时间】:2013-09-08 03:22:27
【问题描述】:

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

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

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

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

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

【问题讨论】:

标签: php xml rss


【解决方案1】:

试试这个Curl

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

【讨论】:

  • @Mark Blackham 如果我的回答是正确的,别忘了检查勾号。
  • 我有时会看到与 Mark 相同的错误,并且我已经在使用 Curl 加载我的 xml。我认为问题一定是有时提要格式不正确。我已经通过在我的 simplexml_load_string 前面添加一个 @ 来解决它:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-19
  • 2016-02-20
  • 1970-01-01
  • 2012-08-17
相关资源
最近更新 更多