123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601 |
- <?php
- namespace Dotdigitalgroup\Email\Model\Apiconnector;
- /**
- * Rest class to make cURL requests.
- */
- class Rest
- {
- /**
- * @var string|null
- */
- private $url;
- /**
- * @var string
- */
- private $verb;
- /**
- * @var string|null
- */
- private $requestBody;
- /**
- * @var int
- */
- private $requestLength;
- /**
- * @var string
- */
- private $apiUsername;
- /**
- * @var string
- */
- private $apiPassword;
- /**
- * @var string
- */
- private $acceptType;
- /**
- * @var mixed
- */
- private $responseBody;
- /**
- * @var mixed
- */
- private $responseInfo;
- /**
- * @var string
- */
- private $curlError;
- /**
- * @var \Dotdigitalgroup\Email\Helper\Data
- */
- private $helper;
- /**
- * @var bool
- */
- protected $isNotJson = false;
- /**
- * Rest constructor.
- * @param \Dotdigitalgroup\Email\Helper\Data $data
- * @param int $website
- *
- * @return null
- */
- public function __construct(
- \Dotdigitalgroup\Email\Helper\Data $data,
- $website = 0
- ) {
- $this->helper = $data;
- $this->url = null;
- $this->verb = 'GET';
- $this->requestBody = null;
- $this->requestLength = 0;
- $this->apiUsername = (string)$this->helper->getApiUsername($website);
- $this->apiPassword = (string)$this->helper->getApiPassword($website);
- $this->acceptType = 'application/json';
- $this->responseBody = null;
- $this->responseInfo = null;
- if ($this->requestBody !== null) {
- $this->buildPostBody();
- }
- }
- /**
- * @param mixed $json
- *
- * @return string
- *
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- */
- private function prettyPrint($json)
- {
- $result = '';
- $level = 0;
- $prevChar = '';
- $inQuotes = false;
- $endsLineLevel = null;
- $jsonLength = strlen($json);
- for ($i = 0; $i < $jsonLength; ++$i) {
- $char = $json[$i];
- $newLIneLevel = null;
- $post = '';
- if ($endsLineLevel !== null) {
- $newLIneLevel = $endsLineLevel;
- $endsLineLevel = null;
- }
- if ($char === '"' && $prevChar != '\\') {
- $inQuotes = !$inQuotes;
- } elseif (!$inQuotes) {
- switch ($char) {
- case '}':
- case ']':
- $level--;
- $endsLineLevel = null;
- $newLIneLevel = $level;
- break;
- case '{':
- case '[':
- $level++;
- break;
- case ',':
- $endsLineLevel = $level;
- break;
- case ':':
- $post = ' ';
- break;
- case ' ':
- case "\t":
- case "\n":
- case "\r":
- $char = '';
- $endsLineLevel = $newLIneLevel;
- $newLIneLevel = null;
- break;
- }
- }
- if ($newLIneLevel !== null) {
- $result .= "\n" . str_repeat("\t", $newLIneLevel);
- }
- $result .= $char . $post;
- $prevChar = $char;
- }
- return $result;
- }
- /**
- * Returns the object as JSON.
- *
- * @param bool $pretty
- *
- * @return string
- */
- public function toJSON($pretty = false)
- {
- if (!$pretty) {
- return json_encode($this->expose());
- } else {
- return $this->prettyPrint(json_encode($this->expose()));
- }
- }
- /**
- * Exposes the class as an array of objects.
- *
- * @return array
- */
- public function expose()
- {
- return get_object_vars($this);
- }
- /**
- * Reset the client.
- *
- * @return $this
- */
- public function flush()
- {
- $this->apiUsername = '';
- $this->apiPassword = '';
- $this->requestBody = null;
- $this->requestLength = 0;
- $this->verb = 'GET';
- $this->responseBody = null;
- $this->responseInfo = null;
- return $this;
- }
- /**
- * @throws \Exception
- *
- * @return mixed
- */
- public function execute()
- {
- $ch = curl_init();
- $this->setAuth($ch);
- try {
- switch (strtoupper($this->verb)) {
- case 'GET':
- $this->executeGet($ch);
- break;
- case 'POST':
- $this->executePost($ch);
- break;
- case 'PUT':
- $this->executePut($ch);
- break;
- case 'DELETE':
- $this->executeDelete($ch);
- break;
- default:
- throw new \InvalidArgumentException(
- 'Current verb (' . $this->verb
- . ') is an invalid REST verb.'
- );
- }
- } catch (\InvalidArgumentException $e) {
- curl_close($ch);
- throw $e;
- } catch (\Exception $e) {
- curl_close($ch);
- throw $e;
- }
- /*
- * check and debug api request total time
- */
- $this->processDebugApi();
- return $this->responseBody;
- }
- /**
- * @return void
- */
- private function processDebugApi()
- {
- if ($this->helper->isDebugEnabled()) {
- $info = $this->getResponseInfo();
- //the response info data is set
- if (isset($info['url']) && isset($info['total_time'])) {
- $url = $info['url'];
- $time = $info['total_time'];
- $totalTime = sprintf(' time : %g sec', $time);
- $check = $this->helper->getApiResponseTimeLimit();
- $limit = ($check) ? $check : '2';
- $message = $this->verb . ', ' . $url . $totalTime;
- //check for slow queries
- if ($time > $limit) {
- //log the slow queries
- $this->helper->log($message);
- }
- }
- }
- }
- /**
- * Post data.
- *
- * @param null $data
- *
- * @return $this
- */
- public function buildPostBody($data = null)
- {
- $this->requestBody = json_encode($data);
- return $this;
- }
- /**
- * Execute curl get request.
- *
- * @param mixed $ch
- *
- * @return null
- */
- private function executeGet($ch)
- {
- $this->doExecute($ch);
- }
- /**
- * Execute post request.
- *
- * @param mixed $ch
- *
- * @return null
- */
- private function executePost($ch)
- {
- if (!is_string($this->requestBody)) {
- $this->buildPostBody();
- }
- curl_setopt($ch, CURLOPT_POSTFIELDS, $this->requestBody);
- curl_setopt($ch, CURLOPT_POST, true);
- $this->doExecute($ch);
- }
- /**
- * Post from the file.
- *
- * @param mixed $filename
- *
- * @return null
- */
- public function buildPostBodyFromFile($filename)
- {
- $this->requestBody = [
- 'file' => '@' . $filename,
- ];
- }
- /**
- * Execute put.
- *
- * @param mixed $ch
- *
- * @return null
- */
- private function executePut($ch)
- {
- if (!is_string($this->requestBody)) {
- $this->buildPostBody();
- }
- $this->requestLength = strlen($this->requestBody);
- $fh = fopen('php://memory', 'rw');
- fwrite($fh, $this->requestBody);
- rewind($fh);
- curl_setopt($ch, CURLOPT_INFILE, $fh);
- curl_setopt($ch, CURLOPT_INFILESIZE, $this->requestLength);
- curl_setopt($ch, CURLOPT_PUT, true);
- $this->doExecute($ch);
- fclose($fh);
- }
- /**
- * Ececute delete.
- *
- * @param mixed $ch
- *
- * @return null
- */
- private function executeDelete($ch)
- {
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
- $this->doExecute($ch);
- }
- /**
- * Execute request.
- *
- * @param mixed $ch
- *
- * @return null
- */
- private function doExecute(&$ch)
- {
- $this->setCurlOpts($ch);
- if ($this->isNotJson) {
- $this->responseBody = curl_exec($ch);
- } else {
- $this->responseBody = json_decode(curl_exec($ch));
- }
- $this->responseInfo = curl_getinfo($ch);
- //if curl error found
- if (curl_errno($ch)) {
- //save the error
- $this->curlError = curl_error($ch);
- }
- curl_close($ch);
- }
- /**
- * Curl options.
- *
- * @param mixed $ch
- *
- * @return null
- */
- private function setCurlOpts(&$ch)
- {
- curl_setopt($ch, CURLOPT_TIMEOUT, 60);
- curl_setopt($ch, CURLOPT_URL, $this->url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt(
- $ch,
- CURLOPT_HTTPHEADER,
- [
- 'Accept: ' . $this->acceptType,
- 'Content-Type: application/json',
- ]
- );
- }
- /**
- * Basic auth.
- *
- * @param mixed $ch
- *
- * @return null
- */
- private function setAuth(&$ch)
- {
- if ($this->apiUsername !== null && $this->apiPassword !== null) {
- curl_setopt($ch, CURLAUTH_BASIC, CURLAUTH_DIGEST);
- curl_setopt(
- $ch,
- CURLOPT_USERPWD,
- $this->apiUsername . ':' . $this->apiPassword
- );
- }
- }
- /**
- * Get accept type.
- *
- * @return string
- */
- public function getAcceptType()
- {
- return $this->acceptType;
- }
- /**
- * Set accept type.
- *
- * @param mixed $acceptType
- *
- * @return null
- */
- public function setAcceptType($acceptType)
- {
- $this->acceptType = $acceptType;
- }
- /**
- * Get api username.
- *
- * @return string
- */
- public function getApiUsername()
- {
- return $this->apiUsername;
- }
- /**
- * Set api username.
- *
- * @param mixed $apiUsername
- *
- * @return $this
- */
- public function setApiUsername($apiUsername)
- {
- $this->apiUsername = trim($apiUsername);
- return $this;
- }
- /**
- * Get api password.
- *
- * @return string
- */
- public function getApiPassword()
- {
- return $this->apiPassword;
- }
- /**
- * Set api password.
- *
- * @param mixed $apiPassword
- *
- * @return $this
- */
- public function setApiPassword($apiPassword)
- {
- $this->apiPassword = trim($apiPassword);
- return $this;
- }
- /**
- * Get response body.
- *
- * @return string/object
- */
- public function getResponseBody()
- {
- return $this->responseBody;
- }
- /**
- * Get response info.
- *
- * @return mixed
- */
- private function getResponseInfo()
- {
- return $this->responseInfo;
- }
- /**
- * Get url.
- *
- * @return string
- */
- public function getUrl()
- {
- return $this->url;
- }
- /**
- * Set url.
- *
- * @param mixed $url
- *
- * @return $this
- */
- public function setUrl($url)
- {
- $this->url = $url;
- return $this;
- }
- /**
- * get the verb.
- *
- * @return string
- */
- public function getVerb()
- {
- return $this->verb;
- }
- /**
- * Set the verb.
- *
- * @param mixed $verb
- *
- * @return $this
- */
- public function setVerb($verb)
- {
- $this->verb = $verb;
- return $this;
- }
- /**
- * @return mixed
- */
- public function getCurlError()
- {
- //if curl error
- if (!empty($this->curlError)) {
- //log curl error
- $message = 'CURL ERROR ' . $this->curlError;
- $this->helper->log($message);
- return $this->curlError;
- }
- return false;
- }
- }
|