-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter-rest-api.php
executable file
·157 lines (116 loc) · 4.75 KB
/
twitter-rest-api.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace TwitterRestApi;
Class Wrapper {
public $api_key;
public $api_secret;
public $access_token;
public $access_token_secret;
public $oauth;
public $api_base_url;
public function __construct($api_key, $api_secret, $version = '1.1'){
$this->api_key = $api_key;
$this->api_secret = $api_secret;
$this->api_base_url = 'https://api.twitter.com/'.$version.'/';
}
public function setAccessToken($token, $secret){
$this->access_token = $token;
$this->access_token_secret = $secret;
}
public function get($url, $parameters=''){
return json_decode($this->curlGET($this->buildRequestURL($url), $parameters));
}
public function post($url, $parameters=''){
return json_decode($this->curlPOST($this->buildRequestURL($url), $parameters));
}
public function debug($data){
echo "<pre>";
print_r($data);
echo "</pre>";
}
private function curlPOST($url, $parameters=''){
$oauth = $this->setOAuthSignature($url, 'POST', $parameters);
$header = array(
$this->buildAuthorizationHeader($oauth),
'Expect:'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, (is_array($parameters) ? ltrim($this->parseParameters($parameters), '?') : ''));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
private function curlGET($url, $parameters=''){
$oauth = $this->setOAuthSignature($url, 'GET', $parameters);
$header = array(
$this->buildAuthorizationHeader($oauth),
'Expect:'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $url.(is_array($parameters) ? $this->parseParameters($parameters) : ''));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
private function buildRequestURL($url){
return $this->api_base_url.ltrim($url, '/');
}
private function parseParameters($parameters){
$params = '?';
$params_array = array();
foreach ($parameters as $key => $val){
$params_array[] = $key.'='.urlencode($val);
}
$params .= implode('&', $params_array);
return $params;
}
private function addParamsToOauth($parameters, $oauth){
foreach($parameters as $key => $val){
$oauth[$key] = $val;
}
return $oauth;
}
private function setOAuthSignature($url, $type, $data=''){
$oauth = array(
'oauth_consumer_key' => $this->api_key,
'oauth_nonce' => time(),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_token' => $this->access_token,
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
if (is_array($data)){
$oauth = $this->addParamsToOauth($data, $oauth);
}
$base_info = $this->buildBaseString($url, $type, $oauth);
$composite_key = rawurlencode($this->api_secret) . '&' . rawurlencode($this->access_token_secret);
$oauth['oauth_signature'] = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
return $oauth;
}
private function buildBaseString($baseURI, $method, $params) {
$r = array();
ksort($params);
foreach($params as $key=>$value){
$r[] = "$key=" . rawurlencode($value);
}
return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}
private function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value){
$values[] = "$key=\"" . rawurlencode($value) . "\"";
}
$r .= implode(', ', $values);
return $r;
}
}
?>