29 use \HPCloud\Bootstrap;
70 if (!empty($this->multi)) {
71 curl_multi_close($this->multi);
85 public function doRequest($uri, $method =
'GET', $headers = array(), $body = NULL) {
92 if ($method ==
'POST') {
97 $in = fopen(
'php://temp',
'wr', FALSE);
98 fwrite($in, $body, strlen($body));
108 if (is_string($resource)) {
109 $in = open($resource,
'rb', FALSE);
118 $in = fopen(
'php://temp',
'rb+');
119 stream_copy_to_stream($resource, $in);
132 if (!function_exists(
'curl_init')) {
133 throw new \HPCloud\Exception(
'The CURL library is not available.');
140 CURLOPT_USERAGENT => self::HTTP_USER_AGENT . self::HTTP_USER_AGENT_SUFFIX,
165 $out = fopen(
'php://temp',
'wb+');
166 $headerFile = fopen(
'php://temp',
'w+');
168 $curl = curl_init($uri);
179 if (is_string($in)) {
181 $opts[CURLOPT_POSTFIELDS] = $in;
182 if (!isset($headers[
'Content-Length'])) {
183 $headers[
'Content-Length'] = strlen($in);
188 elseif (is_resource($in)) {
190 $opts[CURLOPT_INFILE] = $in;
193 if (!empty($headers[
'Content-Length'])) {
195 $opts[CURLOPT_INFILESIZE] = $headers[
'Content-Length'];
196 unset($headers[
'Content-Length']);
205 $opts[CURLOPT_FILE] = $out;
209 $opts[CURLOPT_WRITEHEADER] = $headerFile;
215 $opts[CURLOPT_VERBOSE] = (int) $debug;
226 $opts[CURLOPT_SSL_VERIFYPEER] = $validate;
231 'proxy' => CURLOPT_PROXY,
232 'proxy.tunnel' => CURLOPT_HTTPPROXYTUNNEL,
233 'proxy.auth' => CURLOPT_PROXYAUTH,
234 'proxy.port' => CURLOPT_PROXYPORT,
235 'proxy.type' => CURLOPT_PROXYTYPE,
236 'proxy.userpwd' => CURLOPT_PROXYUSERPWD,
238 foreach ($proxyMap as $conf => $opt) {
247 curl_setopt_array($curl, $opts);
249 $info = curl_getinfo($curl);
250 $status = $info[
'http_code'];
256 if (!$ret || $status < 200 || $status > 299) {
257 if (empty($responseHeaders)) {
258 $err =
'Unknown (non-HTTP) error: ' . $status;
261 $err = $responseHeaders[0];
265 Response::failure($status, $err, $info[
'url'], $method, $info);
271 $resp =
new Response($out, $info, $responseHeaders);
274 if (is_resource($copy)) {
302 if (empty($this->multi)) {
303 $multi = curl_multi_init();
311 curl_multi_add_handle(
$multi, $handle);
316 $ret = curl_multi_exec(
$multi, $active);
317 }
while ($ret == CURLM_CALL_MULTI_PERFORM);
319 while ($active && $ret == CURLM_OK) {
320 if (curl_multi_select(
$multi) != -1) {
322 $mrc = curl_multi_exec(
$multi, $active);
323 }
while ($mrc == CURLM_CALL_MULTI_PERFORM);
327 curl_multi_remove_handle(
$multi, $handle);
348 while ($header = fgets(
$file)) {
349 $header = trim($header);
350 if ($header ==
'HTTP/1.1 100 Continue') {
354 if (!empty($header)) {
375 $method = strtoupper($method);
379 curl_setopt($curl, CURLOPT_HTTPGET, TRUE);
382 curl_setopt($curl, CURLOPT_NOBODY, TRUE);
388 curl_setopt($curl, CURLOPT_PUT, TRUE);
397 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
406 foreach ($headers as
$name => $value) {
407 $buffer[] = sprintf($format,
$name, $value);
410 curl_setopt($curl, CURLOPT_HTTPHEADER, $buffer);