Skip to content

Commit 29b2217

Browse files
committed
OAuth Files
1 parent f215db8 commit 29b2217

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed

config.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
4+
Define("CONSUMER_API_KEY","JwOQkKLhP0YR872jzbx1Hbcvb");
5+
Define("CONSUMER_API_SECRET","quJ4hObqwJaTog4r37YEty4O2zf3pfT0kbymH4EvHrAYclB0l1");
6+
Define("CALL_BACK","http://localhost".$_SERVER['PHP_SELF']);
7+
?>

index.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
session_start();
3+
include('config.php');
4+
require "../autoload.php";
5+
use Abraham\TwitterOAuth\TwitterOAuth;
6+
7+
//If Session With Token is set that means user is logged in on twter and has authorized app.
8+
if(isset($_SESSION['access_token'])){
9+
10+
$user_id=$_SESSION['access_token']['user_id'];
11+
$screen_name=$_SESSION['access_token']['screen_name'];
12+
13+
//Logout Feature
14+
echo "<br><a href='process.php?logout'>Logout</a><br><br>";
15+
16+
//Access Token From Session
17+
$access_token=$_SESSION['access_token']['oauth_token'];
18+
19+
//Access Token Secret From Session
20+
$access_token_secret=$_SESSION['access_token']['oauth_token_secret'];
21+
22+
// Passing the Access Token and Secret Now to Post/Get Resources on the behalf of user.
23+
$con=new TwitterOAuth(CONSUMER_API_KEY,CONSUMER_API_SECRET,$access_token,$access_token_secret);
24+
25+
//1-Getting User Details:(Name, Followers, Following, Profile Pic, Banner)
26+
$info=$con->get('users/show',['screen_name'=>$screen_name]);
27+
28+
//Endocding To Decode in Array
29+
$info=json_encode($info);
30+
31+
//Decodeing To Array
32+
$info=json_decode($info,true);
33+
34+
//Information i.e Name, Des Profile Image ETC
35+
$name=$info['name'];
36+
$country=$info['location'];
37+
$text=$info['description'];
38+
$profile_image_url=$info['profile_image_url'];
39+
40+
//Displaying User Info
41+
echo "Welcome Mr: ".$name."<br><hr>";
42+
echo"<h1>Personal Info</h1>";
43+
echo "<img src=$profile_image_url width='100' height='100'></img><br><br>";
44+
echo "Full Name:".$name."<br>";
45+
echo "Description: ".$text."<br>";
46+
echo "Location: ".$country."<br><hr><br>";
47+
echo"<form action='' method='post'><textarea cols='20' rows='10' name='status'></textarea><br><input type='submit' value='Tweet'></form>";
48+
49+
50+
//2-Post Box for Posting a Info Above Then If Posted then Post a Tweet
51+
if(isset($_POST['status'])){
52+
53+
$status=$_POST['status'];
54+
$status_post=$con->post('statuses/update',['status'=>$status]);
55+
56+
if($con->getLastHttpCode()==200){
57+
echo "Tweet Has Been Posted !";
58+
}
59+
else{
60+
echo "Please Try Again";
61+
}
62+
63+
}
64+
65+
//3-User Tweets
66+
$tweets=$con->get("statuses/user_timeline", ["screen_name"=>$screen_name]);
67+
$tweets=json_encode($tweets);
68+
$tweets=json_decode($tweets,true);
69+
70+
echo "<hr><h1>My Tweets</h1><br>";
71+
foreach ($tweets as $tweet) {
72+
73+
echo "- ".$tweet['text']."<br><br>";
74+
}
75+
76+
//4- User Followers List
77+
$followers=$con->get("followers/list", ["screen_name"=>$screen_name]);
78+
$followers=json_encode($followers);
79+
$followers=json_decode($followers,true);
80+
81+
echo "<hr><h1>Followers</h2><br>";
82+
83+
//Nested Loop For Retireving Followers List
84+
foreach ($followers as $follower) {
85+
if(is_array($follower)){
86+
foreach ($follower as $follow) {
87+
echo $follow['name']."<br><br>";
88+
}
89+
}
90+
}
91+
92+
}
93+
else{
94+
echo "<a href='process.php'><img width='150' height='50' src='login.png'></a></img>";
95+
}
96+
97+
?>

login.png

26.9 KB
Loading

process.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
session_start();
3+
include('config.php');
4+
require "../autoload.php";
5+
use Abraham\TwitterOAuth\TwitterOAuth;
6+
7+
//If Session Token and Oauth Token Not Same It Means Session Oauth Token Expired.
8+
if(isset($_GET['oauth_token']) AND $_SESSION['oauth_token']!==$_GET['oauth_token']){
9+
10+
//So We Need to Destry Session
11+
session_destroy();
12+
13+
//Unsetting Session Variables
14+
unset($_SESSION);
15+
16+
//Redirecting so user can sign in again.
17+
header('location:index.php');
18+
}
19+
20+
//if Session and Recived Oauth Token are same it means verified, Now we need to get access token
21+
elseif(isset($_GET['oauth_token']) AND $_SESSION['oauth_token']==$_GET['oauth_token']){
22+
23+
//Passing Oauth Token which will be converted in Access Token..
24+
$con=new TwitterOAuth(CONSUMER_API_KEY,CONSUMER_API_SECRET,$_SESSION['oauth_token'],$_SESSION['oauth_token_secret']);
25+
26+
//Getting Oauth Verfier
27+
$oauth_verifier=$_GET['oauth_verifier'];
28+
29+
//Getting an array which will contain access token, secret , user id and screen name
30+
$access_token=$con->oauth('oauth/access_token',['oauth_verifier'=>$oauth_verifier]);
31+
32+
//Now Unset Oauth Token since we have got access token.
33+
unset($_SESSION['oauth_token']);
34+
35+
//Now Unset Oauth Token Secret since we have got access token Secret.
36+
unset($_SESSION['oauth_token_secret']);
37+
38+
//Stroing the resultant access token in session, Now this will be like a email in normal sign in code.
39+
$_SESSION['access_token']=$access_token;
40+
41+
//Now Passing control to index.php, There third party application can do anythin on the behalf of user !
42+
header('location:index.php');
43+
}
44+
45+
//For Logout
46+
elseif(isset($_GET['logout'])){
47+
48+
session_destroy();
49+
unset($_SESSION);
50+
header('location:index.php');
51+
}
52+
53+
else{
54+
$con=new TwitterOAuth(CONSUMER_API_KEY,CONSUMER_API_SECRET);
55+
56+
// Getting Oauth Token and Secret
57+
$oauth=$con->oauth('oauth/request_token',['oauth_callback'=>CALL_BACK]);
58+
59+
//Getting Oauth Token
60+
$oauth_token=$oauth['oauth_token'];
61+
62+
//Getting Oauth Token Secret
63+
$oauth_token_secret=$oauth['oauth_token_secret'];
64+
65+
//Storing Oauth Token in Session Because They Will Be Different for Each user and we need them in many requests
66+
$_SESSION['oauth_token']=$oauth_token;
67+
68+
//Storing Oauth Secret in Session
69+
$_SESSION['oauth_token_secret']=$oauth_token_secret;
70+
71+
//Getting Link with the oauth token to redirect user to Twiter for Login and Permission
72+
$oauth_url=$con->url('oauth/authenticate',['oauth_token'=>$oauth_token]);
73+
74+
//Redirectng user
75+
header('location:'.$oauth_url);
76+
77+
//Make Sure Script Die
78+
die();
79+
}
80+
81+
?>

0 commit comments

Comments
 (0)