8000 Finish 39-curl-timeout · lpirir/codebird-php@70b1b01 · GitHub
[go: up one dir, main page]

Skip to content

Commit 70b1b01

Browse files
committed
Finish 39-curl-timeout
2 parents 3cbae77 + 5c157db commit 70b1b01

File tree

3 files changed

+86
-29
lines changed

3 files changed

+86
-29
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
codebird-php - changelog
22
========================
33

4+
2.5.0 (not yet released)
5+
+ Option to set cURL timeout
6+
47
2.4.2 (not yet released)
58
+ Add section about cacert.pem to README
69

README.md

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,13 @@ You can find it within the return object’s ```httpstatus``` property.
204204

205205
### 5.1 Dealing with rate-limits
206206

207-
Basically, Codebird leaves it up to you to handle Twitter’s rate limit.
207+
Basically, Codebird leaves it up to you to handle Twitter’s rate limit.
208208
The library returns the response HTTP status code, so you can detect rate limits.
209209

210-
I suggest you to check if the ```$reply->httpstatus``` property is ```400```
211-
and check with the Twitter API to find out if you are currently being
212-
rate-limited.
213-
See the [Rate Limiting FAQ](https://dev.twitter.com/docs/rate-limiting-faq)
210+
I suggest you to check if the ```$reply->httpstatus``` property is ```400```
211+
and check with the Twitter API to find out if you are currently being
212+
rate-limited.
213+
See the [Rate Limiting FAQ](https://dev.twitter.com/docs/rate-limiting-faq)
214214
for more information.
215215

216216
6. Return formats
@@ -277,7 +277,7 @@ First retrieve the user object using
277277

278278
with ```$username``` being the username of the account you wish to retrieve the profile image from.
279279

280-
Then get the value from the index ```profile_image_url``` or ```profile_image_url_https``` of the user object previously retrieved.
280+
Then get the value from the index ```profile_image_url``` or ```profile_image_url_https``` of the user object previously retrieved.
281281

282282

283283
For example:
@@ -289,7 +289,7 @@ For example:
289289

290290
When the user returns from the authentication screen, you need to trade
291291
the obtained request token for an access token, using the OAuth verifier.
292-
As discussed in the section ‘Usage example,’ you use a call to
292+
As discussed in the section ‘Usage example,’ you use a call to
293293
```oauth/access_token``` to do that.
294294

295295
The API reply to this method call tells you details about the user that just logged in.
@@ -305,29 +305,29 @@ stdClass Object
305305
[user_id] => 14648265
306306
[screen_name] => mynetx
307307
[httpstatus] => 200
308-
)
308+
)
309309
```
310310

311-
If you need to get more details, such as the user’s latest tweet,
312-
you should fetch the complete User Entity. The simplest way to get the
313-
user entity of the currently authenticated user is to use the
311+
If you need to get more details, such as the user’s latest tweet,
312+
you should fetch the complete User Entity. The simplest way to get the
313+
user entity of the currently authenticated user is to use the
314314
```account/verify_credentials``` API method. In Codebird, it works like this:
315315

316316
```php
317317
$reply = $cb->account_verifyCredentials();
318318
print_r($reply);
319319
```
320320

321-
I suggest to cache the User Entity after obtaining it, as the
322-
```account/verify_credentials``` method is rate-limited by 15 calls per 15 minutes.
321+
I suggest to cache the User Entity after obtaining it, as the
322+
```account/verify_credentials``` method is rate-limited by 15 calls per 15 minutes.
323323

324324
…walk through cursored results?
325325
-------------------------------
326326

327-
The Twitter REST API utilizes a technique called ‘cursoring’ to paginate
328-
large result sets. Cursoring separates results into pages of no more than
329-
5000 results at a time, and provides a means to move backwards and
330-
forwards through these pages.
327+
The Twitter REST API utilizes a technique called ‘cursoring’ to paginate
328+
large result sets. Cursoring separates results into pages of no more than
329+
5000 results at a time, and provides a means to move backwards and
330+
forwards through these pages.
331331

332332
Here is how you can walk through cursored results with Codebird.
333333

@@ -348,11 +348,11 @@ $nextCursor = $result1->next_cursor_str;
348348
}
349349
```
350350

