Source of file RestClient.php
Size: 3,452 Bytes - Last Modified: 2021-12-23T10:20:09+00:00
/var/www/docs.ssmods.com/process/src/src/Ctct/Util/RestClient.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 | <?php namespace Ctct\Util; use Ctct\Exceptions\CTCTException; use Ctct\Util\RestClientInterface; use Ctct\Util\CurlResponse; /** * Wrapper for curl HTTP request * * @package Util * @author Constant Contact */ class RestClient implements RestClientInterface { /** * Make an Http GET request * @param $url - request url * @param array $headers - array of all http headers to send * @return array - array of the response body, http info, and error (if one exists) */ public function get($url, array $headers) { return self::httpRequest($url, "GET", $headers); } /** * Make an Http POST request * @param $url - request url * @param array $headers - array of all http headers to send * @param $data - data to send with request * @return array - array of the response body, http info, and error (if one exists) */ public function post($url, array $headers = array(), $data = null) { return self::httpRequest($url, "POST", $headers, $data); } /** * Make an Http PUT request * @param $url - request url * @param array $headers - array of all http headers to send * @param $data - data to send with request * @return array - array of the response body, http info, and error (if one exists) */ public function put($url, array $headers = array(), $data = null) { return self::httpRequest($url, "PUT", $headers, $data); } /** * Make an Http DELETE request * @param $url - request url * @param array $headers - array of all http headers to send * @param $data - data to send with request * @return array - array of the response body, http info, and error (if one exists) */ public function delete($url, array $headers = array()) { return self::httpRequest($url, "DELETE", $headers); } /** * Make an Http request * @param $url - request url * @param array $headers - array of all http headers to send * @param $data - data to send with the request * @throws CTCTException - if any errors are contained in the returned payload * @return CurlResponse */ 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.0"); 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; } } |