I want to get user profile using access token that i have already generated, and if the access token is invalid it shoud display error. anyone can help to do this ? i want php code to handle this. i am waiting....

有帮助吗?

解决方案

Since you already have the access token, the simplest and clean way would be-

$user_details = "https://graph.facebook.com/me?access_token=" .$access_token;

$response = file_get_contents($user_details);
$response = json_decode($response);
print_r($response);

其他提示

First check if the user is logged in

 $facebook = new Facebook(array(
 'appId'  => 'AppIDHere',
 'secret' => 'AppSecretHere',
 ));

 // See if there is a user from a cookie
 $user = $facebook->getUser();

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

If your access token is not null, fetch user data

 <?php if ($user): ?>

 Profile Picture: <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
 User Id: <?php echo $user_profile['id'];?>
 Name: <?php echo $user_profile['name'];?>
 First Name: <?php echo $user_profile['first_name'];?>
 Last Name: <?php echo $user_profile['last_name'];?>
 Link: <?php echo $user_profile['link'];?>
 Gender: <?php echo $user_profile['gender'];?>
 Username: <?php echo $user_profile['username'];?>

 <?php else: ?>
 <strong>You are not Logged In.</strong>
 <?php endif ?>

The user variables can be got from Facebook Graph API

If You want to get user_email and other info then you can use this code....

$facebook = new Facebook(array(
 'appId'  => 'AppIDHere',
 'secret' => 'AppSecretHere',
 ));

 // See if there is a user from a cookie
 $user = $facebook->getUser();

if ($user) {
 try {
// Proceed knowing you have a logged in user who's authenticated.
  $user_profile = $facebook->api('/me?fields=email,name,id,gender');
     } catch (FacebookApiException $e) {
  echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
  $user = null;
  }
  }
 I used facebook graph Api 
 $facebook = new Facebook(array('appId'  => $fbconfig['appid'],'secret' =>  
 $fbconfig['secret'],'cookie' => true,));
  $session = $facebook->getUser();
   if($session)
    {
      try{
      $uid = $session;      
                $fbme = $facebook->api('/me');      
                $access_token = $facebook->getAccessToken();

                // FQL : to fetch the email id 
                $fql = "SELECT email FROM user where uid='".$uid."'";  
                $response = $facebook->api(array('method' => 
                'fql.query','query' =>$fql,));  
                $user_email = $response[0][email];

                // Friend COunt.
                $friend_res = $facebook->api('/me/friends');
                $friend_count = count($friend_res[data]);

                //likes
                $likes = $facebook->api('/me/likes');
                $fb_like = $likes[data];

                //  Getting more user personal data 
                $param  =   array(
                        'method'  => 'users.getinfo',
                        'uids'    => $uid,
                        'fields'  => 'name,sex,pic,current_location,profile_url,email,birthday',
                        'callback'=> ''
                    );
                $userInfo   =   $facebook->api($param); 
           }catch(FacebookApiException $e){

        }

    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top