-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi.php
More file actions
97 lines (88 loc) · 3.29 KB
/
api.php
File metadata and controls
97 lines (88 loc) · 3.29 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/** API CALLS **/
function apiCall($request) {
global $CONSUMER_KEY, $U_SESSION, $CONSUMER_SECRET;
switch($request) {
// First script call, checks for user registered
// and updates the system_url with the new one.
case 'url':
$access_token = (array) $U_SESSION['access_token'];
if($U_SESSION['status'] == 'verified') getPage($U_SESSION['system_url'] . 'allow/true/' . $access_token['screen_name']);
if($system) die('url_updated');
die('url');
break;
// Generates an URL that authenticates the avatar.
case 'allow':
$connection = new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET);
$request_token = $connection->getRequestToken(); // Callback url for this user (ID)
$U_SESSION['oauth_token'] = $token = $request_token['oauth_token'];
$U_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
switch ($connection->http_code) {
case 200:
storeSession($ownerkey, $U_SESSION);
die( 'allow:' . $connection->getAuthorizeURL($token) );
break;
default:
echo 'error:' . getError($connection->http_info, true);// . ':' . var_dump($U_SESSION);
exit;
}
break;
// Simple status update.
// @status - string
case 'post':
$parameters = array('status' => $_POST['status']);
$connection = twitterConnect();
$status = $connection->post('statuses/update', $parameters);
switch ($connection->http_code) {
case 200:
echo 'status:updated';
break;
default:
echo 'status:error:' . getError($connection->http_info, true);
exit;
}
break;
// Timeline request: Sends to the LSL the lastest post available up to 10
// @last - integer(id) of the last status received by lsl
case 'timeline':
$connection = twitterConnect();
$paramaters = array('since_id'=>$_POST['last']);
$timeline = $connection->post('statuses/friends_timeline', $parameters);
switch ($connection->http_code) {
case 200:
$full_timeline = json_decode($timeline);
$trim_timeline = array();
//Parse the full_timeline into a trimmed one (only neccessary data)
foreach($full_timeline as $status) {
$id = $status->id;
$user = $status->user->screen_name;
$msg = $status->text;
$trim_timeline[] = array($id, $user, $msg);
}
//Check the timeline size, output max data and send the remaining to LSL
break;
default:
getPage($U_SESSION['system_url'] . 'timeline/error/' . getError($connection->http_info, true));
exit;
}
break;
default:
echo ':)';
exit;
}
}
// Returns the twitter object authenticated with the access_tokens
function twitterConnect() {
global $CONSUMER_KEY, $U_SESSION, $CONSUMER_SECRET;
$access_token = (array) $U_SESSION['access_token'];
$OAUTH_TOKEN = $access_token['oauth_token'];
$OAUTH_TOKEN_SECRET = $access_token['oauth_token_secret'];
return new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET, $OAUTH_TOKEN, $OAUTH_TOKEN_SECRET);
}
// Return a string with the error detail given by twitter.
function getError($data, $encode = false) {
$data = (array) $data;
return ($encode) ? base64_encode($data[0]) : $data[0];
//$json_data = json_decode($data[0]);
//return ($encode) ? base64_encode($json_data->error) : $json_data->error;
}