duanlu0559
2014-01-30 06:24 阅读 57

执行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.

Login.php

<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
            alert(user_id);
            $.post('start.php', { user_id : user_id }, function ()
            {
                    window.location.href = 'start.php';
            });
        },"json");
    });
   </script>

redirect.php

<?php

/* Start session and load library. */
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');

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


    break;
  default:
    /* Show notification if something went wrong. */
    echo 'Could not connect to Twitter. Refresh the page or try again later.';
}
    ?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dousha1873 dousha1873 2014-02-06 18:51

    twitterLoginForSite

    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.

    点赞 评论 复制链接分享

相关推荐