2013-05-16 67 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(); 
相关问题