【问题标题】:How to get user profile info using access token in php-sdk如何使用 php-sdk 中的访问令牌获取用户个人资料信息
【发布时间】:2014-04-19 14:05:57
【问题描述】:

我想使用我已经生成的访问令牌获取用户配置文件,如果访问令牌无效,它应该显示错误。任何人都可以帮助做到这一点?我想要 php 代码来处理这个。我在等……

【问题讨论】:

  • ?我正在寻求帮助:/
  • 不,您是在公然要求某人向您的方向扔工作代码。请阅读How to Ask

标签: php facebook facebook-graph-api facebook-php-sdk facebook-access-token


【解决方案1】:

如果您想获取 user_email 和其他信息,那么您可以使用此代码....

$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;
  }
  }

【讨论】:

    【解决方案2】:
     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){
    
            }
    
        }
    

    【讨论】:

      【解决方案3】:

      由于您已经拥有 访问令牌,因此最简单且干净的方法是-

      $user_details = "https://graph.facebook.com/me?access_token=" .$access_token;
      
      $response = file_get_contents($user_details);
      $response = json_decode($response);
      print_r($response);
      

      【讨论】:

      • 它的工作!但是如何处理无效令牌?
      • @SamnadSainulabdeen:在提问之前你先自己做一些研究怎么样? google.com/search?q=facebook+app+handle+invalid+token -> developers.facebook.com/blog/post/2011/05/13/…
      • 如果出现错误,您将在响应字符串中获得error 键!如果有帮助,请接受 ans!
      • @SahilMittal 谢谢我稍微修改了代码,现在可以工作了...&lt;?php error_reporting(0); require_once('php-sdk/facebook.php'); $access_token = 'ACCESSTOKEN'; // Attempt to query the graph: $graph_url = "https://graph.facebook.com/me?" . "access_token=" . $access_token; $response = file_get_contents($graph_url); if($response === FALSE) { echo "Invalid Acces Token"; } else { echo "Valid Access Token"; } ?&gt;
      【解决方案4】:

      首先检查用户是否登录

       $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;
        }
        }
      

      如果您的访问令牌不为空,则获取用户数据

       <?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 ?>
      

      用户变量可以从Facebook Graph API获取

      【讨论】:

      • 无论如何都可以在没有 appid 和 appsecret 的情况下获取用户信息(我已经使用应用程序生成了访问令牌)
      猜你喜欢
      • 2021-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多