1
1
<?php
2
2
3
+ /**
4
+ * TwitterOAuth - https://github.com/ricardoper/TwitterOAuth
5
+ * PHP library to communicate with Twitter OAuth API version 1.1
6
+ *
7
+ * @author Ricardo Pereira <github@ricardopereira.es>
8
+ * @copyright 2013
9
+ */
10
+
3
11
namespace TwitterOAuth ;
4
12
5
13
class TwitterOAuth
@@ -19,7 +27,12 @@ class TwitterOAuth
19
27
protected $ postParams = array ();
20
28
21
29
22
- public function __construct ($ config )
30
+ /**
31
+ * Prepare a new conection with Twitter API via OAuth
32
+ *
33
+ * @params array $config Configuration array with OAuth access data
34
+ */
35
+ public function __construct (array $ config )
23
36
{
24
37
$ defs = array (
25
38
'consumer_key ' => '' ,
@@ -40,7 +53,14 @@ public function __construct($config)
40
53
unset($ defs , $ filters );
41
54
}
42
55
43
- public function get ($ call , $ getParams = null , $ format = null )
56
+ /**
57
+ * Send a GET call to Twitter API via OAuth
58
+ *
59
+ * @params string $call Twitter resource string
60
+ * @params array $getParams GET parameters to send
61
+ * @params string $format Set the response format
62
+ */
63
+ public function get ($ call , array $ getParams = null , $ format = null )
44
64
{
45
65
$ this ->call = $ call ;
46
66
@@ -55,7 +75,15 @@ public function get($call, $getParams = null, $format = null)
55
75
return $ this ->sendRequest ();
56
76
}
57
77
58
- public function post ($ call , $ postParams = null , $ getParams = null , $ format = null )
78
+ /**
79
+ * Send a POST call to Twitter API via OAuth
80
+ *
81
+ * @params string $call Twitter resource string
82
+ * @params array $postParams POST parameters to send
83
+ * @params array $getParams GET parameters to send
84
+ * @params string $format Set the response format
85
+ */
86
+ public function post ($ call , array $ postParams = null , array $ getParams = null , $ format = null )
59
87
{
60
88
$ this ->call = $ call ;
61
89
@@ -76,7 +104,13 @@ public function post($call, $postParams = null, $getParams = null, $format = nul
76
104
return $ this ->sendRequest ();
77
105
}
78
106
79
- protected function getParams ($ params )
107
+ /**
108
+ * Converting parameters array to a single string with encoded values
109
+ *
110
+ * @param array $params Input parameters
111
+ * @return string Single string with encoded values
112
+ */
113
+ protected function getParams (array $ params )
80
114
{
81
115
$ r = '' ;
82
116
@@ -91,6 +125,12 @@ protected function getParams($params)
91
125
return trim ($ r , '& ' );
92
126
}
93
127
128
+ /**
129
+ * Getting full URL from a Twitter resource and format
130
+ *
131
+ * @param bool $withParams If true then parameters will be outputted
132
+ * @return string Full URL
133
+ */
94
134
protected function getUrl ($ withParams = false )
95
135
{
96
136
$ getParams = '' ;
@@ -106,6 +146,11 @@ protected function getUrl($withParams = false)
106
146
return $ this ->url . $ this ->call . '. ' . $ this ->format . $ getParams ;
107
147
}
108
148
149
+ /**
150
+ * Getting OAuth parameters to be used in request headers
151
+ *
152
+ * @return array OAuth parameters
153
+ */
109
154
protected function getOauthParameters ()
110
155
{
111
156
$ time = time ();
@@ -120,6 +165,11 @@ protected function getOauthParameters()
120
165
);
121
166
}
122
167
168
+ /**
169
+ * Converting all parameters arrays to a single string with encoded values
170
+ *
171
+ * @return string Single string with encoded values
172
+ */
123
173
protected function getRequestString ()
124
174
{
125
175
$ params = array_merge ($ this ->getParams , $ this ->postParams , $ this ->getOauthParameters ());
@@ -129,6 +179,11 @@ protected function getRequestString()
129
179
return rawurlencode ($ params );
130
180
}
131
181
182
+ /**
183
+ * Getting OAuth signature base string
184
+ *
185
+ * @return string OAuth signature base string
186
+ */
132
187
protected function getSignatureBaseString ()
133
188
{
134
189
$ method = strtoupper ($ this ->method );
@@ -138,16 +193,31 @@ protected function getSignatureBaseString()
138
193
return $ method . '& ' . $ url . '& ' . $ this ->getRequestString ();
139
194
}
140
195
196
+ /**
197
+ * Getting a signing key
198
+ *
199
+ * @return string Signing key
200
+ */
141
201
protected function getSigningKey ()
142
202
{
143
203
return $ this ->config ['consumer_secret ' ] . '& ' . $ this ->config ['oauth_token_secret ' ];
144
204
}
145
205
206
+ /**
207
+ * Calculating the signature
208
+ *
209
+ * @return string Signature
210
+ */
146
211
protected function calculateSignature ()
147
212
{
148
213
return base64_encode (hash_hmac ('sha1 ' , $ this ->getSignatureBaseString (), $ this ->getSigningKey (), true ));
149
214
}
150
215
216
+ /**
217
+ * Converting OAuth parameters array to a single string with encoded values
218
+ *
219
+ * @return string Single string with encoded values
220
+ */
151
221
protected function getOauthString ()
152
222
{
153
223
$ oauth = array_merge ($ this ->getOauthParameters (), array ('oauth_signature ' => $ this ->calculateSignature ()));
@@ -167,6 +237,11 @@ protected function getOauthString()
167
237
return $ oauth ;
168
238
}
169
239
240
+ /**
241
+ * Building request HTTP headers
242
+ *
243
+ * @return array HTTP headers
244
+ */
170
245
protected function buildRequestHeader ()
171
246
{
172
247
return array (
@@ -175,6 +250,11 @@ protected function buildRequestHeader()
175
250
);
176
251
}
177
252
253
+ /**
254
+ * Send GET or POST requests to Twitter API
255
+ *
256
+ * @return mixed Response output with the selected format
257
+ */
178
258
protected function sendRequest ()
179
259
{
180
260
$ url = $ this ->getUrl (true );
0 commit comments