2012-01-17 149 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的訪問令牌,用戶必須先授權(獲取權限)您的應用程序。我希望這會有所幫助