Exemplo n.º 1
0
 /**
  * Make an HTTP request
  * @param $url - request url
  * @param $method - HTTP method to use for the request
  * @param array $headers - any http headers that should be included with the request
  * @param string|null $data - payload to send with the request, if any
  * @return CurlResponse
  * @throws CTCTException
  */
 private static function httpRequest($url, $method, array $headers = array(), $data = null)
 {
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_HEADER, 0);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($curl, CURLOPT_USERAGENT, "ConstantContact AppConnect PHP Library v1.1");
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
     // add data to send with request if present
     if ($data) {
         curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
     }
     $response = CurlResponse::create(curl_exec($curl), curl_getinfo($curl), curl_error($curl));
     curl_close($curl);
     // check if any errors were returned
     $body = json_decode($response->body, true);
     if (isset($body[0]) && array_key_exists('error_key', $body[0])) {
         $ex = new CtctException($response->body);
         $ex->setCurlInfo($response->info);
         $ex->setErrors($body);
         throw $ex;
     }
     return $response;
 }