2

我使用 Facebook API,我希望检索已连接用户的 Facebook 信息。我可以在 javascript 中检索信息,但我想获取信息以填充存储在我的数据库中的 PHP。

这是我在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 上创建应用程序的步骤吗?

4

4 回答 4

1

尝试

$user_profile = $facebook->api('/me');
print_r($user_profile)
于 2013-05-16T19:53:39.667 回答
0

这是一个获取用户数据的快速脚本

<?php     
try {
    // Get UID of the user
    $uid = $this->fb->getUser();

    // Get basic info about the user
    $me = $this->fb->api('/me');

    // Get the user's facebook stream
    $feed = $this->fb->api('/me/home');

    // Obtain user's and his/her friend's basic information via FQL Multiquery
    $streamQuery = <<<STREAMQUERY
{
"basicinfo": "SELECT uid,name,pic_square FROM user WHERE uid=me()",
"friendsinfo" : "SELECT uid, name, pic_square FROM user WHERE uid = me() OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me())"
}
STREAMQUERY;
    $streamParams = array(
                          'method' => 'fql.multiquery',
                          'queries' => $streamQuery
                   );
    $streamResult = $this->fb->api($streamParams);

    //Obtain user likes, interests, movies, music, books
    $likes = $this->fb->api('/me/likes');
    $interests = $this->fb->api('/me/interests');
    $movies = $this->fb->api('/me/movies');
    $music = $this->fb->api('/me/music');
    $books = $this->fb->api('/me/books');
}catch(FacebookApiException $e) {
    error_log($e);
    //Session expired or user de-authenticated the app
    $this->showConnectToFB(true);
}

?>
于 2013-05-16T07:29:58.380 回答
0

我现在在一个旧线程上回答,但我遇到了同样的问题。我已经解决了这个问题,用我的访问令牌创建了一个 $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 不会做任何事情。这也解决了我的问题。

于 2014-08-27T15:08:00.853 回答
0

这里

$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();
于 2017-04-02T23:22:57.027 回答