2012-01-17 152 views
0

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

<?php 

     require_once('facebook.php'); 


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

     $user_id = $facebook->getUser(); 

     echo $user_id; 
    ?> 

此代码每次返回即使当我登录Facebook上与我的帐户0,有人可以帮我解决这个问题,或者说明了什么问题?我使用的是Facebook的PHP-SDK-v3.1.1-21


我想让这样的事情,在登记表要添加Facebook的喜欢按钮,此按钮,他成为了球迷,当用户点击我的Facebook粉丝页面,如果他不打按钮脚本必须检查此和输出错误,在用户可以注册之前一定要打按钮,有人可以给我一些提示或脚本如何用PHP来实现这一点?

+1

登录到Facebook还不够。你的应用程序也需要。 – 2012-01-17 21:02:30

+0

该怎么办? – 2012-01-17 21:04:00

+1

那里有[大量教程](https://www.google.com/search?q=php+facebook+connect)。看看其中的一些,并给它一个镜头。如果您有任何问题,请不要犹豫,随时回答一个具体问题。 – 2012-01-17 21:16:53

回答

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> 
+0

我想做这样的事情,以注册形式想要添加像Facebook一样的按钮,当用户点击这个按钮时,他成为我的Facebook粉丝页面的粉丝,如果他不像按钮脚本那样点击,必须检查这个并输出错误,那用户在注册之前一定要打按钮,有人会给我一些提示或者脚本如何用PHP来实现这个? – 2012-01-18 12:18:13

+0

你不应该在一个问题中问两个问题。我已经回答了您的原始问题。请不要在线提出其他问题。请参阅:http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – DMCS 2012-01-18 14:04:26

0

好像$facebook->getUser();不工作。仔细检查你是否正确链接到facebook.php。

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

+0

你好,这是一个可能的评论..因为它不是真正的回答问题,或者不能说明问题出在哪里 – 2013-02-08 17:48:34

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) 
0

我有同样的问题,以后的试图找出很长一段时间是什么问题是,我发现你需要访问令牌来获取用户ID。要获取来自signed_request的访问令牌,用户必须先授权(获取权限)您的应用程序。我希望这会有所帮助