Exemplo n.º 1
0
 protected function requestImpl($http_method = "GET", $endpoint, $query, $params, $post_field = array(), Request $request)
 {
     //$curl = curl_copy_handle($this->curl);
     $curl = $this->curl;
     $url = $endpoint . $query;
     if (!empty($params)) {
         $url .= "?" . http_build_query($params);
     }
     curl_setopt($curl, CURLOPT_URL, $url);
     //curl_setopt($curl, CURLINFO_HEADER_OUT, true);
     $headers = array();
     foreach ($request->getHeaders() as $key => $value) {
         if ($key == "Content-Length") {
             /* Note: curl don't need content length as calculate itself */
             continue;
         }
         $headers[] = sprintf("%s: %s", $key, $value);
     }
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     if ($http_method == "POST") {
         curl_setopt($curl, CURLOPT_POST, 1);
         curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getContentBody());
     } else {
         if ($http_method == "GET") {
             curl_setopt($curl, CURLOPT_HTTPGET, 1);
         } else {
             if ($http_method == 'PUT') {
                 curl_setopt($curl, CURLOPT_PUT, 1);
                 curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getContentBody());
             } else {
                 if ($http_method == 'DELETE') {
                     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
                     curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getContentBody());
                 } else {
                     throw new \Exception("unsupported httpd method: " . $http_method);
                 }
             }
         }
     }
     $response = curl_exec($curl);
     $info = curl_getinfo($curl);
     $header = substr($response, 0, $info['header_size']);
     $headers = array();
     foreach (preg_split("/\r?\n/", trim($header)) as $line) {
         if (!isset($headers['HTTP_CODE'])) {
             list($version, $status, $status) = explode(" ", $line, 3);
             $headers['HTTP_CODE'] = $status;
         } else {
             @(list($key, $value) = explode(":", $line, 2));
             $headers[$key] = trim($value);
         }
     }
     $output = substr($response, $info['header_size']);
     return array($headers, $output);
 }