Exemple #1
0
 /**
  * @param array $opt
  * @param array $optCustom
  *
  * @return RequestResponse
  * @throws RequestException
  */
 private function process(array $opt, array $optCustom = array())
 {
     $curl = curl_init();
     // add options to retrieve header
     $opt[CURLOPT_HEADER] = 1;
     // merge options
     foreach ($optCustom as $key => $val) {
         $opt[$key] = $optCustom[$key];
     }
     curl_setopt_array($curl, $opt);
     // run request
     $response = curl_exec($curl);
     // parse header
     $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
     $header = $this->parseHttpHeaders(substr($response, 0, $header_size));
     // parse body
     $body = substr($response, $header_size);
     // cache error if any occurs
     $error = curl_error($curl);
     // cache http code
     $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     // url on which we eventually might have ended up (301 redirects)
     $lastUrl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
     curl_close($curl);
     // throw if request failed
     if ($response === false) {
         throw new RequestException($error);
     }
     // --------------------------------------
     $response = new RequestResponse();
     return $response->setHttpCode($httpCode)->setHeader($header)->setBody($body)->setLastUrl($lastUrl);
 }