PHP Developer

My notes and thoughts about Linux, WordPress, PHP, WPML, Toolset and many more.

Facebook Comments API using PHP and JSON: With Demo Example

This is a short tutorial illustrating a script that can pull out Facebook comments through their API graph using PHP and JSON. If you are developer, this can be a valuable information to you.

NOTE: If you want the complete script example right away, scroll down below FINAL STEP now.

STEP1- Define the URL

It is already assumed that the domain you are going to retrieve and parse Facebook comments is already implementing Facebook comments for some time and there are lots of comments to retrieve. The url can be define in a variable such as shown below. And bear in mind that it will only grab comments specific to this URL.

STEP2- Formulate the Facebook graph API request URL

The next thing is to use the API URL for retrieving Facebook comments. You will be passing the URL of your domain (where you would like to retrieve the Facebook comments) as the query to this request URL, such as shown below:

STEP3- Retrieve comments using file_get_contents

Now you have the request URL, you can finally retrieve the Facebook comments data from their API using file_get_contents:

If you dumped the output of $requests using var_dump, the data is encoded in JSON format. This is harder to manipulate in your PHP scripts and this data format needs to be converted into a PHP array in STEP4.

STEP4- Convert JSON data format to PHP array

To convert the JSON data format to PHP array, you need to use json_decode function set to TRUE (this is required):

If you don’t include “TRUE”, then you will be able to work it out the data as a PHP array. Try dumping the output of $comments and it should now be in array format.

FINAL STEP- Loop through the array to retrieve contents

This is the most important part of the script. It depends on what information you need from the graph API. Supposing you want to retrieve the main comments author, contents and date (including the replies).

Below are the final working PHP script (including steps 1 to 5) that you can use:

I’m not going to explain each of the multidimensional arrays that are being accessed by the script, I assume you already understand this basic concept. It’s up to you analyze each lines. The technique to easily understand the above script is to dump the $comments array variable output in the browser (after JSON decoding):

And then analyze how the above lines relates to the multidimensional array structure.

Applications of this Script

There are lots of applications in the above script. I can cite some examples:

1.) You will need to transfer/migrate the entire Facebook comments of a certain website to its own database structure using MySQL for example.

The above script parse the complete comments of a single URL only, to parse the entire comments in the entire domain URLs, you need a list of the URLs then create a loop.

2.) You want a more readable presentation of the Facebook comments in your site (since the default presentation is using an iframe, etc).

3.) You want a method to backup the Facebook comments or access it through a PHP script.

Featured image credits: Linux Screenshots: https://www.flickr.com/photos/xmodulo/

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">