0

我正在使用 twitteroauth.php 使用 twitter api 向我的网站添加登录功能。

它工作正常。但我想使用 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.');
}
?>
4

2 回答 2

2

我正在寻找类似于用于 Twitter 登录的 Facebook JS SDK 的东西。Facebook JS SDK 登录时不刷新页面。我使用 JS Popup 窗口制作了自己的。它实际上没有使用 AJAX。

一个。您需要一个链接来触发弹出窗口:

<a href="#" onclick="window.open('http://localhost.com/twitter_login.php', 'Twitter', 'width=640,height=480');">登录 Twitter</一个>

湾。用于 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>
于 2012-12-19T09:12:22.047 回答
0

It's quite easy by using jQuery's $.ajax() function, where you basically post your data to your PHP code.

The only thing that needs a change is the header section - you might want to return a JSON encoded message - like "OK" or "Error Message", so you can react upon that.

$.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);
        }
    }
});

And the PHP part:

<?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.'));
}
?>
于 2012-09-16T07:23:02.860 回答