[英]Facebook API : getUser() always returning 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的原因,我將感謝您的幫助!
用戶必須先登錄。
用戶登錄(通過Facebook登錄到您的網站)后,將顯示其ID。
我相信a)構造函數需要一個由您的應用程序ID和密碼組成的數組,b)您需要首先登錄用戶。
如果在GitHub上查看Facebook PHP SDK的頁面,則在調用$facebook->getUser();
之后會注意到$facebook->getUser();
他們有一個關於API調用的章節,另一個是關於生成登錄或注銷URL的章節,具體取決於用戶是否登錄。
流程如下:
當用戶回到您的應用程序時,該用戶將處於會話狀態。
首先要檢查的是
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.