Example #1
0
 public function request($method, $url, $data = null, $headers = null)
 {
     $resp = new Response($this, $this->receiver);
     $curl = curl_init();
     $curl_opts = $this->derest->curl_opts;
     if (isset($this->instanceData['curl_opts']) && is_array($this->instanceData['curl_opts'])) {
         $curl_opts += $this->instanceData['curl_opts'];
     }
     $curl_opts[CURLOPT_CUSTOMREQUEST] = $method;
     $curl_opts[CURLOPT_URL] = $this->parseUrl($url);
     $curl_opts[CURLOPT_HEADERFUNCTION] = array($resp, 'receiveHeader');
     $curl_opts[CURLOPT_WRITEFUNCTION] = array($resp, 'receiveData');
     if ($method == 'HEAD') {
         $curl_opts[CURLOPT_NOBODY] = true;
     }
     // headers
     if (!is_array($headers)) {
         $headers = [];
     }
     if (isset($data)) {
         if (is_object($data) && $data instanceof Data) {
             $curl_opts[CURLOPT_POSTFIELDS] = $data->getData($this);
         } elseif (is_array($data) && isset($this->instanceData['data_format']) && $this->instanceData['data_format'] != '') {
             if ($this->instanceData['data_format'] == 'json') {
                 $headers['Accept'] = 'application/json';
                 $headers['Content-Type'] = 'application/json';
                 $enc = json_encode($data);
                 if ($enc === false) {
                     throw new Json_Encode_Exception('Encoding error: ' . $resp->getLastJsonErrorMessage(), $resp->getLastJsonErrorCode());
                 }
                 $curl_opts[CURLOPT_POSTFIELDS] = $enc;
             } else {
                 throw new Data_Encode_Exception('Unknown encoding data format: ' . $this->instanceData['data_format']);
             }
         } else {
             $curl_opts[CURLOPT_POSTFIELDS] = $data;
         }
     }
     if (isset($curl_opts[CURLOPT_POSTFIELDS]) && is_string($curl_opts[CURLOPT_POSTFIELDS])) {
         $headers['Content-Length'] = strlen($curl_opts[CURLOPT_POSTFIELDS]);
     }
     $flatHeaders = [];
     foreach ($headers as $hname => $hvalue) {
         $flatHeaders[] = $hname . ': ' . $hvalue;
     }
     $curl_opts[CURLOPT_HTTPHEADER] = $flatHeaders;
     foreach ($curl_opts as $cname => $cval) {
         curl_setopt($curl, $cname, $cval);
     }
     if (isset($this->logger)) {
         $this->logger->logRequest($this, ['headers' => $headers, 'url' => $curl_opts[CURLOPT_URL], 'method' => $method, 'curl_opts' => $curl_opts]);
     }
     $curl_result = curl_exec($curl);
     if ($curl_result === FALSE) {
         throw new Curl_Exception('Curl error: ' . curl_error($curl));
     }
     $resp->meta = curl_getinfo($curl);
     $resp->receiveDataFinished($curl);
     curl_close($curl);
     if (isset($this->logger)) {
         $this->logger->logResponse($this, $resp);
     }
     if ($this->throw_exceptions && isset($resp->meta)) {
         if ($resp->meta['http_code'] >= 400 && $resp->meta['http_code'] <= 600) {
             $error_info = $this->processError($resp);
             if ($resp->meta['http_code'] >= 400 && $resp->meta['http_code'] <= 499) {
                 throw new ClientError_Exception($error_info->message, $error_info->code, null, $error_info->data);
             } elseif ($resp->meta['http_code'] >= 500 && $resp->meta['http_code'] <= 599) {
                 throw new ServerError_Exception($error_info->message, $error_info->code, null, $error_info->data);
             } elseif (!isset($resp->meta['http_code']) || $resp->meta['http_code'] >= 600) {
                 throw new UnknownResponse_Exception($error_info->message, $error_info->code, null, $error_info->data);
             }
         }
     }
     return $resp;
 }