2013-05-16 122 views
2

我使用Facebook API,並希望檢索連接用戶的Facebook信息。我可以在javascript中檢索信息,但我想要獲取信息以填充存儲在我的數據庫中的PHP。如何使用Facebook PHP API獲取用戶信息

這是我在javascript函數代碼:

<html> 
    <head> 
     <title>My Application</title> 
     <style type="text/css"> 
      div { padding: 10px; } 
     </style> 
     <meta charset="UTF-8"> 
    </head> 
    <body> 
     <div id="fb-root"></div> 
     <script type="text/javascript"> 
      var fbAppId = 'myAppId'; 
      var objectToLike = 'http://techcrunch.com/2013/02/06/facebook-launches-developers-live-video-channel-to-keep-its-developer-ecosystem-up-to-date/'; 

      if (fbAppId === 'replace me') { 
      alert('Please set the fbAppId in the sample.'); 
      } 

      window.fbAsyncInit = function() { 
      FB.init({ 
       appId  : fbAppId,  // App ID 
       status  : true,   // check login status 
       cookie  : true,   // enable cookies to allow the server to access the session 
       xfbml  : true   // parse page for xfbml or html5 social plugins like login button below 
      }); 

      FB.login(function(response) { 
       if (response.authResponse) { 
       FB.api('/me', function(response) { 
        window.alert(response.last_name + ', ' + response.first_name + ", " + response.email); 
       }); 
       } 
      }); 
      }; 

      (function(d, s, id){ 
      var js, fjs = d.getElementsByTagName(s)[0]; 
      if (d.getElementById(id)) {return;} 
      js = d.createElement(s); js.id = id; 
      js.src = "//connect.facebook.net/en_US/all.js"; 
      fjs.parentNode.insertBefore(js, fjs); 
      }(document, 'script', 'facebook-jssdk')); 

     </script> 
</body> 
</html> 

這是我的PHP代碼,我不能讓它工作

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

$config = array(); 
$config['appId'] = 'myAppId'; 
$config['secret'] = 'myCodeSecret'; 
$config['fileUpload'] = false; // optional 

$facebook = new Facebook($config); 

$user = $facebook->getUser(); 

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

如果我顯示變量$user,我讓我的用戶ID。但是我無法獲得其他信息。

我看起來更詳細,這可能是在Facebook的應用程序的配置問題。你能否解釋在Facebook上創建應用程序的步驟?

+1

嘗試的var_dump($ user_profile);讓我們看看什麼是響應 – Fabio

+0

這會返回NULL NULL – Florent

+0

這意味着您的調用失敗,代碼或參數可能有問題,您是否正確設置了登錄信息? – Fabio

回答

0

這裏是一個快速腳本,獲取用戶數據

​​
+0

這仍然不起作用。 變量聲明存在問題$ streamQuery – Florent

+0

這應該很好。我的猜測是你或你的文本編輯器在$ streamQuery變量的內容之前應用了製表符或空格,這就是破壞它的原因。他使用heredoc符號來聲明字符串,並且在heredoc標記結束標記(在本例中爲'STREAMQUERY;')之前不能有任何空格。 –

1

嘗試

$user_profile = $facebook->api('/me'); 
print_r($user_profile) 
+0

這仍然不起作用。 – Florent

0

現在我回答關於一個古老的線程,但我遇到同樣的問題。我已經解決了這個問題,用我的acces令牌創建一個$ params數組。

所以要做的事情是這樣的。

$config = array(); 
$config['appId'] = $appid; 
$config['secret'] = $appSecret; 
$config['fileUpload'] = false; // optional 
$fb = new Facebook($config); 

$params = array("access_token" => "acces_token_given_by_facebook"); 
$object = $fb->api('/me', 'GET', $params); 

print_r($object); 

當您將$ params添加到您的獲取請求時,它將起作用。 Facebook將不會做任何事情,直到您已經發布了訪問令牌。這也解決了我的問題。

0

here

$fb = new Facebook\Facebook([ 
    'app_id' => '{app-id}', 
    'app_secret' => '{app-secret}', 
    'default_graph_version' => 'v2.2', 
    ]); 

try { 
    // Returns a `Facebook\FacebookResponse` object 
    $response = $fb->get('/me?fields=id,name', '{access-token}'); 
} catch(Facebook\Exceptions\FacebookResponseException $e) { 
    echo 'Graph returned an error: ' . $e->getMessage(); 
    exit; 
} catch(Facebook\Exceptions\FacebookSDKException $e) { 
    echo 'Facebook SDK returned an error: ' . $e->getMessage(); 
    exit; 
} 

$user = $response->getGraphUser(); 

echo 'Name: ' . $user['name']; 
// OR 
// echo 'Name: ' . $user->getName(); 
相關問題