forked from haniabidkz/Php-Twitter-OAuth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.php
81 lines (57 loc) · 2.51 KB
/
process.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
<?php
session_start();
include('config.php');
require "../autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
//If Session Token and Oauth Token Not Same It Means Session Oauth Token Expired.
if(isset($_GET['oauth_token']) AND $_SESSION['oauth_token']!==$_GET['oauth_token']){
//So We Need to Destry Session
session_destroy();
//Unsetting Session Variables
unset($_SESSION);
//Redirecting so user can sign in again.
header('location:index.php');
}
//if Session and Recived Oauth Token are same it means verified, Now we need to get access token
elseif(isset($_GET['oauth_token']) AND $_SESSION['oauth_token']==$_GET['oauth_token']){
//Passing Oauth Token which will be converted in Access Token..
$con=new TwitterOAuth(CONSUMER_API_KEY,CONSUMER_API_SECRET,$_SESSION['oauth_token'],$_SESSION['oauth_token_secret']);
//Getting Oauth Verfier
$oauth_verifier=$_GET['oauth_verifier'];
//Getting an array which will contain access token, secret , user id and screen name
$access_token=$con->oauth('oauth/access_token',['oauth_verifier'=>$oauth_verifier]);
//Now Unset Oauth Token since we have got access token.
unset($_SESSION['oauth_token']);
//Now Unset Oauth Token Secret since we have got access token Secret.
unset($_SESSION['oauth_token_secret']);
//Stroing the resultant access token in session, Now this will be like a email in normal sign in code.
$_SESSION['access_token']=$access_token;
//Now Passing control to index.php, There third party application can do anythin on the behalf of user !
header('location:index.php');
}
//For Logout
elseif(isset($_GET['logout'])){
session_destroy();
unset($_SESSION);
header('location:index.php');
}
else{
$con=new TwitterOAuth(CONSUMER_API_KEY,CONSUMER_API_SECRET);
// Getting Oauth Token and Secret
$oauth=$con->oauth('oauth/request_token',['oauth_callback'=>CALL_BACK]);
//Getting Oauth Token
$oauth_token=$oauth['oauth_token'];
//Getting Oauth Token Secret
$oauth_token_secret=$oauth['oauth_token_secret'];
//Storing Oauth Token in Session Because They Will Be Different for Each user and we need them in many requests
$_SESSION['oauth_token']=$oauth_token;
//Storing Oauth Secret in Session
$_SESSION['oauth_token_secret']=$oauth_token_secret;
//Getting Link with the oauth token to redirect user to Twiter for Login and Permission
$oauth_url=$con->url('oauth/authenticate',['oauth_token'=>$oauth_token]);
//Redirectng user
header('location:'.$oauth_url);
//Make Sure Script Die
die();
}
?>