2012-09-16 73 views
0

我正在使用twitter auth使用twitteroauth.php將登錄功能添加到我的網站。如何使用PHP和AJAX使用twitter登錄

它工作正常。但我想要使用jQuery和AJAX實現相同的功能,以便頁面在返回時不會刷新。

以下是我的一段代碼

<?php 
require("twitter/twitteroauth.php"); 
require 'config/twconfig.php'; 
session_start(); 

$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET); 
// Requesting authentication tokens, the parameter is the URL we will be redirected to 
$request_token = $twitteroauth->getRequestToken('http://list.2lessons.com/fbtwLogin/getTwitterData.php'); 

// Saving them into the session 

$_SESSION['oauth_token'] = $request_token['oauth_token']; 
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; 

// If everything goes well.. 
if ($twitteroauth->http_code == 200) { 
    // Let's generate the URL and redirect 
    $url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']); 
    header('Location: ' . $url); 
} else { 
    // It's a bad idea to kill the script, but we've got to know when there's an error. 
    die('Something wrong happened.'); 
} 
?> 

回答

0

這是通過使用jQuery's $.ajax()功能,在這裏你基本上你的數據發佈到你的PHP代碼相當容易。

唯一需要更改的是標題部分 - 您可能想要返回JSON編碼的消息 - 如「確定」或「錯誤消息」,以便對此作出響應。

$.ajax({ 
    url: "your_file.php", 
    type: 'post', 
    dataType: 'json', 
    success: function(data, status, xhr) { 
     if (data.msg == 'OK') { 
      // all ok, user logged in 
     } else { 
      // display error 
      console.log(data.msg); 
     } 
    } 
}); 

而且PHP的一部分:

<?php 
require("twitter/twitteroauth.php"); 
require 'config/twconfig.php'; 
session_start(); 

$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET); 
// Requesting authentication tokens, the parameter is the URL we will be redirected to 
$request_token = $twitteroauth->getRequestToken('http://list.2lessons.com/fbtwLogin/getTwitterData.php'); 

// Saving them into the session 

$_SESSION['oauth_token'] = $request_token['oauth_token']; 
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret']; 

// If everything goes well.. 
if ($twitteroauth->http_code == 200) { 
    // Let's generate the URL and redirect 
    $url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']); 
    echo json_encod(array('msg' => 'OK')); 
} else { 
    // It's a bad idea to kill the script, but we've got to know when there's an error. 
    echo json_encod(array('msg' => 'Something wrong happened.')); 
} 
?> 
2

我正在尋找類似Facebook的JS SDK Twitter的登錄的東西。 Facebook JS SDK在登錄時不刷新頁面。我使用JS Popup窗口創建了自己的頁面。它實際上不使用AJAX。

a。你需要一個鏈接來觸發彈出窗口:

< a href =「#」onclick =「window.open('http://localhost.com/twitter_login.php','Twitter','width = 640,高度= 480');「>登錄Twitter </a>

b。用於Twitter登錄的PHP腳本(您上面提交的代碼)和 回調(用戶在登錄後登陸)。

回調應該是這樣的:

<?php 
// In php part you should store information about login (AccessToken, User info, and anything else you want) into DB 

?> 
<html> 
<head><title></title></head> 
<body> 
<!-- In html part I let user know that he/she has been logged in. In my case, Twitter icons --> 
<script type="text/javascript"> 
     if(window.opener && !window.opener.closed) { 
      window.opener.refreshTwitterIcons(); // function is called in parent window from which user has triggered the popup 
     } 
     window.close(); // Closing the popup 
    </script> 
</body> 
</html> 
+0

是的,這是一個很好的建議。但是如何從彈出窗口返回到父窗口(它激活了彈出窗口) – 2lessons

+0

「return」是什麼意思?在上面的例子中,函數'refreshTwitterIcons()'是從父級調用的。 'window.opener'通常返回一個彈出窗口的父窗口。如果你想僅僅返回父視圖,只需使用'window.close()'。我的建議類似於Facobook登錄程序。您只需點擊「登錄」按鈕,然後打開彈出窗口,輸入您的登錄數據並允許應用程序。然後彈出窗口中的頁面被重定向回到回調窗口,並關閉彈出窗口(使用'window.close()')。希望現在很清楚:) –

+0

我現在不能顯示實時預覽:( –