我正在尝试更改我网站上 Facebook 登录的重定向 URL。这样我就可以转到一个页面,如果他们不存在,我可以在我的数据库中创建一个新用户,然后将他们重定向到我的主页。但是,当我尝试登录时,我在 Facebook 上收到以下错误:PHP SDK 单元测试发生错误。请稍后再试。
我的代码(我想将它们重定向到 mysite.com/createfbuser.php):
public function getLoginUrl($params=array()) {
$this->establishCSRFTokenState();
$currentUrl = $_SERVER['SERVER_NAME'] . '/createfbuser.php';
return $this->getUrl(
'www',
'dialog/oauth',
array_merge(array(
'client_id' => $this->getAppId(),
'redirect_uri' => $currentUrl, // possibly overwritten
'state' => $this->state,
'scope' => 'email'),
$params));
}
EDIT 原代码为$currentUrl = $this->getCurrentUrl();
供引用
最佳答案
首先,您不必编辑 PHP SDK,下面是验证用户然后重定向到您的着陆页的示例,
确保更换:
YOUR-APP-ID-HERE 和你的 facebook 应用程序 ID,
YOUR-APP-API-SECRET-HERE 和您的 facebook 应用程序 key
YOUR-REDIRECT-URL-HERE 和您的着陆页 URL
<?php
// Requires Facebook PHP SDK 3.0.1: https://github.com/facebook/php-sdk/
require ('facebook.php');
define('FACEBOOK_APP_ID',"YOUR-APP-ID-HERE");
define('FACEBOOK_SECRET',"YOUR-APP-API-SECRET-HERE");
define('REDIRECT_URI',"YOUR-REDIRECT-URL-HERE");
$user = null;
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true
));
$user = $facebook->getUser(); // Get the UID of the connected user, or 0 if the Facebook user is not connected.
if($user == 0) {
// If the user is not connected to your application, redirect the user to authentication page
/**
* Get a Login URL for use with redirects. By default, full page redirect is
* assumed. If you are using the generated URL with a window.open() call in
* JavaScript, you can pass in display=popup as part of the $params.
*
* The parameters:
* - redirect_uri: the url to go to after a successful login
* - scope: comma separated list of requested extended perms
*/
$login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI));
echo ("<script> top.location.href='".$login_url."'</script>");
} else {
// if the user is already connected, then redirect them to landing page or show some content
echo ("<script> window.location.href='".REDIRECT_URI."'</script>");
}
?>
如果你想获得扩展权限,那么只需在登录 url 中添加另一个“范围”参数,例如:
$login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI,'scope' => 'comma-separated-list-of-requested-extended-perms'));
有关权限的更多详细信息,请参阅 facebook permissions docs
关于php - 在登录时更改 Facebook 重定向 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6780641/