2014-02-19 45 views
0

我已經在php中創建了一個web服務器,它通過我的webApp收到facebook userAccessToken並檢查它是否有效,我想獲取有關該用戶的基本信息,但是我找不到方法這個。有人可以幫助我嗎?PHP。獲取有關Facebook用戶的信息

+0

ty it wi th簡單的js api –

+0

我需要用php服務器端來做。我已經與Js – ShilleR

回答

0
<?php 
     require_once('php-sdk/facebook.php'); 

     $config = array(
     'appId' => 'YOUR_APP_ID', 
     'secret' => 'YOUR_APP_SECRET', 
     'allowSignedRequest' => false // optional but should be set to false for non-canvas apps 
    ); 

     $facebook = new Facebook($config); 
     $user_id = $facebook->getUser(); 
    ?> 

<html> 
    <head></head> 
    <body> 



    <?php 
     if($user_id) { 

      // We have a user ID, so probably a logged in user. 
      // If not, we'll get an exception, which we handle below. 
      try { 

      $user_profile = $facebook->api('/me','GET'); 
      echo "Name: " . $user_profile['name']; 

      } catch(FacebookApiException $e) { 
      // If the user is logged out, you can have a 
      // user ID even though the access token is invalid. 
      // In this case, we'll get an exception, so we'll 
      // just ask the user to login again here. 
      $login_url = $facebook->getLoginUrl(); 
      echo 'Please <a href="' . $login_url . '">login.</a>'; 
      error_log($e->getType()); 
      error_log($e->getMessage()); 
      } 
     } else { 

      // No user, print a link for the user to login 
      $login_url = $facebook->getLoginUrl(); 
      echo 'Please <a href="' . $login_url . '">login.</a>'; 

     } 

     ?> 

    </body> 
</html> 

一旦你在使用記錄此命令檢索用戶數據數組:

$user_profile = $facebook->api('/me','GET'); 

或者你也可以使用這一功能,ID(其它用戶)來獲取用戶

$user_profile = $facebook->api('userID','GET'); 

一切你需要你可以在這裏找到:

https://developers.facebook.com/docs/reference/php/ 
+0

做了我不明白這個代碼。該行$ user_id = $ facebook-> getUser()它返回哪個用戶?你在哪裏做登錄Ø給Facebook的userToken? – ShilleR

+0

$ facebook-> api('/ me','GET');這會得到您擁有的用戶,您的個人Facebook用戶。 –

+0

您已經創建了一個應用程序?這是需要爲了您的腳本登錄到Facebook –

相關問題