Read Facebook Feeds In PHP

The following is deprecated by Facebook in favor of their developer API, but this will be kept for historical purposes.

So you want to read a Facebook feed in your PHP application but you’re running into trouble. If you’re like me you probably spent hours pulling your hair out trying to figure out what the issue is when your code looks perfectly fine. If you’re not like me, congratulations, you still have hair to pull out, and this post will hopefully help you keep your hair for many years to come!

The reason, it turns out, that I couldn’t pull in data from a simple XML feed from Facebook in my PHP code is that Facebook doesn’t want its feed being fed to anything that’s not a browser or pre-approved non-browser application. This is unfortunate as you may be required to add it to your application.

Luckily, there is a simple work around! Facebook servers are looking at your request and determining if you’re a server or a computer, so all you should need to do (at the time of this writing anyway) is add this line of code to your PHP application before any content is output to the browser. It will change the user agent request to look like it’s coming from a browser:

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

BOOM roasted!

Leave a Reply

Your email address will not be published. Required fields are marked *