我正在使用用 PHP 编写的 Facebook API 连接脚本(由 Facebook 提供)。一切正常,它会生成登录 URL,并在我登录时将我重定向回网站。
但是, $user 变量似乎未定义。
有没有人遇到过类似的问题?在 Facebook 应用程序统计页面中,我可以看到该应用程序已被使用。
更新 - 代码:
<?php
$app_id = "xxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxx";
$site_url = "http://xxx";
try{
include_once "src/facebook.php";
}catch(Exception $e){
error_log($e);
}
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret
));
if($user = $facebook->getUser())
{
echo 'ok';
}
else
{
}
if($user){
//==================== Single query method ======================================
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;
}
//==================== Single query method ends =================================
}
if($user){
// Get logout URL
$logoutUrl = $facebook->getLogoutUrl();
}else{
// Get login URL
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email,user_birthday,user_about_me',
'redirect_uri' => $site_url . '/auth_complete.php',
));
}
if($user){
// Proceed knowing you have a logged in user who has a valid session.
//========= Batch requests over the Facebook Graph API using the PHP-SDK ========
// Save your method calls into an array
$queries = array(
array('method' => 'GET', 'relative_url' => '/'.$user),
array('method' => 'GET', 'relative_url' => '/'.$user.'/home?limit=50'),
array('method' => 'GET', 'relative_url' => '/'.$user.'/friends'),
array('method' => 'GET', 'relative_url' => '/'.$user.'/photos?limit=6'),
);
// POST your queries to the batch endpoint on the graph.
try{
$batchResponse = $facebook->api('?batch='.json_encode($queries), 'POST');
}catch(Exception $o){
error_log($o);
}
//Return values are indexed in order of the original array, content is in ['body'] as a JSON
//string. Decode for use as a PHP array.
$user_info = json_decode($batchResponse[0]['body'], TRUE);
$feed = json_decode($batchResponse[1]['body'], TRUE);
$friends_list = json_decode($batchResponse[2]['body'], TRUE);
$photos = json_decode($batchResponse[3]['body'], TRUE);
//========= Batch requests over the Facebook Graph API using the PHP-SDK ends =====
}
?>