|
@@ -3,7 +3,7 @@ namespace OSS\Http;
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * Handles all HTTP requests using cURL and manages the responses.
|
|
|
+ * Handle all HTTP requests using cURL and manages the responses.
|
|
|
*
|
|
|
* @version 2011.06.07
|
|
|
* @copyright 2006-2011 Ryan Parman
|
|
@@ -23,6 +23,21 @@ class RequestCore
|
|
|
* The headers being sent in the request.
|
|
|
*/
|
|
|
public $request_headers;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The raw response callback headers
|
|
|
+ */
|
|
|
+ public $response_raw_headers;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Response body when error occurs
|
|
|
+ */
|
|
|
+ public $response_error_body;
|
|
|
+
|
|
|
+ /**
|
|
|
+ *The hander of write file
|
|
|
+ */
|
|
|
+ public $write_file_handle;
|
|
|
|
|
|
/**
|
|
|
* The body being sent in the request.
|
|
@@ -54,18 +69,13 @@ class RequestCore
|
|
|
*/
|
|
|
public $response_info;
|
|
|
|
|
|
- /**
|
|
|
- * The handle for the cURL object.
|
|
|
- */
|
|
|
- public $curl_handle;
|
|
|
-
|
|
|
/**
|
|
|
* The method by which the request is being made.
|
|
|
*/
|
|
|
public $method;
|
|
|
|
|
|
/**
|
|
|
- * Stores the proxy settings to use for the request.
|
|
|
+ * Store the proxy settings to use for the request.
|
|
|
*/
|
|
|
public $proxy = null;
|
|
|
|
|
@@ -160,14 +170,14 @@ class RequestCore
|
|
|
public $registered_streaming_write_callback = null;
|
|
|
|
|
|
/**
|
|
|
- * 请求超时时间, 默认是5184000秒,6天
|
|
|
+ * The request timeout time, which is 5,184,000 seconds,that is, 6 days by default
|
|
|
*
|
|
|
* @var int
|
|
|
*/
|
|
|
public $timeout = 5184000;
|
|
|
|
|
|
/**
|
|
|
- * 连接超时时间,默认是10秒
|
|
|
+ * The connection timeout time, which is 10 seconds by default
|
|
|
*
|
|
|
* @var int
|
|
|
*/
|
|
@@ -206,7 +216,7 @@ class RequestCore
|
|
|
// CONSTRUCTOR/DESTRUCTOR
|
|
|
|
|
|
/**
|
|
|
- * Constructs a new instance of this class.
|
|
|
+ * Construct a new instance of this class.
|
|
|
*
|
|
|
* @param string $url (Optional) The URL to request or service endpoint to query.
|
|
|
* @param string $proxy (Optional) The faux-url to use for proxy settings. Takes the following format: `proxy://user:pass@hostname:port`
|
|
@@ -239,7 +249,7 @@ class RequestCore
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Destructs the instance. Closes opened file handles.
|
|
|
+ * Destruct the instance. Closes opened file handles.
|
|
|
*
|
|
|
* @return $this A reference to the current instance.
|
|
|
*/
|
|
@@ -261,7 +271,7 @@ class RequestCore
|
|
|
// REQUEST METHODS
|
|
|
|
|
|
/**
|
|
|
- * Sets the credentials to use for authentication.
|
|
|
+ * Set the credentials to use for authentication.
|
|
|
*
|
|
|
* @param string $user (Required) The username to authenticate with.
|
|
|
* @param string $pass (Required) The password to authenticate with.
|
|
@@ -275,7 +285,7 @@ class RequestCore
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Adds a custom HTTP header to the cURL request.
|
|
|
+ * Add a custom HTTP header to the cURL request.
|
|
|
*
|
|
|
* @param string $key (Required) The custom HTTP header to set.
|
|
|
* @param mixed $value (Required) The value to assign to the custom HTTP header.
|
|
@@ -288,7 +298,7 @@ class RequestCore
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Removes an HTTP header from the cURL request.
|
|
|
+ * Remove an HTTP header from the cURL request.
|
|
|
*
|
|
|
* @param string $key (Required) The custom HTTP header to set.
|
|
|
* @return $this A reference to the current instance.
|
|
@@ -314,7 +324,7 @@ class RequestCore
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Sets a custom useragent string for the class.
|
|
|
+ * Set a custom useragent string for the class.
|
|
|
*
|
|
|
* @param string $ua (Required) The useragent string to use.
|
|
|
* @return $this A reference to the current instance.
|
|
@@ -363,7 +373,7 @@ class RequestCore
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Sets the length in bytes to read from the stream while streaming up.
|
|
|
+ * Set the length in bytes to read from the stream while streaming up.
|
|
|
*
|
|
|
* @param integer $size (Required) The length in bytes to read from the stream.
|
|
|
* @return $this A reference to the current instance.
|
|
@@ -376,7 +386,7 @@ class RequestCore
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Sets the resource to read from while streaming up. Reads the stream from its current position until
|
|
|
+ * Set the resource to read from while streaming up. Reads the stream from its current position until
|
|
|
* EOF or `$size` bytes have been read. If `$size` is not given it will be determined by <php:fstat()> and
|
|
|
* <php:ftell()>.
|
|
|
*
|
|
@@ -404,7 +414,7 @@ class RequestCore
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Sets the file to read from while streaming up.
|
|
|
+ * Set the file to read from while streaming up.
|
|
|
*
|
|
|
* @param string $location (Required) The readable location to read from.
|
|
|
* @return $this A reference to the current instance.
|
|
@@ -418,7 +428,7 @@ class RequestCore
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Sets the resource to write to while streaming down.
|
|
|
+ * Set the resource to write to while streaming down.
|
|
|
*
|
|
|
* @param resource $resource (Required) The writeable resource to write to.
|
|
|
* @return $this A reference to the current instance.
|
|
@@ -431,7 +441,7 @@ class RequestCore
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Sets the file to write to while streaming down.
|
|
|
+ * Set the file to write to while streaming down.
|
|
|
*
|
|
|
* @param string $location (Required) The writeable location to write to.
|
|
|
* @return $this A reference to the current instance.
|
|
@@ -439,9 +449,6 @@ class RequestCore
|
|
|
public function set_write_file($location)
|
|
|
{
|
|
|
$this->write_file = $location;
|
|
|
- $write_file_handle = fopen($location, 'w');
|
|
|
-
|
|
|
- return $this->set_write_stream($write_file_handle);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -473,6 +480,28 @@ class RequestCore
|
|
|
return $this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * A callback function that is invoked by cURL for streaming up.
|
|
|
+ *
|
|
|
+ * @param resource $curl_handle (Required) The cURL handle for the request.
|
|
|
+ * @param resource $header_content (Required) The header callback result.
|
|
|
+ * @return headers from a stream.
|
|
|
+ */
|
|
|
+ public function streaming_header_callback($curl_handle, $header_content)
|
|
|
+ {
|
|
|
+ $code = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
|
|
|
+
|
|
|
+ if (isset($this->write_file) && intval($code) / 100 == 2 && !isset($this->write_file_handle))
|
|
|
+ {
|
|
|
+ $this->write_file_handle = fopen($this->write_file, 'w');
|
|
|
+ $this->set_write_stream($this->write_file_handle);
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->response_raw_headers .= $header_content;
|
|
|
+ return strlen($header_content);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* Register a callback function to execute whenever a data stream is read from using
|
|
|
* <CFRequest::streaming_read_callback()>.
|
|
@@ -571,10 +600,18 @@ class RequestCore
|
|
|
*/
|
|
|
public function streaming_write_callback($curl_handle, $data)
|
|
|
{
|
|
|
+ $code = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
|
|
|
+
|
|
|
+ if (intval($code) / 100 != 2)
|
|
|
+ {
|
|
|
+ $this->response_error_body .= $data;
|
|
|
+ return strlen($data);
|
|
|
+ }
|
|
|
+
|
|
|
$length = strlen($data);
|
|
|
$written_total = 0;
|
|
|
$written_last = 0;
|
|
|
-
|
|
|
+
|
|
|
while ($written_total < $length) {
|
|
|
$written_last = fwrite($this->write_stream, substr($data, $written_total));
|
|
|
|
|
@@ -594,7 +631,7 @@ class RequestCore
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Prepares and adds the details of the cURL request. This can be passed along to a <php:curl_multi_exec()>
|
|
|
+ * Prepare and adds the details of the cURL request. This can be passed along to a <php:curl_multi_exec()>
|
|
|
* function.
|
|
|
*
|
|
|
* @return resource The handle for the cURL object.
|
|
@@ -617,6 +654,7 @@ class RequestCore
|
|
|
curl_setopt($curl_handle, CURLOPT_NOSIGNAL, true);
|
|
|
curl_setopt($curl_handle, CURLOPT_REFERER, $this->request_url);
|
|
|
curl_setopt($curl_handle, CURLOPT_USERAGENT, $this->useragent);
|
|
|
+ curl_setopt($curl_handle, CURLOPT_HEADERFUNCTION, array($this, 'streaming_header_callback'));
|
|
|
curl_setopt($curl_handle, CURLOPT_READFUNCTION, array($this, 'streaming_read_callback'));
|
|
|
|
|
|
// Verification of the SSL cert
|
|
@@ -647,8 +685,6 @@ class RequestCore
|
|
|
|
|
|
// Enable a proxy connection if requested.
|
|
|
if ($this->proxy) {
|
|
|
- curl_setopt($curl_handle, CURLOPT_HTTPPROXYTUNNEL, true);
|
|
|
-
|
|
|
$host = $this->proxy['host'];
|
|
|
$host .= ($this->proxy['port']) ? ':' . $this->proxy['port'] : '';
|
|
|
curl_setopt($curl_handle, CURLOPT_PROXY, $host);
|
|
@@ -715,7 +751,7 @@ class RequestCore
|
|
|
|
|
|
default: // Assumed GET
|
|
|
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, $this->method);
|
|
|
- if (isset($this->write_stream)) {
|
|
|
+ if (isset($this->write_stream) || isset($this->write_file)) {
|
|
|
curl_setopt($curl_handle, CURLOPT_WRITEFUNCTION, array($this, 'streaming_write_callback'));
|
|
|
curl_setopt($curl_handle, CURLOPT_HEADER, false);
|
|
|
} else {
|
|
@@ -747,18 +783,23 @@ class RequestCore
|
|
|
{
|
|
|
// Accept a custom one if it's passed.
|
|
|
if ($curl_handle && $response) {
|
|
|
- $this->curl_handle = $curl_handle;
|
|
|
$this->response = $response;
|
|
|
}
|
|
|
|
|
|
// As long as this came back as a valid resource...
|
|
|
- if (is_resource($this->curl_handle)) {
|
|
|
+ if (is_resource($curl_handle)) {
|
|
|
// Determine what's what.
|
|
|
- $header_size = curl_getinfo($this->curl_handle, CURLINFO_HEADER_SIZE);
|
|
|
+ $header_size = curl_getinfo($curl_handle, CURLINFO_HEADER_SIZE);
|
|
|
$this->response_headers = substr($this->response, 0, $header_size);
|
|
|
$this->response_body = substr($this->response, $header_size);
|
|
|
- $this->response_code = curl_getinfo($this->curl_handle, CURLINFO_HTTP_CODE);
|
|
|
- $this->response_info = curl_getinfo($this->curl_handle);
|
|
|
+ $this->response_code = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
|
|
|
+ $this->response_info = curl_getinfo($curl_handle);
|
|
|
+
|
|
|
+ if (intval($this->response_code) / 100 != 2 && isset($this->write_file))
|
|
|
+ {
|
|
|
+ $this->response_headers = $this->response_raw_headers;
|
|
|
+ $this->response_body = $this->response_error_body;
|
|
|
+ }
|
|
|
|
|
|
// Parse out the headers
|
|
|
$this->response_headers = explode("\r\n\r\n", trim($this->response_headers));
|
|
@@ -775,11 +816,10 @@ class RequestCore
|
|
|
|
|
|
// Reset the headers to the appropriate property.
|
|
|
$this->response_headers = $header_assoc;
|
|
|
- $this->response_headers['_info'] = $this->response_info;
|
|
|
- $this->response_headers['_info']['method'] = $this->method;
|
|
|
-
|
|
|
+ $this->response_headers['info'] = $this->response_info;
|
|
|
+ $this->response_headers['info']['method'] = $this->method;
|
|
|
+
|
|
|
if ($curl_handle && $response) {
|
|
|
- //return new $this->response_class($this->response_headers, $this->response_body, $this->response_code, $this->curl_handle);
|
|
|
return new ResponseCore($this->response_headers, $this->response_body, $this->response_code);
|
|
|
}
|
|
|
}
|
|
@@ -789,7 +829,7 @@ class RequestCore
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Sends the request, calling necessary utility functions to update built-in properties.
|
|
|
+ * Send the request, calling necessary utility functions to update built-in properties.
|
|
|
*
|
|
|
* @param boolean $parse (Optional) Whether to parse the response with ResponseCore or not.
|
|
|
* @return string The resulting unparsed data from the request.
|
|
@@ -816,88 +856,6 @@ class RequestCore
|
|
|
return $this->response;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Sends the request using <php:curl_multi_exec()>, enabling parallel requests. Uses the "rolling" method.
|
|
|
- *
|
|
|
- * @param array $handles (Required) An indexed array of cURL handles to process simultaneously.
|
|
|
- * @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul>
|
|
|
- * <li><code>callback</code> - <code>string|array</code> - Optional - The string name of a function to pass the response data to. If this is a method, pass an array where the <code>[0]</code> index is the class and the <code>[1]</code> index is the method name.</li>
|
|
|
- * <li><code>limit</code> - <code>integer</code> - Optional - The number of simultaneous requests to make. This can be useful for scaling around slow server responses. Defaults to trusting cURLs judgement as to how many to use.</li></ul>
|
|
|
- * @return array Post-processed cURL responses.
|
|
|
- */
|
|
|
- public function send_multi_request($handles, $opt = null)
|
|
|
- {
|
|
|
- set_time_limit(0);
|
|
|
-
|
|
|
- // Skip everything if there are no handles to process.
|
|
|
- if (count($handles) === 0) return array();
|
|
|
-
|
|
|
- if (!$opt) $opt = array();
|
|
|
-
|
|
|
- // Initialize any missing options
|
|
|
- $limit = isset($opt['limit']) ? $opt['limit'] : -1;
|
|
|
-
|
|
|
- // Initialize
|
|
|
- $handle_list = $handles;
|
|
|
- $http = new $this->request_class();
|
|
|
- $multi_handle = curl_multi_init();
|
|
|
- $handles_post = array();
|
|
|
- $added = count($handles);
|
|
|
- $last_handle = null;
|
|
|
- $count = 0;
|
|
|
- $i = 0;
|
|
|
-
|
|
|
- // Loop through the cURL handles and add as many as it set by the limit parameter.
|
|
|
- while ($i < $added) {
|
|
|
- if ($limit > 0 && $i >= $limit) break;
|
|
|
- curl_multi_add_handle($multi_handle, array_shift($handles));
|
|
|
- $i++;
|
|
|
- }
|
|
|
-
|
|
|
- do {
|
|
|
- $active = false;
|
|
|
-
|
|
|
- // Start executing and wait for a response.
|
|
|
- while (($status = curl_multi_exec($multi_handle, $active)) === CURLM_CALL_MULTI_PERFORM) {
|
|
|
- // Start looking for possible responses immediately when we have to add more handles
|
|
|
- if (count($handles) > 0) break;
|
|
|
- }
|
|
|
-
|
|
|
- // Figure out which requests finished.
|
|
|
- $to_process = array();
|
|
|
-
|
|
|
- while ($done = curl_multi_info_read($multi_handle)) {
|
|
|
- // Since curl_errno() isn't reliable for handles that were in multirequests, we check the 'result' of the info read, which contains the curl error number, (listed here http://curl.haxx.se/libcurl/c/libcurl-errors.html )
|
|
|
- if ($done['result'] > 0) {
|
|
|
- throw new RequestCore_Exception('cURL resource: ' . (string)$done['handle'] . '; cURL error: ' . curl_error($done['handle']) . ' (' . $done['result'] . ')');
|
|
|
- } // Because curl_multi_info_read() might return more than one message about a request, we check to see if this request is already in our array of completed requests
|
|
|
- elseif (!isset($to_process[(int)$done['handle']])) {
|
|
|
- $to_process[(int)$done['handle']] = $done;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Actually deal with the request
|
|
|
- foreach ($to_process as $pkey => $done) {
|
|
|
- $response = $http->process_response($done['handle'], curl_multi_getcontent($done['handle']));
|
|
|
- $key = array_search($done['handle'], $handle_list, true);
|
|
|
- $handles_post[$key] = $response;
|
|
|
-
|
|
|
- if (count($handles) > 0) {
|
|
|
- curl_multi_add_handle($multi_handle, array_shift($handles));
|
|
|
- }
|
|
|
-
|
|
|
- curl_multi_remove_handle($multi_handle, $done['handle']);
|
|
|
- curl_close($done['handle']);
|
|
|
- }
|
|
|
- } while ($active || count($handles_post) < $added);
|
|
|
-
|
|
|
- curl_multi_close($multi_handle);
|
|
|
-
|
|
|
- ksort($handles_post, SORT_NUMERIC);
|
|
|
- return $handles_post;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
/*%******************************************************************************************%*/
|
|
|
// RESPONSE METHODS
|
|
|
|