11 December 2013

How to decode JSON using PHP5 for Facebook application

json facebook
The new Graph API helps to read and write data to Facebook. Every object in the social graph has a unique ID. You can fetch the data associated with an object by fetching https://graph.facebook.com/ID. For example, the official page for the Facebook Platform has id 19292868552, so you can fetch the object at https://graph.facebook.com/19292868552 All responses are JSON objects. Here is the code to decode JSON OBJECT / to get data from the graph api link . 

Code Sample : //FETCHING THE JSON FROM THE URL

$jsonurl = "https://graph.facebook.com/". $user_id; $json = file_get_contents($jsonurl,0,null,null); $json_output = json_decode($json);

// THIS PRINTS THE WHOLE STRING OF JSON

var_dump( json_decode( $json,true)) ;

// ITS SAME AS YOU ARE USING it as an object

$user_firstName = $json_output->first_name;

//PRINTS THE FIRST NAME OF THE LOGGED IN USER

echo $user_firstName ;

You can also directly use the graph api for showing the profile picture. For example :
<img src="https://graph.facebook.com/<?= $user ?>/picture"/>

Link :

  Graph API Facebook Documentation

(If you found this article useful then share with your friends.)

No comments: