예제 #1
0
 /**
  * Internal workhorse.
  */
 protected function handleDoRequest($uri, $method, $headers, $in = NULL)
 {
     // XXX: I don't like this, but I'm getting bug reports that mistakenly
     // assume this library is broken, when in fact CURL is not installed.
     if (!function_exists('curl_init')) {
         throw new \HPCloud\Exception('The CURL library is not available.');
     }
     // A majority of curl users suggest allowing curl to automatically calculate
     // the content-length header. Ensure that the header is not set in code.
     // http://stackoverflow.com/questions/9152165/php-curl-content-length-und-content-type-wrong
     if (!empty($headers['Content-Length'])) {
         unset($headers['Content-Length']);
     }
     //syslog(LOG_WARNING, "Real Operation: $method $uri");
     //$urlParts = parse_url($uri);
     $opts = array(CURLOPT_USERAGENT => self::HTTP_USER_AGENT . self::HTTP_USER_AGENT_SUFFIX);
     // Write to in-mem handle backed by a temp file.
     $out = fopen('php://temp', 'wb+');
     $headerFile = fopen('php://temp', 'w+');
     $curl = curl_init($uri);
     // Set method
     $this->determineMethod($curl, $method);
     // Set the upload
     $copy = NULL;
     // If we get a string, we send the string data.
     if (is_string($in)) {
         $opts[CURLOPT_POSTFIELDS] = $in;
     } elseif (is_resource($in)) {
         $opts[CURLOPT_INFILE] = $in;
     }
     // Set headers.
     $this->setHeaders($curl, $headers);
     // Get the output.
     $opts[CURLOPT_FILE] = $out;
     // We need to capture the headers, too.
     $opts[CURLOPT_WRITEHEADER] = $headerFile;
     if (Bootstrap::hasConfig('transport.debug')) {
         $debug = Bootstrap::config('transport.debug', NULL);
         $opts[CURLOPT_VERBOSE] = (int) $debug;
     }
     if (Bootstrap::hasConfig('transport.timeout')) {
         $opts[CURLOPT_TIMEOUT] = (int) Bootstrap::config('transport.timeout');
     }
     if (Bootstrap::hasConfig('transport.ssl.verify')) {
         $validate = (bool) Bootstrap::config('transport.ssl.verify', TRUE);
         $opts[CURLOPT_SSL_VERIFYPEER] = $validate;
     }
     // PROXY settings
     $proxyMap = array('proxy' => CURLOPT_PROXY, 'proxy.tunnel' => CURLOPT_HTTPPROXYTUNNEL, 'proxy.auth' => CURLOPT_PROXYAUTH, 'proxy.port' => CURLOPT_PROXYPORT, 'proxy.type' => CURLOPT_PROXYTYPE, 'proxy.userpwd' => CURLOPT_PROXYUSERPWD);
     foreach ($proxyMap as $conf => $opt) {
         if (Bootstrap::hasConfig($conf)) {
             $val = Bootstrap::config($conf);
             $opts[$opt] = $val;
         }
     }
     // Set all of the curl opts and then execute.
     curl_setopt_array($curl, $opts);
     $ret = $this->execCurl($curl);
     $info = curl_getinfo($curl);
     $status = $info['http_code'];
     rewind($headerFile);
     $responseHeaders = $this->fetchHeaders($headerFile);
     fclose($headerFile);
     if (!$ret || $status < 200 || $status > 299) {
         if (empty($responseHeaders)) {
             $err = 'Unknown (non-HTTP) error: ' . $status;
         } else {
             $err = $responseHeaders[0];
         }
         //rewind($out);
         //fwrite(STDERR, stream_get_contents($out));
         Response::failure($status, $err, $info['url'], $method, $info);
     }
     rewind($out);
     // Now we need to build a response.
     $resp = new Response($out, $info, $responseHeaders);
     //curl_close($curl);
     if (is_resource($copy)) {
         fclose($copy);
     }
     return $resp;
 }
