6 * @subpackage Transport
13 * @subpackage Transport
15 class Requests_Transport_cURL implements Requests_Transport {
16 const CURL_7_10_5 = 0x070A05;
17 const CURL_7_16_2 = 0x071002;
27 * Information on the current request
29 * @var array cURL information array, see {@see http://php.net/curl_getinfo}
48 * Have we finished the headers yet?
52 protected $done_headers = false;
55 * If streaming to a file, keep the file pointer
59 protected $stream_handle;
64 public function __construct() {
65 $curl = curl_version();
66 $this->version = $curl['version_number'];
67 $this->fp = curl_init();
69 curl_setopt($this->fp, CURLOPT_HEADER, false);
70 curl_setopt($this->fp, CURLOPT_RETURNTRANSFER, 1);
71 if ($this->version >= self::CURL_7_10_5) {
72 curl_setopt($this->fp, CURLOPT_ENCODING, '');
74 if (defined('CURLOPT_PROTOCOLS')) {
75 curl_setopt($this->fp, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
77 if (defined('CURLOPT_REDIR_PROTOCOLS')) {
78 curl_setopt($this->fp, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
85 * @throws Requests_Exception On a cURL error (`curlerror`)
87 * @param string $url URL to request
88 * @param array $headers Associative array of request headers
89 * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
90 * @param array $options Request options, see {@see Requests::response()} for documentation
91 * @return string Raw HTTP result
93 public function request($url, $headers = array(), $data = array(), $options = array()) {
94 $this->setup_handle($url, $headers, $data, $options);
96 $options['hooks']->dispatch('curl.before_send', array(&$this->fp));
98 if ($options['filename'] !== false) {
99 $this->stream_handle = fopen($options['filename'], 'wb');
100 curl_setopt($this->fp, CURLOPT_FILE, $this->stream_handle);
103 if (isset($options['verify'])) {
104 if ($options['verify'] === false) {
105 curl_setopt($this->fp, CURLOPT_SSL_VERIFYHOST, 0);
106 curl_setopt($this->fp, CURLOPT_SSL_VERIFYPEER, 0);
108 } elseif (is_string($options['verify'])) {
109 curl_setopt($this->fp, CURLOPT_CAINFO, $options['verify']);
113 if (isset($options['verifyname']) && $options['verifyname'] === false) {
114 curl_setopt($this->fp, CURLOPT_SSL_VERIFYHOST, 0);
117 $response = curl_exec($this->fp);
119 $options['hooks']->dispatch('curl.after_send', array(&$fake_headers));
121 if (curl_errno($this->fp) === 23 || curl_errno($this->fp) === 61) {
122 curl_setopt($this->fp, CURLOPT_ENCODING, 'none');
123 $response = curl_exec($this->fp);
126 $this->process_response($response, $options);
127 curl_close($this->fp);
128 return $this->headers;
132 * Send multiple requests simultaneously
134 * @param array $requests Request data
135 * @param array $options Global options
136 * @return array Array of Requests_Response objects (may contain Requests_Exception or string responses as well)
138 public function request_multiple($requests, $options) {
139 $multihandle = curl_multi_init();
140 $subrequests = array();
141 $subhandles = array();
143 $class = get_class($this);
144 foreach ($requests as $id => $request) {
145 $subrequests[$id] = new $class();
146 $subhandles[$id] = $subrequests[$id]->get_subrequest_handle($request['url'], $request['headers'], $request['data'], $request['options']);
147 $request['options']['hooks']->dispatch('curl.before_multi_add', array(&$subhandles[$id]));
148 curl_multi_add_handle($multihandle, $subhandles[$id]);
152 $responses = array();
154 $request['options']['hooks']->dispatch('curl.before_multi_exec', array(&$multihandle));
160 $status = curl_multi_exec($multihandle, $active);
162 while ($status === CURLM_CALL_MULTI_PERFORM);
164 $to_process = array();
166 // Read the information as needed
167 while ($done = curl_multi_info_read($multihandle)) {
168 $key = array_search($done['handle'], $subhandles, true);
169 if (!isset($to_process[$key])) {
170 $to_process[$key] = $done;
174 // Parse the finished requests before we start getting the new ones
175 foreach ($to_process as $key => $done) {
176 $options = $requests[$key]['options'];
177 $responses[$key] = $subrequests[$key]->process_response(curl_multi_getcontent($done['handle']), $options);
179 $options['hooks']->dispatch('transport.internal.parse_response', array(&$responses[$key], $requests[$key]));
181 curl_multi_remove_handle($multihandle, $done['handle']);
182 curl_close($done['handle']);
184 if (!is_string($responses[$key])) {
185 $options['hooks']->dispatch('multiple.request.complete', array(&$responses[$key], $key));
190 while ($active || $completed < count($subrequests));
192 $request['options']['hooks']->dispatch('curl.after_multi_exec', array(&$multihandle));
194 curl_multi_close($multihandle);
200 * Get the cURL handle for use in a multi-request
202 * @param string $url URL to request
203 * @param array $headers Associative array of request headers
204 * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
205 * @param array $options Request options, see {@see Requests::response()} for documentation
206 * @return resource Subrequest's cURL handle
208 public function &get_subrequest_handle($url, $headers, $data, $options) {
209 $this->setup_handle($url, $headers, $data, $options);
211 if ($options['filename'] !== false) {
212 $this->stream_handle = fopen($options['filename'], 'wb');
213 curl_setopt($this->fp, CURLOPT_FILE, $this->stream_handle);
220 * Setup the cURL handle for the given data
222 * @param string $url URL to request
223 * @param array $headers Associative array of request headers
224 * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
225 * @param array $options Request options, see {@see Requests::response()} for documentation
227 protected function setup_handle($url, $headers, $data, $options) {
228 $options['hooks']->dispatch('curl.before_request', array(&$this->fp));
230 $headers = Requests::flatten($headers);
231 if (in_array($options['type'], array(Requests::HEAD, Requests::GET, Requests::DELETE)) & !empty($data)) {
232 $url = self::format_get($url, $data);
234 elseif (!empty($data) && !is_string($data)) {
235 $data = http_build_query($data, null, '&');
238 switch ($options['type']) {
240 curl_setopt($this->fp, CURLOPT_POST, true);
241 curl_setopt($this->fp, CURLOPT_POSTFIELDS, $data);
243 case Requests::PATCH:
245 curl_setopt($this->fp, CURLOPT_CUSTOMREQUEST, $options['type']);
246 curl_setopt($this->fp, CURLOPT_POSTFIELDS, $data);
248 case Requests::DELETE:
249 curl_setopt($this->fp, CURLOPT_CUSTOMREQUEST, 'DELETE');
252 curl_setopt($this->fp, CURLOPT_NOBODY, true);
256 if( is_int($options['timeout']) or $this->version < self::CURL_7_16_2 ) {
257 curl_setopt($this->fp, CURLOPT_TIMEOUT, ceil($options['timeout']));
259 curl_setopt($this->fp, CURLOPT_TIMEOUT_MS, round($options['timeout'] * 1000) );
261 if( is_int($options['connect_timeout']) or $this->version < self::CURL_7_16_2 ) {
262 curl_setopt($this->fp, CURLOPT_CONNECTTIMEOUT, ceil($options['connect_timeout']));
264 curl_setopt($this->fp, CURLOPT_CONNECTTIMEOUT_MS, round($options['connect_timeout'] * 1000));
266 curl_setopt($this->fp, CURLOPT_URL, $url);
267 curl_setopt($this->fp, CURLOPT_REFERER, $url);
268 curl_setopt($this->fp, CURLOPT_USERAGENT, $options['useragent']);
269 curl_setopt($this->fp, CURLOPT_HTTPHEADER, $headers);
271 if (true === $options['blocking']) {
272 curl_setopt($this->fp, CURLOPT_HEADERFUNCTION, array(&$this, 'stream_headers'));
276 public function process_response($response, $options) {
277 if ($options['blocking'] === false) {
279 $options['hooks']->dispatch('curl.after_request', array(&$fake_headers));
282 if ($options['filename'] !== false) {
283 fclose($this->stream_handle);
284 $this->headers = trim($this->headers);
287 $this->headers .= $response;
290 if (curl_errno($this->fp)) {
291 throw new Requests_Exception('cURL error ' . curl_errno($this->fp) . ': ' . curl_error($this->fp), 'curlerror', $this->fp);
294 $this->info = curl_getinfo($this->fp);
296 $options['hooks']->dispatch('curl.after_request', array(&$this->headers));
297 return $this->headers;
301 * Collect the headers as they are received
303 * @param resource $handle cURL resource
304 * @param string $headers Header string
305 * @return integer Length of provided header
307 protected function stream_headers($handle, $headers) {
308 // Why do we do this? cURL will send both the final response and any
309 // interim responses, such as a 100 Continue. We don't need that.
310 // (We may want to keep this somewhere just in case)
311 if ($this->done_headers) {
313 $this->done_headers = false;
315 $this->headers .= $headers;
317 if ($headers === "\r\n") {
318 $this->done_headers = true;
320 return strlen($headers);
324 * Format a URL given GET data
327 * @param array|object $data Data to build query using, see {@see http://php.net/http_build_query}
328 * @return string URL with data
330 protected static function format_get($url, $data) {
332 $url_parts = parse_url($url);
333 if (empty($url_parts['query'])) {
334 $query = $url_parts['query'] = '';
337 $query = $url_parts['query'];
340 $query .= '&' . http_build_query($data, null, '&');
341 $query = trim($query, '&');
343 if (empty($url_parts['query'])) {
344 $url .= '?' . $query;
347 $url = str_replace($url_parts['query'], $query, $url);
354 * Whether this transport is valid
356 * @codeCoverageIgnore
357 * @return boolean True if the transport is valid, false otherwise.
359 public static function test($capabilities = array()) {
360 if (!function_exists('curl_init') && !function_exists('curl_exec'))
363 // If needed, check that our installed curl version supports SSL
364 if (isset( $capabilities['ssl'] ) && $capabilities['ssl']) {
365 $curl_version = curl_version();
366 if (!(CURL_VERSION_SSL & $curl_version['features']))