2012-09-04 40 views
0

這是我的代碼:Facebook的API:的getUser()總是返回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 我會感謝您的幫助!

+0

粘貼這將是很好的掩蓋API密鑰和密碼這種方式有沒有安全問題,沒有多餘的使用應用程序的完成。未經授權的使用可能導致Facebook阻止您的帳戶 – pal4life

回答

1

要檢查的第一件事是

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

用戶必須先登錄。

用戶登錄後(通過Facebook發送到您的網站)將顯示其ID。

+0

嗡嗡聲實際上是我在上面提供的代碼中,我試圖用來獲取用戶信息。 所以,如果我理解你的權利?我需要先認證? – Slrg

0

我相信)的構造函數需要您的應用程序的ID和祕密的陣列,以及b)您需要首先登錄的用戶。

如果您查看的頁面爲Facebook PHP SDK GitHub上,在$facebook->getUser();的調用之後,您會注意到它們有一段關於進行API調用的部分,另一部分關於生成登錄或註銷URL,具體取決於用戶是否登錄。

流程是這樣的:

  1. 檢查是否有在會話的用戶
  2. 試圖獲取用戶詳細信息
  3. 如果沒有用戶,重定向到登錄URL

當用戶回到你的應用程序,用戶將進入會話。

+0

我已經登錄了自己的帳戶。所以我應該至少能看到我的名字! – Slrg

+0

這可能是。但*您的*網站不知道*您*已登錄到Facebook;您需要首先進行身份驗證,以便SDK知道誰是我。 –