-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter_callback.php
83 lines (71 loc) · 2.82 KB
/
twitter_callback.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
session_start();
include_once("twitter_config.php");
include_once("twitter_inc/twitteroauth.php");
include_once("db.php");
if(isset($_REQUEST['oauth_token']) && $_SESSION['token'] !== $_REQUEST['oauth_token']) {
//If token is old, distroy session and redirect user to index.php
session_destroy();
header('Location: index.php');
}elseif(isset($_REQUEST['oauth_token']) && $_SESSION['token'] == $_REQUEST['oauth_token']) {
//Successful response returns oauth_token, oauth_token_secret, user_id, and screen_name
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['token'] , $_SESSION['token_secret']);
$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);
if($connection->http_code == '200')
{
//Redirect user to twitter
$_SESSION['status'] = 'verified';
$_SESSION['request_vars'] = $access_token;
//Insert user into the database
$params = array('include_email' => 'true', 'include_entities' => 'false', 'skip_status' => 'true');
$user_info = $connection->get('account/verify_credentials', $params); // get the data
echo "<pre>";
var_dump($user_info);
echo "</pre>";
$name = explode(" ",$user_info->name);
$s_id = $user_info->id_str;
echo "$s_id<hr>";
$image = $user_info->profile_image_url;
echo "$image<hr>";
$email = $user_info->email;
echo "$email<hr>";
$fName = isset($name[0])?$name[0]:$user_info->screen_name;
echo "$fName<hr>";
$llName = isset($name[1])?$name[1]:'';
$lllName = isset($name[2])?$name[2]:'';
$lName = $llName." ".$lllName;
echo "$lName<hr>";
$type = "4";
$link = "http://apiproject.com/db.php?s_id=".$s_id."&image=".$image."&email=".$email."&fName=".$fName."&lName=".$lName."&type=".$type;
//echo "<pre>".$link."</pre>";
//$db_user->checkUser('twitter',$user_info->id,$user_info->screen_name,$fname,$lname,$user_info->lang,$access_token['oauth_token'],$access_token['oauth_token_secret'],$user_info->profile_image_url);
//Unset no longer needed request tokens
unset($_SESSION['token']);
unset($_SESSION['token_secret']);
header('Location: '.$link);
}else{
die("error, try again later!");
}
}else{
if(isset($_GET["denied"]))
{
header('Location: login.php');
die();
}
//Fresh authentication
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);
$request_token = $connection->getRequestToken(OAUTH_CALLBACK);
//Received token info from twitter
$_SESSION['token'] = $request_token['oauth_token'];
$_SESSION['token_secret'] = $request_token['oauth_token_secret'];
//Any value other than 200 is failure, so continue only if http code is 200
if($connection->http_code == '200')
{
//redirect user to twitter
$twitter_url = $connection->getAuthorizeURL($request_token['oauth_token']);
header('Location: ' . $twitter_url);
}else{
die("error connecting to twitter! try again later!");
}
}
?>