用twitter登录;使http_code为0

时间:2013-06-10 17:47:20

标签: php login oauth twitter twitter-oauth

我有这段代码可以在我的网站上使用twitter登录

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

我正在使用Abraham Williams twitter oauth。这种情况好几周,但现在 我得到的http_code为0,甚至没有列在错误代码的twitters列表中。 可能是什么问题

1 个答案:

答案 0 :(得分:1)

这是我正在使用的剪切工作正常。

首先确保以下内容 在dev twitter中,你的app可以读/写 刷新使用者密钥,重新创建访问令牌。

如果后续工作不起作用,那么问题就出在其他地方了! 见https://dev.twitter.com/search/apachesolr_search/HTTP%20CODE%200

请阅读说明,按照以下步骤操作

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