问题
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