簡體   English   中英

Facebook Javascript SDK登錄-對PHP的POST響應

[英]Facebook Javascript SDK Login - POST Response to PHP

下面的代碼是Facebook的“使用JavaScript SDK登錄網絡”,它將瀏覽器的電子郵件地址和名稱發送到客戶端javascript。
但是,如何將電子郵件和名稱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);
    });
}

您可以在客戶端JavaScript中使用手動聯網的主力軍: XMLHttpRequest

基本上,這是一個對象,它使您可以手動與服務器通信。 您可以將其與FormData對象結合使用,以將數據發布到服務器上。 您發送的請求可以像其他任何形式的POST請求一樣進行處理。

收到電子郵件和名稱后,即可執行此操作。

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);

然后在您的服務器上,您可以像正常使用那樣訪問POST數據

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM