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

99封情书 提交于 2019-12-11 04:17:22

问题


I am using simplexml_load_file to load the BBC Weather RSS feed and it randomly gives the following error:

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

It seems to fail randomly. My code is not dynamically changing so I can't figure out why it fails only sometimes.

If I grab the rss file that "supposedly" has the missing < tag and store it on my computer and point the simplexml_load_file to that location it works fine.

Any advice most appreciated as this minor issue is driving me mad.


回答1:


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


来源:https://stackoverflow.com/questions/18616890/simplexml-load-file-function-simplexml-load-file-start-tag-expected-no

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!