Example #1
0
 public function request($method, $url, $headers = null, $body = null)
 {
     $handle = $this->getHandle();
     $this->assertRequest($method, $url, $headers, $body);
     if ($headers == null) {
         $headers = array();
     }
     if ($body != null) {
         $headers["Content-Length"] = strlen($body);
     }
     $encoded_headers = array();
     foreach ($headers as $header => $value) {
         $encoded_headers[] = sprintf("%s: %s", $header, $value);
     }
     $options = array(CURLOPT_URL => $url, CURLOPT_CUSTOMREQUEST => strtoupper($method), CURLOPT_POSTFIELDS => $body, CURLOPT_HTTPHEADER => $encoded_headers, CURLOPT_HEADER => true, CURLOPT_CONNECTTIMEOUT => 20, CURLOPT_RETURNTRANSFER => true);
     if (strpos($url, "https://") === 0) {
         $options[CURLOPT_SSL_VERIFYHOST] = $this->_verify_ssl ? self::SSL_VERIFY_NAME_AND_HOST : false;
         $options[CURLOPT_SSL_VERIFYPEER] = $this->_verify_ssl;
         if (defined('CURLOPT_CAINFO') && $this->_ssl_ca_path !== null) {
             $options[CURLOPT_CAINFO] = $this->_ssl_ca_path;
         }
     }
     foreach ($options as $option => $value) {
         curl_setopt($handle, $option, $value);
     }
     $result = curl_exec($handle);
     if ($result === false) {
         throw new TransportException("cURL error: " . curl_error($handle));
     }
     $this->log("Received response: " . $result);
     return Response::fromRaw($result);
 }