Published on Oct 15, 2010

Get User Information, Status & Shares through Facebook Graph API

Getting User Information, Status & Shares through Graph API

Getting User Information, Status & Shares through Graph API

We’ve already seen in my previous post how to Find Users through Name or Email using Facebook Graph API. The steps 1-3 of that post has shown how we can get the access_token for the API requests to work properly. We’ve also shown how to search for users with their name or email address in facebook. In the search results we got the user ids which we can use to get further detail information about each user. We can get each user’s public information, their recent status messages and even the links they share, stories they like and much more.

To do this we assume that you already know how to get the access_token from the Graph API. If not then here is the link to my previous post which discusses about this issue. Follow step 1-3 to understand how to get the access_token.

Here is the full code segment for getting the access_token and then the user information (we assume that the user_id is sent through the get parameter named ‘id’):

function callFb($url)
{
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true
));

$result = curl_exec($ch);
curl_close($ch);
return $result;
}

$url = "https://graph.facebook.com/oauth/access_token?client_id=...&redirect_uri=...&client_secret=...&code=...";
$access_token = callFb($url);
$access_token = substr($access_token, strpos($access_token, "=")+1, strlen($access_token));

$url = "https://graph.facebook.com/".$_GET['id']."?access_token=$access_token&fields=id,name,gender,birthday,email,website,hometown,location,timezone,religion,political,relationship_status,interested_in,meeting_for,bio,quotes,about,link";
$ret_json = callFb($url);

$user = json_decode($ret_json, true);

print_r($user);

$url = "https://graph.facebook.com/".$_GET['id']."/feed?access_token=$access_token";
$ret_json = callFb($url);

$user_feed = json_decode($ret_json, true);

print_r($user_feed);

After we get the access token we query the Graph API with the access_token and we mention the fields we need about the user. Keep in mind that those fields that the user didn’t make public in his/her privacy settings will not be available in the resultant json after the API call.

To access user’s wall to get his/her status, shares etc. we can get the user’s feed in a similar fashion as shown in the code segment.

Author: Masum
Tags: , , , , ,

4 Comments + Add Comment

  • John

    August 13, 2011 at 8:57 pm

    This does not quite work for me…

    I can’t get the user’s extended information, just the basics.
    I know that for example the relationship status of the user I’m querying is public, so I think it should be returned in the query, but it is not.

    Have other people, been able to get extend (public) information of user with this method or does this only works for the user that gave the authorization….?

    • Masum

      September 5, 2011 at 11:36 am

      Well, You can only get extended information if the user has privacy settings for those data set to public or has authenticated your application. But some data like friends of the user, birthday, relationship info will only be available if the user authenticated your application.

  • Denaitre Roux

    August 15, 2011 at 12:36 am

    It doesn’t work for me neither. I can only have the basic information as well, even for people who broadcast their information.
    Is there a reason Mahfuzur ?

    • Masum

      September 5, 2011 at 11:35 am

      Well, You can only get extended information if the user has privacy settings for those data set to public or has authenticated your application. But some data like friends of the user, birthday, relationship info will only be available if the user authenticated your application.

Leave a comment

Sponsored