如何使用Twitter使用PHP和AJAX登陆如何使用、Twitter、AJAX、PHP

2023-09-10 18:34:05 作者:怪咖╮

我用twitteroauth.php使用Twitter API的登录功能添加到我的网站。

它工作正常。但我想使用jQuery和AJAX,这样页面不会得到刷新的回报来实现相同的。

以下是我的一块code

 < PHP
要求(推特/ twitteroauth.php);
要求配置/ twconfig.php;
在session_start();

$ twitteroauth的=新TwitterOAuth对象(YOUR_CONSUMER_KEY,YOUR_CONSUMER_SECRET);
//请求认证令牌,该参数是我们将被重定向到URL
$ request_token = $ twitteroauth-> getRequestToken('http://list.2lessons.com/fbtwLogin/getTwitterData.php');

//保存他们进入会议

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

//如果一切顺利的话..
如果($ twitteroauth-> HTTP_ code == 200){
    //让我们生成的网址和重定向
    $ URL = $ twitteroauth-> getAuthorizeURL($ request_token ['oauth_token']);
    标题(位置:$网址);
} 其他 {
    //这是一个坏主意,杀了剧本,但我们必须知道什么时候有一个错误。
    死亡(有错误发生。);
}
?>
 

解决方案

我正在寻找类似Facebook的JS SDK的微博登录。在登录时Facebook的JS SDK不会刷新页面。我已经用JS弹出窗口让我自己。它实际上没有使用AJAX。

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

< A HREF =#的onclick =的window.open('http://localhost.com/twitter_login.php,微博,宽度= 640,高度= 480');>登录到Twitter< / A>

乙。 Twitter的登录(您在上面提交的code)和PHP脚本     回调(当用户登陆登录后)。

回调应该是这样的:

 < PHP
//在PHP的一部分,你应该了解的登录信息(AccessToken,用户信息,以及任何你想要的东西)存储到数据库

?>
< HTML>
< HEAD><冠军>< /标题>< /头>
<身体GT;
<! - 在HTML中的一部分,我让用户知道他/她已被记录在我的情况,微博图标 - >
<脚本类型=文/ JavaScript的>
        如果(window.opener&安培;&安培;!window.opener.closed){
            window.opener.refreshTwitterIcons(); //函数被调用父窗口,从中用户触发弹出
        }
        window.close()的; //关闭弹出窗口
    < / SCRIPT>
< /身体GT;
< / HTML>
 
请教朋友们,如何用ajax实现点击登录弹出对话框登陆,成功后页面不刷新,修改登录为用户名

I'm using twitteroauth.php to add login functionality to my website using twitter api.

It works fine. But i want to implement the same using jQuery and AJAX so that page won't get refreshed on return.

Following is my piece of code

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

解决方案

I was searching for something similar to Facebook JS SDK for Twitter login. Facebook JS SDK does not refresh the page when logging in. I have made my own using JS Popup window. It's actually not using AJAX.

a. You need a link to trigger popup window:

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

b. PHP script for Twitter login (The code you've submitted above) and callback (Where user lands after login).

Callback should look like:

<?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>