0

I'm trying to load this page https://developers.facebook.com/blog/feed in my site with no luck. I'm using this code

<?php
$xml = simplexml_load_file('https://developers.facebook.com/blog/feed/');
   print_r($xml);

?>

but i get many line of error like this

Warning: simplexml_load_file() [function.simplexml-load-file]: https://developers.facebook.com/blog/feed/:10: parser error : xmlParseEntityRef: no name in /fb_feed/fb_feed.php on line 2

Thanks to all who help me

Marco Caltagirone
  • 1,206
  • 1
  • 14
  • 23
  • 1
    Possible dupe of [this question](http://stackoverflow.com/questions/10276656/php-errors-parsing-xml-rss-feed) – Sepster Sep 06 '12 at 10:11

3 Answers3

2

I think this is a problem with the XML feed itself.

See this article.

Load the string with file_get_contents, and do a str_replace on the amperand to

&amp;

So leaving you with

$xml = simplexml_load_string(str_replace('&','&amp;',file_get_contents('https://developers.facebook.com/blog/feed/')));

EDIT:

Just seen in the comments, this has been tackled before and the str_replace can be improved from my original to

$xml = simplexml_load_string(str_replace(array("&amp;", "&"), array("&", "&amp;"),file_get_contents('https://developers.facebook.com/blog/feed/')));

This avoids converting already correctly encoded ampersands.

EDIT 2 :

Facebook redirects requests from file_get_contents to a browser select page. So we need to 'trick' it into thinking we're using a regular browser.

$url='https://developers.facebook.com/blog/feed/';
$crl = curl_init();
$timeout = 5;
curl_setopt ($crl, CURLOPT_URL,$url);
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($crl,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$ret = curl_exec($crl);
curl_close($crl);
$xml = simplexml_load_string(str_replace(array("&amp;", "&"),array("&", "&amp;"),$ret));     
var_dump($xml);

The first answer should work in most cases, but edit 2 is for the Facebook Dev blog, or any other that redirects based on the user-agent header.

Mark
  • 3,005
  • 1
  • 21
  • 30
  • youre str_replace did indeed fix the problem to load the XML! – Jurgo Sep 06 '12 at 10:20
  • now i have no error but i get the content of facebook main page and not the xml of https://developers.facebook.com/blog/feed/ – Marco Caltagirone Sep 06 '12 at 10:23
  • so you get as the result the content of facebook developers blog? it's very strange i copy exactly what you wrote but i get an xml of facebook login page – Marco Caltagirone Sep 06 '12 at 10:32
  • 1
    Looks like Facebook is redirecting the request, let me see if I can find a way around. – Mark Sep 06 '12 at 10:37
  • thanks for your help. This site is able to take the contents http://grabpage.info/h/developers.facebook.com/blog/feed – Marco Caltagirone Sep 06 '12 at 10:54
  • with the code of edit 2 i get this error Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /fb_feed/fb_feed.php:19 Stack trace: #0 /fb_feed/fb_feed.php(19): SimpleXMLElement->__construct('') #1 {main} thrown in /fb_feed/fb_feed.php on line 19 – Marco Caltagirone Sep 06 '12 at 14:13
  • 1
    Hi Marco, I've edited the answer load the XML with the str_replace we used previously. The error looks like it's caused by invalid XML, so hopefully that should fix it (I've tested and it works ok for me). – Mark Sep 07 '12 at 08:53
  • Thanks again but now i get this result "bool(false)" – Marco Caltagirone Sep 07 '12 at 10:00
  • cURL support enabled cURL Information libcurl/7.12.0 OpenSSL/0.9.7d zlib/1.2.1.1 – Marco Caltagirone Sep 07 '12 at 10:21
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16409/discussion-between-mark-and-marco-caltagirone) – Mark Sep 07 '12 at 10:22
0

it could be the case that you need to encode the url as this page suggests

simplexml_load_file(rawurlencode('https://developers.facebook.com/blog/feed/'))

if that doesnt work you can try to load the file by file_get_contents and pass the returning value to the xml parser:

simplexml_load_string( file_get_contents('https://developers.facebook.com/blog/feed/') );
Ties
  • 5,726
  • 3
  • 28
  • 37
0
<?php
$url = "https://developers.facebook.com/blog/feed/";
$xml = str_replace('&','&amp;', file_get_contents($url));
$xml = simplexml_load_string($xml);
print_r($xml);
?>
Jurgo
  • 907
  • 4
  • 18