简体   繁体   中英

Facebook Javascript SDK Login - POST Response to PHP

The code below is Facebook's "Login for the Web with the JavaScript SDK", which will send the browser's email address and name to the client-side javascript.
But how do I get the email and name POSted to a PHP file on my website server, so I can log the user in using PHP?

function statusChangeCallback(response) {
    if (response.status === 'connected') {
        testAPI();
    } else if (response.status === 'not_authorized') {
        document.getElementById('status').innerHTML = 'Please log ' + 'into this app.';
    } else {
        document.getElementById('status').innerHTML = 'Please log ' + 'into Facebook.';
    }
}
function checkLoginState() {
    FB.getLoginStatus(function(response){
        statusChangeCallback(response);
    });
}
function testAPI() {
        FB.api('/me',  {fields: 'email,name'},function(response) {
            document.getElementById('status').innerHTML = 'Thanks for logging in again, ' + JSON.stringify(response) + '!<br/><img src="https://graph.facebook.com/'+response.id+'/picture?width=300" />';
    });
}
$(document).ready(function() {
    $.ajaxSetup({ cache: true });
    $.getScript('//connect.facebook.net/en_US/sdk.js', function(){
        FB.init({
            appId: '211867502496511',
            cookie: true, // enable cookies to allow the server to access the session
            xfbml: true, // parse social plugins on this page
            version: 'v2.5' // or v2.0, v2.1, v2.2, v2.3
        }); 
        FB.getLoginStatus(function(response) {
            statusChangeCallback(response);
        });
    });
});
function doalog(){
    FB.login(function(response){
        statusChangeCallback(response);
    });
}

You can use the workhorse of manual networking in client side JavaScript: the XMLHttpRequest .

Basically this is an object that allows you to manually communicate with your server. You can use it in conjunction with a FormData object to POST the data do your server. The request you send can be handled like any other form POST request.

Once you have the email and name you can do this.

var name = theMethodYouUseToGetTheName();
var email = theMethodYouUseToGetTheEmail();

var request = new XMLHttpRequest();
request.onReadyStateChanged = function() {
    if (request.readyState == 4) {
        if (request.status == 200) {
            // your request has been sent and you might have a response
        } else {
           // there has been an error
        }
    }
}

var formData = new FormData();
formData.append("name", name);
formData.append("email", email);

request.open("POST", "http://yourwebsite.com/your/php/file.php", true);
request.send(formData);

Then on your server you can access the POST data like normal using

$userName = $_POST["name"];
$userEmail = $_POST["email"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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