简体   繁体   English

Facebook使用PHP登录然后使用get args重定向到URL

[英]Facebook login using PHP then redirect to URL with get args

I am trying to make a Facebook login in my website, I have faced a problems. 我想在我的网站上登录Facebook,我遇到了问题。

I have used this class to use Facebook login in PHP. 我已经使用这个类在PHP中使用Facebook登录。

I have created a PHP file in my site called login.php which contain the following code 我在我的站点中创建了一个名为login.php的PHP文件,其中包含以下代码

$facebook = new FacebookLogin(APP_ID, APP_SECRET);
$user = $facebook->doLogin();
// then initialize the SESSION variables here using $user info
header("Location: ' . $url);

The problem now, is that I want to pass get arguments to the login.php and from this argument I can know the $url which I will redirect to. 现在的问题是,我想将get参数传递给login.php ,从这个参数我可以知道我将重定向到的$ url。 But the FacebookLogin class is making a redirect call so the get argument in login.php is lost, so I have decided to add the login.php argument to $this->url in the FacebookLogin constructor with this line: 但是FacebookLogin类正在进行重定向调用,因此login.php中的get参数丢失了,所以我决定使用以下行在FacebookLogin构造函数中将login.php参数添加到$this->url

$this->my_url = 'http://' . $_SERVER['SERVER_NAME'] .  $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];

The problem now is that this is not working too as in the verifyLogin() , this line is returning empty result as the URL is not correct: 现在的问题是,这在verifyLogin()verifyLogin() ,由于URL不正确,此行返回空结果:

$result = @file_get_contents(self::TOKEN_URL . '?' . http_build_query($data));

and it was supposed to return the access_token 它应该返回access_token

So what do you think? 所以你怎么看?

Simply I need a way that the user click a link to login by Facebook but before returning to the user, I want to pass by my code in PHP then redirect to a url that was specified by the user click in the get arguments. 简单地说,我需要一种方法,用户点击Facebook登录的链接,但在返回用户之前,我希望通过PHP中的代码传递,然后重定向到用户指定的URL,点击get参数。

I'm sorry, it may be silly but I've spent a lot of time trying to solve this problem and have a deadline after few hours. 对不起,这可能很傻,但我花了很多时间试图解决这个问题,并在几个小时后设定截止日期。

Appreciate your help 感谢您的帮助

What you try to achieve is simple with usage of PHP-SDK : 使用PHP-SDK,您尝试实现的目标很简单:

$facebook = new Facebook(array(
  'appId' => 'YOUR_APP_ID',
  'secret'=> 'YOUR_APP_SECRET'
));

$loginURL = $facebook->getLoginUrl(array(
  //'redirect_uri'=>'URL_WHERE_USER_WILL_BE_LANDED_AFTER_LOGIN' 
));

header("Location: {$loginURL}");

If you wish to pass some data to your application for later step than user came back authorized, pass that as parts of URL arguments for URL provided in redirect_uri parameter of getLoginUrl . 如果您希望将某些数据传递给您的应用程序以供后续步骤使用,请将其作为getLoginUrl redirect_uri参数中提供的URL的URL参数的一部分传递。

So if for example you will pass http://my.app.com/?link_to_visit=some_link as redirect_url you will be redirected to that URL instead of current URL (before authorization). 因此,例如,如果您将http://my.app.com/?link_to_visit=some_link作为redirect_url传递,您将被重定向到该URL而不是当前URL(在授权之前)。

Later you can just: 稍后您可以:

$facebook = new Facebook(array(
  'appId' => 'YOUR_APP_ID',
  'secret'=> 'YOUR_APP_SECRET'
));

$userId = $facebook->getUser();
$accessToken = $facebook->getAccessToken();

$linkToVisit = $_REQUEST['link_to_visit'];

BTW , for the sake of humanity don't use some silly client written by someone with unknown reputation before couple of years in favor of Official Facebook PHP-SDK that do follow all changes (and they are huge) in platform... 顺便说一句 ,为了人性化,不要使用一些陌生的客户端,这些客户端会在几年前支持官方Facebook PHP-SDK,并且在平台上跟随所有变化(并且它们是巨大的)...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM