views:

646

answers:

1

http://www.facebook.com/feeds/share_posts.php?id=207302593&viewer=207302593&key=d95b2de790&format=rss20

The link above is the URL to my shared posts on Facebook. When I load that URL in to simplexml_load_file() it provides an entire page of errors. Looking at the errors, it looks like Facebook is spitting out HTML instead of the feed. I thought this was an authentication issue but I logged out of facebook and loaded the feed url in to my browser and I was able to see the feed. Here are the errors I'm getting. Any ideas? Thanks!

A PHP Error was encountered

Severity: Warning

Message: simplexml_load_file() [function.simplexml-load-file]: http://www.facebook.com/feeds/share_posts.php?id=207302593&viewer=207302593&key=d95b2de790&format=rss20:12: parser error : AttValue: " or ' expected

Filename: models/Stream.php

Line Number: 55

A PHP Error was encountered

Severity: Warning

Message: simplexml_load_file() [function.simplexml-load-file]: </script><noscript> <meta http-equiv=refresh content="0; URL=?_fb_noscript=1" />

...
+5  A: 

Way to redefine "feed" Facebook. Give this a try, and take a look at the downloaded page

echo file_get_contents('http://www.facebook.com/feeds/share_posts.php?id=207302593&amp;viewer=207302593&amp;key=d95b2de790&amp;format=rss20');

It looks like Facebook is doing User-Agent sniffing, and rejecting any "browser" (in this case PHP is the browser) it doesn't think can handle its pages. That they're doing this on feeds is a little weird, but Zuckerberg works in mysterious ways. (doing it to prevent screen scraping is one thing, but feeds are implicitly meant to be read by machines)

You'll want to set your user-agent string to match a browser that Facebook supports. When I need to do this I user curl to download the file (curl has a ton of options for setting http headers).

If curl isn't your thing you can override PHP's default user-agent with an ini setting

    ini_set('user_agent', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3');
Alan Storm