setParams($params) ->setHostUrl($params); } protected function setParams(array $params) { if (!$params['login'] || !$params['password']) { throw new \Exception('Login details are insufficient'); } $this->login = $params['login']; $this->password = $params['password']; $this->httpauth = $params['httpauth']; return $this; } protected function setHostUrl(array $params) { if (!$params['hostname'] || !$params['port']) { throw new \Exception('Server details are insufficient'); } $hostnameParts = $this->prepareHostnameParts($params['hostname']); $protocol = $params['ssl'] == 'on' ? 'https://' : 'http://'; $this->hostUrl = $protocol . $hostnameParts['firstPart'] . ':' . $params['port'] . $hostnameParts['secondPart']; // API ver.2 return $this; } protected function prepareHostnameParts($hostname) { $hostnameParts = explode('/', $hostname); // for incase of such hostnames simpledns.com/api $firstPart = $hostnameParts[0]; unset($hostnameParts[0]); $secondPart = ''; if(count($hostnameParts) > 0) { $secondPart = '/' . implode('/' ,$hostnameParts); } return [ 'firstPart' => $firstPart, 'secondPart' => $secondPart ]; } protected function init() { $this->curl = curl_init(); return $this; } protected function prepareOptions() { curl_setopt($this->curl,CURLOPT_RETURNTRANSFER, true); curl_setopt($this->curl,CURLOPT_SSL_VERIFYPEER, false); curl_setopt($this->curl,CURLOPT_SSL_VERIFYHOST, false); curl_setopt($this->curl,CURLOPT_TIMEOUT, 30); curl_setopt($this->curl,CURLOPT_HTTPAUTH, $this->httpauth); curl_setopt($this->curl,CURLOPT_TIMEOUT, 30); return $this; } protected function setCurlUrl($url) { curl_setopt($this->curl,CURLOPT_URL, $url); return $this; } protected function setCurlMethod($method) { curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, strtoupper($method)); return $this; } protected function setCurlPostFields($data) { curl_setopt($this->curl, CURLOPT_POST, true); curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data); return $this; } protected function setCurlLoginDetails($login, $password) { curl_setopt($this->curl,CURLOPT_USERPWD, $login . ':' . $password); return $this; } public function get($path) { return $this->execute(__FUNCTION__, $path); } public function post($path, $data) { return $this->execute(__FUNCTION__,$path, $data); } public function put($path, $data) { return $this->execute(__FUNCTION__, $path, $data); } public function patch($path, $data) { return $this->execute(__FUNCTION__, $path, $data); } public function delete($path, $data = null) { return $this->execute(__FUNCTION__, $path, $data); } public function isJson($json) { json_decode($json); return json_last_error() == JSON_ERROR_NONE; } protected function execute($method, $path, $data = []) { $url = $this->hostUrl . $path; $this->init() ->prepareOptions() ->setCurlLoginDetails($this->login, $this->password) ->setCurlUrl($url) ->setCurlMethod($method); if ($data) { $this->setCurlPostFields(json_encode($data)); } $result = curl_exec($this->curl); \logModuleCall('dnsmanager', $url, $data, $result); if ($result === false) { throw new \Exception(curl_error($this->curl)); } if($result == '') { $errorInfo = curl_getinfo($this->curl); if(!in_array($errorInfo['http_code'], [200, 204])) { throw new \Exception('HTTP Error: '.$errorInfo['http_code']); } } if ($result && !$this->isJson($result)) { throw new \Exception($result); } return json_decode($result); } }