简体   繁体   English

Facebook Javascript SDK登录-对PHP的POST响应

[英]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. 下面的代码是Facebook的“使用JavaScript SDK登录网络”,它将浏览器的电子邮件地址和名称发送到客户端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? 但是,如何将电子邮件和名称POS定位到网站服务器上的PHP文件中,以便可以使用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 . 您可以在客户端JavaScript中使用手动联网的主力军: 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. 您可以将其与FormData对象结合使用,以将数据发布到服务器上。 The request you send can be handled like any other form POST request. 您发送的请求可以像其他任何形式的POST请求一样进行处理。

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 然后在您的服务器上,您可以像正常使用那样访问POST数据

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

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

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