예제 #2
0
 /**
  * Internal workhorse.
  */
 protected function handleDoRequest($uri, $method, $headers, $in = NULL)
 {
     // XXX: I don't like this, but I'm getting bug reports that mistakenly
     // assume this library is broken, when in fact CURL is not installed.
     if (!function_exists('curl_init')) {
         throw new \HPCloud\Exception('The CURL library is not available.');
     }
     //syslog(LOG_WARNING, "Real Operation: $method $uri");
     //$urlParts = parse_url($uri);
     $opts = array(CURLOPT_USERAGENT => self::HTTP_USER_AGENT . self::HTTP_USER_AGENT_SUFFIX);
     $uri = str_replace(" ", "%20", $uri);
     // Write to in-mem handle backed by a temp file.
     $out = fopen('php://temp', 'wb+');
     $headerFile = fopen('php://temp', 'w+');
     $curl = curl_init($uri);
     //$curl = $this->curl($uri);
     // Set method
     $this->determineMethod($curl, $method);
     // Set the upload
     $copy = NULL;
     // If we get a string, we send the string
     // data.
     if (is_string($in)) {
         //curl_setopt($curl, CURLOPT_POSTFIELDS, $in);
         $opts[CURLOPT_POSTFIELDS] = $in;
         if (!isset($headers['Content-Length'])) {
             $headers['Content-Length'] = strlen($in);
         }
     } elseif (is_resource($in)) {
         //curl_setopt($curl, CURLOPT_INFILE, $in);
         $opts[CURLOPT_INFILE] = $in;
         // Tell CURL about the content length if we know it.
         if (!empty($headers['Content-Length'])) {
             //curl_setopt($curl, CURLOPT_INFILESIZE, $headers['Content-Length']);
             $opts[CURLOPT_INFILESIZE] = $headers['Content-Length'];
             unset($headers['Content-Length']);
         }
     }
     // Set headers.
     $this->setHeaders($curl, $headers);
     // Get the output.
     //curl_setopt($curl, CURLOPT_FILE, $out);
     $opts[CURLOPT_FILE] = $out;
     //curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     // We need to capture the headers, too.
     //curl_setopt($curl, CURLOPT_WRITEHEADER, $headerFile);
     $opts[CURLOPT_WRITEHEADER] = $headerFile;
     if (Bootstrap::hasConfig('transport.debug')) {
         $debug = Bootstrap::config('transport.debug', NULL);
         //curl_setopt($curl, CURLOPT_VERBOSE, (int) $debug);
         $opts[CURLOPT_VERBOSE] = (int) $debug;
     }
     if (Bootstrap::hasConfig('transport.timeout')) {
         //curl_setopt($curl, CURLOPT_TIMEOUT, (int) Bootstrap::config('transport.timeout'));
         $opts[CURLOPT_TIMEOUT] = (int) Bootstrap::config('transport.timeout');
     }
     if (Bootstrap::hasConfig('transport.ssl.verify')) {
         $validate = (bool) Bootstrap::config('transport.ssl.verify', TRUE);
         //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $validate);
         $opts[CURLOPT_SSL_VERIFYPEER] = $validate;
     }
     // Set all of the curl opts and then execute.
     curl_setopt_array($curl, $opts);
     $ret = curl_exec($curl);
     \AJXP_Logger::debug("CURL {$method} " . $uri);
     //$ret = $this->execCurl($curl);
     $info = curl_getinfo($curl);
     //var_dump($info);
     $status = $info['http_code'];
     //var_dump(fstat($headerFile));
     rewind($headerFile);
     $responseHeaders = $this->fetchHeaders($headerFile);
     fclose($headerFile);
     //var_dump($responseHeaders);
     if (!$ret || $status < 200 || $status > 299 || empty($responseHeaders)) {
         if (empty($responseHeaders)) {
             $err = 'Unknown (non-HTTP) error: ' . $status;
         } else {
             $err = $responseHeaders[0];
         }
         //rewind($out);
         //fwrite(STDERR, stream_get_contents($out));
         Response::failure($status, $err, $info['url'], $method, $info);
     }
     rewind($out);
     // Now we need to build a response.
     $resp = new Response($out, $info, $responseHeaders);
     //curl_close($curl);
     if (is_resource($copy)) {
         fclose($copy);
     }
     return $resp;
 }