If you know what type of data you need, it is fairly straightforward to receive it from the Facebook API. However, another hurdle presents itself when you need to parse this data to find the fields you are looking for. The simplest way to see your data before parsing is to go to the Graph API examples on Facebook. At this link, you can see all the data fields Facebook Graph API offers.
For example, let’s say you will get the ‘profile’ of a user. Go to the bottom of the page, and click on user. You will be directed to a new page, and there you can see all the fields about user. Some examples are id, first_name, last_name, link and about.
Facebook API documentation is one of the poorest you will ever find, but they have some good parts too. For example, in every API page you will find a link that shows you what an API call returns. On top of the page, click on
https://graph.facebook.com/me (current user). You will see the JSON response to the ‘me’ API call.
Take some time to look at the results. You can find the field names by just looking at the results.
A good visualization would not hurt either. Chris Nielsen provides a wonderful tool to visualize results here.
Also, when you click on fields, this JSON tool shows you how you can reach that field in JSON text. For example, name of the current location, Varese, Italy can be reached at
Now, when we make the API call and get the result, we need to parse the JSON in PHP and use the data. We can do that very easily:
$attachment = array('access_token' => $access_token); #surprisingly, the result will not be a data object, it will be a single nameless object #all other facebook results are data objects, they can be accessed via $result['data']['yourField'] . $result=$facebook->api('/me', 'GET', $attachment); $location = $result['location']; #you can as well get the name of the location $locationName=$location['name'];
And if you want to write both the location name and location id to your database, you should JSON encode the location to convert the location object to a string , and use addslashes to escape it.
addslashes takes care of single and double quotes.