351-
To navigate back instead of forth, use the field ```$resultX->previous_cursor_str```
351+
To navigate back instead of forth, use the field ```$resultX->previous_cursor_str```
352352
instead of ```next_cursor_str```.
353353

354-
It might make sense to use the cursors in a loop. Watch out, though,
355-
not to send more than the allowed number of requests to ```followers/list```
354+
It might make sense to use the cursors in a loop. Watch out, though,
355+
not to send more than the allowed number of requests to ```followers/list```
356356
per rate-limit timeframe, or else you will hit your rate-limit.
357357

358358
…use xAuth with Codebird?
@@ -370,9 +370,9 @@ $reply = $cb->oauth_accessToken(array(
370370
));
371371
```
372372

373-
Are you getting a strange error message? If the user is enrolled in
374-
login verification, the server will return a HTTP 401 error with a custom body.
375-
If you are using the send_error_codes parameter, you will receive the
373+
Are you getting a strange error message? If the user is enrolled in
374+
login verification, the server will return a HTTP 401 error with a custom body.
375+
If you are using the send_error_codes parameter, you will receive the
376376
following error message in the response body:
377377

378378
```xml
@@ -387,16 +387,28 @@ Otherwise, the response body will contain a plaintext response:
387387
User must verify login
388388
```
389389

390-
When this error occurs, advise the user to
390+
When this error occurs, advise the user to
391391
[generate a temporary password](https://twitter.com/settings/applications)
392392
on twitter.com and use that to complete signing in to the application.
393393

394394
…know what cacert.pem is for?
395395
-----------------------------
396396

397-
Connections to the Twitter API are done over a secured SSL connection.
398-
Since 2.4.0, codebird-php checks if the Twitter API server has a valid
399-
SSL certificate. Valid certificates have a correct signature-chain.
400-
The cacert.pem file contains a list of all public certificates for root
401-
certificate authorities. You can find more information about this file
397+
Connections to the Twitter API are done over a secured SSL connection.
398+
Since 2.4.0, codebird-php checks if the Twitter API server has a valid
399+
SSL certificate. Valid certificates have a correct signature-chain.
400+
The cacert.pem file contains a list of all public certificates for root
401+
certificate authorities. You can find more information about this file
402402
at http://curl.haxx.se/docs/caextract.html.
403+
404+
…set the timeout for requests to the Twitter API?
405+
-------------------------------------------------
406+
407+
For connecting to Twitter, Codebird uses the cURL library.
408+
You can specify both the connection timeout and the request timeout,
409+
in milliseconds:
410+
411+
```php
412+
$cb->setConnectionTimeout(2000);
413+
$cb->setTimeout(5000);
414+
```

src/codebird.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ class Codebird
109109
*/
110110
protected $_version = '2.4.2-dev';
111111

112+
/**
113+
* Request timeout
114+
*/
115+
protected $_timeout;
116+
117+
/**
118+
* Connection timeout
119+
*/
120+
protected $_connectionTimeout;
121+
112122
/**
113123
* Returns singleton class instance
114124
* Always use this method unless you're working with multiple authenticated users at once
@@ -173,6 +183,30 @@ public function setToken($token, $secret)
173183
$this->_oauth_token_secret = $secret;
174184
}
175185

186+
/**
187+
* Sets request timeout in milliseconds
188+
*
189+
* @param int $timeout Request timeout in milliseconds
190+
*
191+
* @return void
192+
*/
193+
public function setTimeout($timeout)
194+
{
195+
$this->_timeout = (int) $timeout;
196+
}
197+
198+
/**
199+
* Sets connection timeout in milliseconds
200+
*
201+
* @param int $timeout Connection timeout in milliseconds
202+
*
203+
* @return void
204+
*/
205+
public function setConnectionTimeout($timeout)
206+
{
207+
$this->_connectionTimeout = (int) $timeout;
208+
}
209+
176210
/**
177211
* Sets the format for API replies
178212
*
@@ -905,6 +939,14 @@ protected function _callApi($httpmethod, $method, $method_template, $params = ar
905939
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
906940
curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . '/cacert.pem');
907941

942+
if (isset($this->_timeout)) {
943+
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->_timeout);
944+
}
945+
946+
if (isset($this->_connectionTimeout)) {
947+
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->_connectionTimeout);
948+
}
949+
908950
$reply = curl_exec($ch);
909951

910952
// certificate validation results

0 commit comments

Comments
 (0)
0