getUser始终返回0

时间:2012-01-17 21:01:00

标签: php facebook

我对这段代码有问题,我到处搜索但没有得到这个问题的答案。 这是我的代码

 <?php

      require_once('facebook.php');


      $facebook = new Facebook(array(
        'appId'  => 'XXXXXXXXXXXXXXX',
        'secret' => 'XXXXXXXXXXXXXXX',
        ));

      $user_id = $facebook->getUser();

        echo $user_id;
    ?>

这个代码每次都返回0,即使我用我的帐户登录facebook,有人可以帮我解决这个问题或解释什么是错的吗?我正在使用facebook-php-sdk-v3.1.1-21


我想制作这样的东西,在注册表格中想要添加像facebook一样的按钮,当用户点击这个按钮时他会成为我的脸书粉丝页面的粉丝,如果他没有点击按钮脚本必须检查这个和输出错误,在用户注册之前必须点击按钮,有人可以给我一些提示或一块脚本如何用PHP实现这个?

4 个答案:

答案 0 :(得分:0)

这里有一个如何使用PHP SDK的好例子:

https://github.com/facebook/php-sdk/tree/master/examples

<?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' => 'xxxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxx',
));

// 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>

答案 1 :(得分:0)

似乎$facebook->getUser();无效。仔细检查你是否正确链接到facebook.php。

此外,您可能想要隐藏您的appId和秘密。 : - )

答案 2 :(得分:0)

我有与github相同的代码,仍然getuser返回0.这是我的代码:这是我测试应用程序的地方,然后将其变为病毒式传播。

<?
require 'facebook.php';
$app_id = 'xxxxxxxxxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxxxxxx';

$post_login_url = "http://myhandyapps.com/tester2.php";
 $code = $_REQUEST["code"];

       //Obtain the access_token with publish_stream permission
      if(empty($code))
         {
           $dialog_url= "https://www.facebook.com/dialog/oauth?"
           . "client_id=" . $app_id
           . "&redirect_uri=" . urlencode($post_login_url)
           . "&scope=publish_stream, photo_upload, user_photos, user_photo_video_tags";
           echo("<script>top.location.href='" . $dialog_url .
           "'</script>");
       }

       else{
        $facebook = new Facebook(array(
          'appId'  => $app_id,
          'secret' => $app_secret,
          'cookie' => true,

        ));

        // Get User ID
        $user = $facebook->getUser();
        echo "$user";
        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;
          }
        }

        if ($user) {
          $logoutUrl = $facebook->getLogoutUrl();
        } else {
          $loginUrl = $facebook->getLoginUrl();
        }
        //echo "$user";
        }
?>

修改 我通过更改我的代码来修复它

$post_login_url = "http://myhandyapps.com/tester2.php";

$post_login_url = "http://apps.facebook.com/nspace_tester";  //got this to my app (CANVAS PAGE)

答案 3 :(得分:0)

我有同样的问题,经过长时间的努力弄清楚问题是什么,我发现你需要访问令牌来获取用户ID。要从signed_request获取访问令牌,用户必须首先授权(获取权限)您的应用程序。我希望这会有所帮助