简体   繁体   English

PHP cURL来验证服务器上的Facebook API登录access_token?

[英]PHP cURL to verify Facebook API login access_token on server?

Could anyone share a working example of how to verify on my PHP server using cURL the Facebook access_token I got from the browser, so that I can verify the login details from the browser are trustworthy then safely create a session for my user on my server? 谁能分享一个可行的示例,说明如何使用cURL从浏览器获取的Facebook access_token在我的PHP服务器上进行验证,以便我可以验证浏览器的登录详细信息是否可信,然后在我的服务器上安全地为用户创建会话?

To review, the steps I want to make work are: 回顾一下,我要做的步骤是:

  1. User clicks "Continue with Facebook" on the browser and gets an access_token. 用户在浏览器上单击“继续使用Facebook”,并获得一个access_token。
  2. I send this to my PHP server. 我将此发送到我的PHP服务器。
  3. The server sends a cURL request to Facebook to validate the user access_token. 服务器向Facebook发送一个cURL请求,以验证用户的access_token。
  4. If access_token is valid, then create session for the user on my server. 如果access_token有效,则在我的服务器上为用户创建会话。

I have my own app with email, Google and Facebook login. 我有自己的应用程序,带有电子邮件,Google和Facebook登录名。 I appreciate any help, thanks. 感谢您的帮助,谢谢。

After quite some time, I got a working PHP script. 一段时间后,我得到了一个有效的PHP脚本。

Just replace the missing variable values and it should work. 只需替换丢失的变量值,它应该可以工作。 Also make sure you have cURL working on your PHP server with phpinfo() or some other means; 另外,请确保使用phpinfo()或其他方法在您的PHP服务器上使用cURL。

<?php

///////////////////////////////////////
// prep Facebook verification
///////////////////////////////////////

// sanitize login data
$_POST['facebook_access_token'] = filter_var($_POST['facebook_access_token'], FILTER_SANITIZE_STRING);

// set variables
$facebook_user_access_token = $_POST['facebook_access_token'];
$my_facebook_app_id = 'REPLACE';
$my_facebook_app_secret = 'REPLACE';
$facebook_application = 'REPLACE'; // in my case 'domain.com', as set up in Facebook

///////////////////////////////////////
// get facebook access token
///////////////////////////////////////
$curl_facebook1 = curl_init(); // start curl
$url = "https://graph.facebook.com/oauth/access_token?client_id=".$my_facebook_app_id."&client_secret=".$my_facebook_app_secret."&grant_type=client_credentials"; // set url and parameters
curl_setopt($curl_facebook1, CURLOPT_URL, $url); // set the url variable to curl
curl_setopt($curl_facebook1, CURLOPT_RETURNTRANSFER, true); // return output as string
$output = curl_exec($curl_facebook1); // execute curl call
curl_close($curl_facebook1); // close curl
$decode_output = json_decode($output, true); // decode the response (without true this will crash)

// store access_token
$facebook_access_token = $decode_output['access_token'];

///////////////////////////////////////
// verify my access was legitimate
///////////////////////////////////////
$curl_facebook2 = curl_init(); // start curl
$url = "https://graph.facebook.com/debug_token?input_token=".$facebook_user_access_token."&access_token=".$facebook_access_token; // set url and parameters
curl_setopt($curl_facebook2, CURLOPT_URL, $url); // set the url variable to curl
curl_setopt($curl_facebook2, CURLOPT_RETURNTRANSFER, true); // return output as string
$output2 = curl_exec($curl_facebook2); // execute curl call
curl_close($curl_facebook2); // close curl
$decode_output2 = json_decode($output2, true); // decode the response (without true this will crash)

// test browser and Facebook variables match for security
if ($my_facebook_app_id == $decode_output2['data']['app_id'] && $decode_output2['data']['application'] == $facebook_application && $decode_output2['data']['is_valid'] == true) {
    echo 'Success. Login is valid.';
}
else {
    echo 'Error.';
}

?>

Special thanks to https://stackoverflow.com/a/16092226/6252345 特别感谢https://stackoverflow.com/a/16092226/6252345

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

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