-2

I am trying to Access users friends information using access token.

So i create a facebook object.

$facebook = new Facebook(array(
            'appId' => APP_ID,
            'secret' => APP_SECRET,
            ));

I then set the access token to that object.

$facebook->setAccessToken(TOKEN);

Then when i try to retrieve the user information it returns 0.

$user = $facebook->getUser();

So the code below does not get executed.

if ($user) 
{
    try 
    {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
    } 
    catch (FacebookApiException $e) 
    {
        error_log($e);
        $user = null;
    }
}

Can anyone tell me where my mistake lies.

user1584103
  • 193
  • 2
  • 15
  • 4
    You probably want to change your secret after this question – zerkms Aug 27 '13 at 09:30
  • possible duplicate of [Why is Facebook PHP SDK getUser always returning 0?](http://stackoverflow.com/questions/6790272/why-is-facebook-php-sdk-getuser-always-returning-0) – Sahil Mittal Aug 27 '13 at 09:41
  • and thousands others.. – Sahil Mittal Aug 27 '13 at 09:41
  • @Shadowfax The problem is that i am using the facebook graph php sdk only and they have not set access token ... – user1584103 Aug 27 '13 at 09:48
  • I need to retrieve the user information and his friend list based on access token only – user1584103 Aug 27 '13 at 09:50
  • You have to show the authentication/login code- that's how you are getting the access toekn right? – Sahil Mittal Aug 27 '13 at 09:58
  • The authentication is done on mobile device which then sends the access token to server. – user1584103 Aug 27 '13 at 10:02
  • You don’t need a user id being set (/automatically determined) internally for this – if you have a valid user access token, then the API will figure out itself, who `/me` is supposed to be. So just remove your `if ($user)` check around the API call. – CBroe Sep 11 '13 at 07:48

1 Answers1

1

You can use curl,

eg.

        $params = array('access_token' => self::accessToken);
        $url = "https://graph.facebook.com/me/friends";
        $url .= '?' . http_build_query($params);


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($ch, CURLOPT_USERAGENT ,'');
    $output = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
   $output = json_decode($output);

This should give you the list of friends of the person whose access token you use.

Shrujan Shetty
  • 2,298
  • 3
  • 27
  • 44