duanlu0559 2014-01-30 06:24
浏览 72

执行twitter登录(服务器端)并通过jquery / ajax获取登录结果

In login.php I have html, ajax code. Login button executes php script redirect.php to perform twitter authentication.

redirect.php invokes someother files for authentication, and finally gives result I want: id and name

I want to retrieve this value in login.php file.


<script type = "text/javascript">
    $('#loginTwitter').click(function () {
        window.location.href = 'redirect.php';
        //After executing, it should come back here to fetch `name` and `id`
        $.get('redirect.php', function(data) {
           var  user_id= data.id;
           var name = data.name;
            //post name and id to start.php
            $.post('start.php', { user_id : user_id }, function ()
                    window.location.href = 'start.php';



/* Start session and load library. */

/* Build TwitterOAuth object with client credentials. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);

/* Get temporary credentials. */
$request_token = $connection->getRequestToken(OAUTH_CALLBACK);

/* Save temporary credentials to session. */
$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];

/* If last connection failed don't display authorization link. */
switch ($connection->http_code) {
  case 200:
    /* Build authorize URL and redirect user to Twitter. */
    $url = $connection->getAuthorizeURL($token);
//      echo "URL is : $url";
//    header('Location: ' . $url);

        if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
            header('Location: ./clearsessions.php');
        $access_token = $_SESSION['access_token'];
        $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
        $content = $connection->get('account/verify_credentials');
        $twitteruser = $content->{'screen_name'};
        $notweets = 5;
        $tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
        $name = $content->{'name'};
        $id = $content->{'id'};
        echo json_encode((object) array('id' => $id, 'name' => $name));

    /* Show notification if something went wrong. */
    echo 'Could not connect to Twitter. Refresh the page or try again later.';
  • 写回答

1条回答 默认 最新

  • dousha1873 2014-02-06 18:51


    Add twitter login button on your site and get user profile data into db

    There are couple of files to do this in nice flow

    Get source here : GitHub link

    1. config.php - Add you twitter auth keys on this page. Later we can access those value just by including this file

    2. login.php - You site page. We will keep login button here

    3. redirect.php - This page will start session and load library function. It creates connection using your twitter keys and redirect to twitter authentication system. Here user authenticates your Twitter apps and enters his twitter credentials

    4. callback.php - After user enters correct parameters he is redirect to this page. If the oauth_token is old redirect to the connect page. If the user has been verified then the access tokens can be saved for future use. it will do it automatically. Here you can now get user profile info. But for easiness we will do it in next page index.php

    5. index.php - Get user profile info and store it in db

    Customization of Abrahams twitter auth github source.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方