6 * @subpackage Transport
13 * @subpackage Transport
15 class Requests_Transport_cURL implements Requests_Transport {
24 * Information on the current request
26 * @var array cURL information array, see {@see http://php.net/curl_getinfo}
45 * Have we finished the headers yet?
49 protected $done_headers = false;
52 * If streaming to a file, keep the file pointer
56 protected $stream_handle;
61 public function __construct() {
62 $curl = curl_version();
63 $this->version = $curl['version'];
64 $this->fp = curl_init();
66 curl_setopt($this->fp, CURLOPT_HEADER, false);
67 curl_setopt($this->fp, CURLOPT_RETURNTRANSFER, 1);
68 if (version_compare($this->version, '7.10.5', '>=')) {
69 curl_setopt($this->fp, CURLOPT_ENCODING, '');
71 if (version_compare($this->version, '7.19.4', '>=')) {
72 curl_setopt($this->fp, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
79 * @throws Requests_Exception On a cURL error (`curlerror`)
81 * @param string $url URL to request
82 * @param array $headers Associative array of request headers
83 * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
84 * @param array $options Request options, see {@see Requests::response()} for documentation
85 * @return string Raw HTTP result
87 public function request($url, $headers = array(), $data = array(), $options = array()) {
88 $this->setup_handle($url, $headers, $data, $options);
90 $options['hooks']->dispatch('curl.before_send', array(&$this->fp));
92 if ($options['filename'] !== false) {
93 $this->stream_handle = fopen($options['filename'], 'wb');
94 curl_setopt($this->fp, CURLOPT_FILE, $this->stream_handle);
97 if (isset($options['verify'])) {
98 if ($options['verify'] === false) {
99 curl_setopt($this->fp, CURLOPT_SSL_VERIFYHOST, 0);
100 curl_setopt($this->fp, CURLOPT_SSL_VERIFYPEER, 0);
102 } elseif (is_string($options['verify'])) {
103 curl_setopt($this->fp, CURLOPT_CAINFO, $options['verify']);
107 if (isset($options['verifyname']) && $options['verifyname'] === false) {
108 curl_setopt($this->fp, CURLOPT_SSL_VERIFYHOST, 0);
111 $response = curl_exec($this->fp);
113 $options['hooks']->dispatch('curl.after_send', array(&$fake_headers));
115 if (curl_errno($this->fp) === 23 || curl_errno($this->fp) === 61) {
116 curl_setopt($this->fp, CURLOPT_ENCODING, 'none');
117 $response = curl_exec($this->fp);
120 $this->process_response($response, $options);
121 return $this->headers;
125 * Send multiple requests simultaneously
127 * @param array $requests Request data
128 * @param array $options Global options
129 * @return array Array of Requests_Response objects (may contain Requests_Exception or string responses as well)
131 public function request_multiple($requests, $options) {
132 $multihandle = curl_multi_init();
133 $subrequests = array();
134 $subhandles = array();
136 $class = get_class($this);
137 foreach ($requests as $id => $request) {
138 $subrequests[$id] = new $class();
139 $subhandles[$id] = $subrequests[$id]->get_subrequest_handle($request['url'], $request['headers'], $request['data'], $request['options']);
140 $request['options']['hooks']->dispatch('curl.before_multi_add', array(&$subhandles[$id]));
141 curl_multi_add_handle($multihandle, $subhandles[$id]);
145 $responses = array();
147 $request['options']['hooks']->dispatch('curl.before_multi_exec', array(&$multihandle));
153 $status = curl_multi_exec($multihandle, $active);
155 while ($status === CURLM_CALL_MULTI_PERFORM);
157 $to_process = array();
159 // Read the information as needed
160 while ($done = curl_multi_info_read($multihandle)) {
161 $key = array_search($done['handle'], $subhandles, true);
162 if (!isset($to_process[$key])) {
163 $to_process[$key] = $done;
167 // Parse the finished requests before we start getting the new ones
168 foreach ($to_process as $key => $done) {
169 $options = $requests[$key]['options'];
170 $responses[$key] = $subrequests[$key]->process_response(curl_multi_getcontent($done['handle']), $options);
172 $options['hooks']->dispatch('transport.internal.parse_response', array(&$responses[$key], $requests[$key]));
174 curl_multi_remove_handle($multihandle, $done['handle']);
175 curl_close($done['handle']);
177 if (!is_string($responses[$key])) {
178 $options['hooks']->dispatch('multiple.request.complete', array(&$responses[$key], $key));
183 while ($active || $completed < count($subrequests));
185 $request['options']['hooks']->dispatch('curl.after_multi_exec', array(&$multihandle));
187 curl_multi_close($multihandle);
193 * Get the cURL handle for use in a multi-request
195 * @param string $url URL to request
196 * @param array $headers Associative array of request headers
197 * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
198 * @param array $options Request options, see {@see Requests::response()} for documentation
199 * @return resource Subrequest's cURL handle
201 public function &get_subrequest_handle($url, $headers, $data, $options) {
202 $this->setup_handle($url, $headers, $data, $options);
204 if ($options['filename'] !== false) {
205 $this->stream_handle = fopen($options['filename'], 'wb');
206 curl_setopt($this->fp, CURLOPT_FILE, $this->stream_handle);
213 * Setup the cURL handle for the given data
215 * @param string $url URL to request
216 * @param array $headers Associative array of request headers
217 * @param string|array $data Data to send either as the POST body, or as parameters in the URL for a GET/HEAD
218 * @param array $options Request options, see {@see Requests::response()} for documentation
220 protected function setup_handle($url, $headers, $data, $options) {
221 $options['hooks']->dispatch('curl.before_request', array(&$this->fp));
223 $headers = Requests::flatten($headers);
224 if (in_array($options['type'], array(Requests::HEAD, Requests::GET, Requests::DELETE)) & !empty($data)) {
225 $url = self::format_get($url, $data);
227 elseif (!empty($data) && !is_string($data)) {
228 $data = http_build_query($data, null, '&');
231 switch ($options['type']) {
233 curl_setopt($this->fp, CURLOPT_POST, true);
234 curl_setopt($this->fp, CURLOPT_POSTFIELDS, $data);
236 case Requests::PATCH:
238 curl_setopt($this->fp, CURLOPT_CUSTOMREQUEST, $options['type']);
239 curl_setopt($this->fp, CURLOPT_POSTFIELDS, $data);
241 case Requests::DELETE:
242 curl_setopt($this->fp, CURLOPT_CUSTOMREQUEST, 'DELETE');
245 curl_setopt($this->fp, CURLOPT_NOBODY, true);
249 curl_setopt($this->fp, CURLOPT_URL, $url);
250 curl_setopt($this->fp, CURLOPT_TIMEOUT, $options['timeout']);
251 curl_setopt($this->fp, CURLOPT_CONNECTTIMEOUT, $options['timeout']);
252 curl_setopt($this->fp, CURLOPT_REFERER, $url);
253 curl_setopt($this->fp, CURLOPT_USERAGENT, $options['useragent']);
254 curl_setopt($this->fp, CURLOPT_HTTPHEADER, $headers);
256 if (true === $options['blocking']) {
257 curl_setopt($this->fp, CURLOPT_HEADERFUNCTION, array(&$this, 'stream_headers'));
261 public function process_response($response, $options) {
262 if ($options['blocking'] === false) {
263 curl_close($this->fp);
265 $options['hooks']->dispatch('curl.after_request', array(&$fake_headers));
268 if ($options['filename'] !== false) {
269 fclose($this->stream_handle);
270 $this->headers = trim($this->headers);
273 $this->headers .= $response;
276 if (curl_errno($this->fp)) {
277 throw new Requests_Exception('cURL error ' . curl_errno($this->fp) . ': ' . curl_error($this->fp), 'curlerror', $this->fp);
280 $this->info = curl_getinfo($this->fp);
282 curl_close($this->fp);
283 $options['hooks']->dispatch('curl.after_request', array(&$this->headers));
284 return $this->headers;
288 * Collect the headers as they are received
290 * @param resource $handle cURL resource
291 * @param string $headers Header string
292 * @return integer Length of provided header
294 protected function stream_headers($handle, $headers) {
295 // Why do we do this? cURL will send both the final response and any
296 // interim responses, such as a 100 Continue. We don't need that.
297 // (We may want to keep this somewhere just in case)
298 if ($this->done_headers) {
300 $this->done_headers = false;
302 $this->headers .= $headers;
304 if ($headers === "\r\n") {
305 $this->done_headers = true;
307 return strlen($headers);
311 * Format a URL given GET data
314 * @param array|object $data Data to build query using, see {@see http://php.net/http_build_query}
315 * @return string URL with data
317 protected static function format_get($url, $data) {
319 $url_parts = parse_url($url);
320 if (empty($url_parts['query'])) {
321 $query = $url_parts['query'] = '';
324 $query = $url_parts['query'];
327 $query .= '&' . http_build_query($data, null, '&');
328 $query = trim($query, '&');
330 if (empty($url_parts['query'])) {
331 $url .= '?' . $query;
334 $url = str_replace($url_parts['query'], $query, $url);
341 * Whether this transport is valid
343 * @codeCoverageIgnore
344 * @return boolean True if the transport is valid, false otherwise.
346 public static function test() {
347 return (function_exists('curl_init') && function_exists('curl_exec'));