I have the following code to read in an XML file:
$xml2 = simplexml_load_file('http://www.facebook.com/feeds/page.php?format=rss20&id=334704593230758');
$item = $xml2->channel->item;
I am getting the following back in my source code:
<b>Warning</b>: simplexml_load_file() [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: http://www.facebook.com/feeds/page.php?format=rss20&id=334704593230758:11: parser error : xmlParseEntityRef: no name in <b>/home/content/49/8644249/html/test/_inc/footer.php</b> on line <b>110</b><br />
it continues on like that for 10 more lines. Is there a problem with the xml code?
OK, slightly weirdly since this is an RSS feed and not designed to be directly human-readable, the answer to this is that you have to include a
User-Agent:header in your request.When I load the URL in Chrome a get valid XML document, when I run you code I get the same errors you do. Upon closer inspection I find that when I run your code I actually get a minimal HTML document back, not the desired XML – in order to get the right result you have to pass a valid user agent string, which means you can’t use
simplexml_load_file()because it doesn’t support stream contexts.This code works for me: