Parsing Facebook Graph JSON Objects in PHP

There are a ton of tutorials available to parse JSON objects, to make Facebook Graph API calls, and to display JSON array objects. Yet, there’s always some mistake we tend to overlook, and if we solve one, another one pops up. This blog is to list out a few errors that I’ve come across, and how I solved them.

I followed the tutorial here , and it worked flawlessly when I wanted only the name, and ID of the person who gives permission. It’s basically a tutorial to allow Facebook Login, and display the details. As of November 2015, it’s highly advisable that you use PHP SDK v4, since that’s the latest version. In order to use the Facebook Graph API, you need to create either a canvas app, or website, like the one in the post. Instructions are clearly given, so all you need to do is just follow it verbatim. In this blog, I’m going to explain my journey in parsing Facebook Graph JSON objects after successfully following the above tutorial.

Problem Statement: To use Facebook Graph API to retrieve and display User Likes.

Step 1) Obtain User Permissions.

In the fbconfig file, here’s what’s given in the last few lines:

else {
$loginUrl = $helper->getLoginUrl();
header(“Location: “.$loginUrl);

}
But what do we actually need? We need external permissions. For those who are not aware, user likes is private. You need to get an access token to retrieve them. But how do we ask for permissions to retrieve the access token? We send an array of required permissions to the login helper, like this:

else {
$permissions=[‘user_friends’,’user_likes’];
$loginUrl = $helper->getLoginUrl($permissions);
header(“Location: “.$loginUrl);
}

The mistakes I made here were basically adding the permissions along with the url in the helper function. It didn’t give any error, but I didn’t get the right output.

Note: The email parameter wouldn’t have been displayed in the tutorial post’s code. Hence it’s important you specify that parameter in your API request.

$request = new FacebookRequest(
$session,
‘GET’,
‘/me’,
array(
‘fields’ => ‘likes,id,email’
)
);

This is the code you’ll get when you run it in the Facebook Graph Explorer. Do check it out and play with it. It’s really cool. Another point you should note is that the name in the permissions is different from the name of the parameter in API request.

Check out the parameter names in the API request here, and the names of parameters in the user permissions can be found when you click “Get Access Token” in the Graph Explorer.

Step 2) Requesting the API

Okay, so now you’ve obtained the permissions. How do you retrieve the data? By requesting.

$request = new FacebookRequest(
$session,
‘GET’,
‘/me’,
array(
‘fields’ => ‘likes,id,email’
)
);

You now need to execute it, and get the result in the form of a Graph Object.

$response = $request->execute();
$graphObject = $response->getGraphObject();

Step 3) Object vs Array

Currently the $graphObject is the Facebook Graph Object. Just dump the contents of the $graphObject, and this is what you’ll get:

92

You get PHP Incomplete Class error. The most annoying, seriously. Took me a while to solve it. It’s a problem with the session variable. Some people say your “session_start()” must happen after all the includes and requires. While others say it’s a serializing problem. Now serialize the graph object and dump it’s contents again.

$_SESSION[‘TEST’]=serialize($graphObject);

93

It says string(4239), which means the serialized object is now a string. After what seemed like a very long time, I realized I had to convert it to an array. There are two ways to do so.

First way:

$request = new FacebookRequest(
$session,
‘GET’,
‘/me’,
array(
‘fields’ => ‘likes,id,email’
)
);

$response = $request->execute();
$graphObject = $response->getGraphObject()->asArray();

Second way:

$graphObject = (new FacebookRequest(
$session,
‘GET’,
‘/me’,
array(
‘fields’ => ‘likes,id,email’
)))->execute()->getGraphObject()->asArray();

Now dump your graph object and this is what you should get:

94

Important Note: If you have to use 

$fbid=$graphObject->getProperty(‘id’);

 you’ll get an error if you convert your graphObject to array, because getProperty is a property of object only. In such cases, just store 

$graphObject->asArray(); 

to a separate variable, and do computations on that variable. Now, you’ll get this output:

95

What is an stdclass object? As quoted in StackOverflow, it means:

"stdClass is just a generic ’empty’ class that’s used when casting other types to objects. “

Step 4) Printing an Array of stdClass objects

I did this, and I got the output “Array”:  echo $user[‘likes’]->data; Which means that the value is “Array” and cannot be converted to string.

Next, I did this:

echo $user[‘likes’]->data[‘id’] and got no output.

Finally I did this:

print_r( $user[‘likes’]->data[‘0’]);

And here’s my output:

This means that the current output is again an object. So we need to parse it once more:

print_r( $user[‘likes’]->data[‘0’]->name);

And you finally get Brigge.

Break down:

$user is an array

likes is an object

data is an array

‘0’ is an object

Do you see a pattern? Facebook Graph Objects are cascaded in an alternate form. Hence it’s extremely vital you properly use square brackets and pointers.

Use a FOREACH loop to get the entire array:

foreach($user[‘likes’]->data as $key)
echo $key->name . “<br>”;

96

This is the end of my journey in parsing single array of objects. In the next tutorial, I will show you how to parse these objects and write into a file in a compact manner. Hope this tutorial helped, and I’ve shared all the errors I’ve faced. Please let me know if these solutions helped you solve your problems as well!

Advertisements

3 thoughts on “Parsing Facebook Graph JSON Objects in PHP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s