0

这是我的代码:

<html>
    <head>
    <body>
        <?php
        require_once 'facebook-php-sdk/src/facebook.php';

        $appapikey = '453976824647366';
        $appsecret = 'fe86f3b0b34b3ed6eb39bbce717b8062';

        $facebook = new Facebook($appapikey, $appsecret);

        $user_id = $facebook->getUser();
        echo $user_id;



        echo "<p>Hello <fb:name uid=\"$user_id\" useyou=\"false\" linked=\"false\" firstnameonly=\"true\"></fb:name>";
        ?>

    </body>

</head>
</html>

一些很简单的事情,但我无法找到为什么 getUser 总是返回 0 我会很感激你的帮助!

4

3 回答 3

0

用户必须先登录。

用户登录(使用 Facebook 登录您的网站)后,将显示他的 ID。

于 2012-09-04T14:43:38.173 回答
0

我相信 a) 构造函数需要一个包含应用程序 ID 和密钥的数组,并且 b) 您需要先让用户登录。

如果您在 GitHub 上查看 Facebook PHP SDK 的页面,您会在调用后注意到$facebook->getUser();他们有一个关于进行 API 调用的部分,以及另一个关于生成登录或注销 URL 的部分,具体取决于用户是否登录或不是。

流程是这样的:

  1. 检查会话中是否有用户
  2. 尝试获取用户详细信息
  3. 如果没有用户,则重定向到登录 URL

当用户回到您的应用程序时,用户将处于会话状态。

于 2012-09-04T14:49:58.673 回答
0

首先要检查的是

require_once 'facebook-php-sdk/src/facebook.php';

确保它与您的 php 代码所在的位置有关,而不是与服务器结构有关。

为了测试这段代码,我可能只使用 Facebook sdk 提供的现有示例,这样您首先可以自信地知道它确实有效,然后可以根据您的需要对其进行调整。只是从这个链接粘贴它 https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php

<?php
/**
 * Copyright 2011 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

require '../src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '344617158898614',
  'secret' => '6dc8ac871858b34798bc2488200e503d',
));

// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}

// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');

?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
    <title>php-sdk</title>
    <style>
      body {
        font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
      }
      h1 a {
        text-decoration: none;
        color: #3b5998;
      }
      h1 a:hover {
        text-decoration: underline;
      }
    </style>
  </head>
  <body>
    <h1>php-sdk</h1>

    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        Login using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
      </div>
    <?php endif ?>

    <h3>PHP Session</h3>
    <pre><?php print_r($_SESSION); ?></pre>

    <?php if ($user): ?>
      <h3>You</h3>
      <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

      <h3>Your User Object (/me)</h3>
      <pre><?php print_r($user_profile); ?></pre>
    <?php else: ?>
      <strong><em>You are not Connected.</em></strong>
    <?php endif ?>

    <h3>Public profile of Naitik</h3>
    <img src="https://graph.facebook.com/naitik/picture">
    <?php echo $naitik['name']; ?>
  </body>
</html>
于 2012-09-04T15:45:16.517 回答