Skip to content

Commit

Permalink
Transfer & connect timeouts, in seconds & milliseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
ozh committed Jan 23, 2014
1 parent 541e6d0 commit 0c6a797
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
17 changes: 17 additions & 0 deletions examples/timeout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

// First, include Requests
include('../library/Requests.php');

// Next, make sure Requests can load internal classes
Requests::register_autoloader();

// Define a timeout of 2.5 seconds
$options = array(
'timeout' => 2.5,
);

// Now let's make a request to a page that will delay its response by 3 seconds
$request = Requests::get('http://httpbin.org/delay/3', array(), $options);

// An exception will be thrown, stating a timeout of the request !
5 changes: 4 additions & 1 deletion library/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ public static function patch($url, $headers, $data = array(), $options = array()
* options:
*
* - `timeout`: How long should we wait for a response?
* (integer, seconds, default: 10)
* (float, seconds with a millisecond precision, default: 10, example: 0.01)
* - `connect_timeout`: How long should we wait while trying to connect?
* (float, seconds with a millisecond precision, default: 10, example: 0.01)
* - `useragent`: Useragent to send to the server
* (string, default: php-requests/$version)
* - `follow_redirects`: Should we follow 3xx redirects?
Expand Down Expand Up @@ -443,6 +445,7 @@ public static function request_multiple($requests, $options = array()) {
protected static function get_default_options($multirequest = false) {
$defaults = array(
'timeout' => 10,
'connect_timeout' => 10,
'useragent' => 'php-requests/' . self::VERSION,
'redirected' => 0,
'redirects' => 10,
Expand Down
12 changes: 10 additions & 2 deletions library/Requests/Transport/cURL.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,17 @@ protected function setup_handle($url, $headers, $data, $options) {
break;
}

if( is_int($options['timeout']) or version_compare($this->version, '7.16.2', '<') ) {
curl_setopt($this->fp, CURLOPT_TIMEOUT, ceil($options['timeout']));
} else {
curl_setopt($this->fp, CURLOPT_TIMEOUT_MS, round($options['timeout'] * 1000) );
}
if( is_int($options['connect_timeout']) or version_compare($this->version, '7.16.2', '<') ) {
curl_setopt($this->fp, CURLOPT_CONNECTTIMEOUT, ceil($options['connect_timeout']));
} else {
curl_setopt($this->fp, CURLOPT_CONNECTTIMEOUT_MS, round($options['connect_timeout'] * 1000));
}
curl_setopt($this->fp, CURLOPT_URL, $url);
curl_setopt($this->fp, CURLOPT_TIMEOUT, $options['timeout']);
curl_setopt($this->fp, CURLOPT_CONNECTTIMEOUT, $options['timeout']);
curl_setopt($this->fp, CURLOPT_REFERER, $url);
curl_setopt($this->fp, CURLOPT_USERAGENT, $options['useragent']);
curl_setopt($this->fp, CURLOPT_HTTPHEADER, $headers);
Expand Down
7 changes: 5 additions & 2 deletions library/Requests/Transport/fsockopen.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function request($url, $headers = array(), $data = array(), $options = ar

$options['hooks']->dispatch('fsockopen.remote_socket', array(&$remote_socket));

$fp = stream_socket_client($remote_socket, $errno, $errstr, $options['timeout'], STREAM_CLIENT_CONNECT, $context);
$fp = stream_socket_client($remote_socket, $errno, $errstr, ceil($options['connect_timeout']), STREAM_CLIENT_CONNECT, $context);

restore_error_handler();

Expand Down Expand Up @@ -198,7 +198,10 @@ public function request($url, $headers = array(), $data = array(), $options = ar
$options['hooks']->dispatch('fsockopen.after_request', array(&$fake_headers));
return '';
}
stream_set_timeout($fp, $options['timeout']);

$timeout_sec = (int) floor( $options['timeout'] );
$timeout_msec = $timeout_sec == $options['timeout'] ? 0 : 1000000 * $options['timeout'] % 1000000;
stream_set_timeout( $fp, $timeout_sec, $timeout_msec );

$this->info = stream_get_meta_data($fp);

Expand Down

0 comments on commit 0c6a797

Please sign in to comment.