我正在使用最新的PHP SDK(v4)开发一个简单的Facebook Canvas应用程序。第一页(index.php)显示Facebook权限弹出窗口(它只询问公共配置文件和电子邮件地址),然后让用户通过表单输入某些数据。从API检索一些其他用户的数据并将其存储在PHP会话变量中。提交表单后,用户将进入第二页(thanks.php),其中会话变量保存在数据库中。该应用程序位于/ facebook / email。
这是我的代码。部分内容取自https://www.webniraj.com/2014/05/01/facebook-api-php-sdk-updated-to-v4-0-0/。
<?php
session_start();
require_once( '../funciones.php' );
require_once '../facebook-php-sdk/autoload.php';
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
use Facebook\FacebookSession;
use Facebook\FacebookSignedRequestFromInputHelper;
use Facebook\FacebookCanvasLoginHelper;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;
$yaparticipo = false;
$app_id = 'xxxxxxxxxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$app_namespace = 'xxxx';
$app_scope = 'email';
// Facebook APP keys
FacebookSession::setDefaultApplication($app_id,$app_secret);
// Helper for fb canvas authentication
$helper = new FacebookCanvasLoginHelper();
// see if $_SESSION exists
if (isset($_SESSION) && isset($_SESSION['fb_token']))
{
// create new fb session from saved fb_token
$session = new FacebookSession($_SESSION['fb_token']);
// validate the fb_token to make sure it's still valid
try
{
$session->validate();
}
catch (Exception $e)
{
// catch any exceptions
$session = null;
}
}
else
{
// no $_SESSION exists
try
{
// create fb session
$session = $helper->getSession();
}
catch(FacebookRequestException $ex)
{
// When Facebook returns an error
//print_r($ex);
$session = null;
}
catch(\Exception $ex)
{
// When validation fails or other local issues
//print_r($ex);
$session = null;
}
}
// check if 1 of the 2 methods above set $session
if (isset($session))
{
// Lets save fb_token for later authentication through saved $_SESSION
$_SESSION['fb_token'] = $session->getToken();
$request = new FacebookRequest($session, 'GET', '/me');
// retrieve some data and store it in session variables
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Facebook app</title>
<link href="../css/style.css" rel="stylesheet">
</head>
<body>
<!-- submit email form -->
</body>
</html>
<?php
}
else {
session_destroy();
$oauthJS = "window.top.location = 'https://www.facebook.com/dialog/oauth?client_id=$app_id&redirect_uri=https://apps.facebook.com/$app_namespace/&scope=$app_scope';";
echo "<script>" . $oauthJS . "</script>";
}
?>
出于某种原因,大多数情况下应用程序运行良好,但有时它会在显示弹出窗口时卡在无限循环中。画布仍然是空白的,浏览器似乎处于一个永恒的'#34; loading&#34;状态。
根据Firebug的说法,重定向工作不正常。此外,由于某种原因,浏览器无法从Facebook域加载图像。请看以下屏幕截图:
http://i.stack.imgur.com/tl0G9.jpg
我在网络上找到了很多修复程序(搜索 facebook登录php无限循环),但没有任何效果。我希望有人可以帮助我。