duanfei8149
2013-06-10 17:47 浏览 92
已采纳

用twitter登录; 使http_code为0

I have this code to enable login with twitter in my site

<?php
require("twitter/twitteroauth.php");
require 'config/twconfig.php'; //CONTAINS CONSUMER SECRET AND CONSUMER KEY
session_start();

$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET);
$twitteroauth->host = "https://api.twitter.com/1.1/";
// Requesting authentication tokens, the parameter is the URL we will be redirected to
$request_token = $twitteroauth->getRequestToken('http://MY WEBSITE URL');

// 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.'.$twitteroauth->http_code);
}
?>

I am using Abraham Williams twitter oauth. This worked well for a couple of weeks, but now am getting a http_code of 0, which is not even listed in twitters list of error codes. What could be the problem

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答 默认 最新

  • 已采纳
    dongpengqin3898 dongpengqin3898 2013-06-14 19:59

    Here is snippted i am using which works fine.

    First make sure of the follownig In dev twitter, you app can read / write Refresh consumer key, Recreate access token.

    If the folling is not working, the problem lies elsewhere t is something else! see https://dev.twitter.com/search/apachesolr_search/HTTP%20CODE%200

    Please read instructions, FOLLOW THEM and YOUR CODE will WORK

    <?php
    require_once('twitteroauth.php');  
    session_start();  
    /* 
     * INSTRUCTIONS!!!
     * https://dev.twitter.com/
     * create app
     * https://dev.twitter.com/ TAB settings 
     * website: THE_URL_TO_YOUR_SCRIPT_WITH_THIS_CODE
     * callback_url http://www.YOURDOMAIN.COM/ 
     * Read, Write and Access direct messages ! 
     * Allow this application to be used to Sign in with Twitter 
     * GO BACK TO DETAILS RECREATE / REFRESH - ACCESS TOKEN! 
     */
    
    $consumerKey = '******************';
    $consumerSecret = '******************';
    $oAuthToken = '*********************';
    $oAuthSecret = '**************************';
    
    // The TwitterOAuth instance  
    $twitteroauth = new TwitterOAuth($consumerKey, $consumerSecret);  
    // Requesting authentication tokens, the parameter is the URL we will be redirected to  
    $request_token = $twitteroauth->getRequestToken('http://DOMAIN.com/YOURLOGINSCRIPT.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.');  
    }  
    ?>
    
    点赞 评论 复制链接分享

相关推荐