Thursday, 9 August 2018

Retrieve A Facebook Page Feed With PHP

This is a short tutorial on how to retrieve a Facebook page’s post feed using PHP and Facebook’s Graph API. Over the past year, many people have been forced to switch to this method, simply because the RSS feed for Facebook pages was discontinued.
First of all, you will need to setup a Facebook App using the Facebook For Developers panel. Don’t worry about all of the different settings that are available. To get a list of posts on a Facebook page, you will only need an App ID and an App Secret. Once you have an App ID and an App Secret, you can re-use those credentials for some of the various API calls.
Have a look at the following piece of code:
If you run the PHP code above, using your App credentials, you’ll see that this API call returns a list of posts from a given Facebook page. In this example, I have decoded the JSON result into a PHP associative array.
Here is a screenshot showing the posts from Reddit’s Facebook page:
As you can see, the posts are contained inside an element called “data”. Each post has three elements:
  • message: A basic description of the post in question. Sometimes, it is what the admin has written. On other occasions, you’ll find that it is a message about how the page has updated its cover photo, etc. To be honest, this field isn’t that useful. To get more information about a specific post, you will need to query the Graph API for more details.
  • created_time: The date and time that the post went live.
  • id: This is actually the ID of the Facebook page AND the ID of the post in question, separated by an underscore. The ID to the left of the underscore is the unique ID of the Facebook page. The ID to the right of the underscore is the unique ID of the post. Keep this in mind if you intend on storing the IDs or embedding posts.

0 comments:

Post a Comment