使用twitteroauth(https://github.com/abraham/twitteroauth/tree/master/src)SDK
创建Twitter登录这是我正在使用的代码
<?php
ob_start();
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require 'twitteroauth/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
define('CONSUMER_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxx');
define('CONSUMER_SECRET', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
define('OAUTH_CALLBACK', getenv('http://istshrna.com/dokholtatbik.php'));
echo "<meta charset='utf-8' />";
if(isset($_REQUEST['oauth_verifier'])){
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK));
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));
$_SESSION['access_token'] = $access_token;
$content = $access_token->oauth('account/verify_credentials');
$data = array();
if( !empty( $content->id )){
$data['id'] = $content->id;
$data['name'] = $content->name;
$data['screen_name'] = $content->screen_name;
$data['picture'] = $content->profile_image_url;
try {
$user_obj->twitter_login($data);
}catch (Exception $e) {
$error = $e->getMessage();
}
}else{
session_unset();
session_destroy();
//header('Location: index.php');
}
}else{
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => OAUTH_CALLBACK));
$_SESSION['oauth_token'] = $request_token['oauth_token'];
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
$url = $connection->url('oauth/authorize', array('oauth_token' => $request_token['oauth_token']));
header("location: $url");
}
?>
但是我在回调
时收到此错误消息致命错误:未捕获的异常&#39; Abraham \ TwitterOAuth \ TwitterOAuthException&#39;与消息 &#39;处理您的OAuth请求时出错:oauth_verifier参数无效&#39; 在/xxxx1/xxxx2/public_html/twitteroauth/src/TwitterOAuth.php:138堆栈跟踪:#0 /xxxx1/xxxx2/public_html/dokholtatbik.php(30):亚伯拉罕\ TwitterOAuth \ TwitterOAuth-&gt; oauth(&#39; oauth / access_to ...&#39;,数组)#1 {main}投放在第138行的/xxxx1/xxxx2/public_html/twitteroauth/src/TwitterOAuth.php
............................................... .......................................