Curl.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules\SimpleDNSV8;
  3. use MGModule\DNSManager2\mgLibs\lang;
  4. class Curl
  5. {
  6. private $login;
  7. private $password;
  8. protected $hostUrl;
  9. protected $ssl;
  10. protected $curl;
  11. public function __construct(array $params)
  12. {
  13. $this->setParams($params)
  14. ->setHostUrl($params);
  15. }
  16. protected function setParams(array $params)
  17. {
  18. if (!$params['login'] || !$params['password'])
  19. {
  20. throw new \Exception('Login details are insufficient');
  21. }
  22. $this->login = $params['login'];
  23. $this->password = $params['password'];
  24. $this->httpauth = $params['httpauth'];
  25. return $this;
  26. }
  27. protected function setHostUrl(array $params)
  28. {
  29. if (!$params['hostname'] || !$params['port'])
  30. {
  31. throw new \Exception('Server details are insufficient');
  32. }
  33. $hostnameParts = $this->prepareHostnameParts($params['hostname']);
  34. $protocol = $params['ssl'] == 'on' ? 'https://' : 'http://';
  35. $this->hostUrl = $protocol . $hostnameParts['firstPart'] . ':' . $params['port'] . $hostnameParts['secondPart']; // API ver.2
  36. return $this;
  37. }
  38. protected function prepareHostnameParts($hostname)
  39. {
  40. $hostnameParts = explode('/', $hostname); // for incase of such hostnames simpledns.com/api
  41. $firstPart = $hostnameParts[0];
  42. unset($hostnameParts[0]);
  43. $secondPart = '';
  44. if(count($hostnameParts) > 0)
  45. {
  46. $secondPart = '/' . implode('/' ,$hostnameParts);
  47. }
  48. return [
  49. 'firstPart' => $firstPart,
  50. 'secondPart' => $secondPart
  51. ];
  52. }
  53. protected function init()
  54. {
  55. $this->curl = curl_init();
  56. return $this;
  57. }
  58. protected function prepareOptions()
  59. {
  60. curl_setopt($this->curl,CURLOPT_RETURNTRANSFER, true);
  61. curl_setopt($this->curl,CURLOPT_SSL_VERIFYPEER, false);
  62. curl_setopt($this->curl,CURLOPT_SSL_VERIFYHOST, false);
  63. curl_setopt($this->curl,CURLOPT_TIMEOUT, 30);
  64. curl_setopt($this->curl,CURLOPT_HTTPAUTH, $this->httpauth);
  65. curl_setopt($this->curl,CURLOPT_TIMEOUT, 30);
  66. return $this;
  67. }
  68. protected function setCurlUrl($url)
  69. {
  70. curl_setopt($this->curl,CURLOPT_URL, $url);
  71. return $this;
  72. }
  73. protected function setCurlMethod($method)
  74. {
  75. curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, strtoupper($method));
  76. return $this;
  77. }
  78. protected function setCurlPostFields($data)
  79. {
  80. curl_setopt($this->curl, CURLOPT_POST, true);
  81. curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
  82. return $this;
  83. }
  84. protected function setCurlLoginDetails($login, $password)
  85. {
  86. curl_setopt($this->curl,CURLOPT_USERPWD, $login . ':' . $password);
  87. return $this;
  88. }
  89. public function get($path)
  90. {
  91. return $this->execute(__FUNCTION__, $path);
  92. }
  93. public function post($path, $data)
  94. {
  95. return $this->execute(__FUNCTION__,$path, $data);
  96. }
  97. public function put($path, $data)
  98. {
  99. return $this->execute(__FUNCTION__, $path, $data);
  100. }
  101. public function patch($path, $data)
  102. {
  103. return $this->execute(__FUNCTION__, $path, $data);
  104. }
  105. public function delete($path, $data = null)
  106. {
  107. return $this->execute(__FUNCTION__, $path, $data);
  108. }
  109. public function isJson($json)
  110. {
  111. json_decode($json);
  112. return json_last_error() == JSON_ERROR_NONE;
  113. }
  114. protected function execute($method, $path, $data = [])
  115. {
  116. $url = $this->hostUrl . $path;
  117. $this->init()
  118. ->prepareOptions()
  119. ->setCurlLoginDetails($this->login, $this->password)
  120. ->setCurlUrl($url)
  121. ->setCurlMethod($method);
  122. if ($data)
  123. {
  124. $this->setCurlPostFields(json_encode($data));
  125. }
  126. $result = curl_exec($this->curl);
  127. \logModuleCall('dnsmanager', $url, $data, $result);
  128. if ($result === false)
  129. {
  130. throw new \Exception(curl_error($this->curl));
  131. }
  132. if($result == '')
  133. {
  134. $errorInfo = curl_getinfo($this->curl);
  135. if(!in_array($errorInfo['http_code'], [200, 204]))
  136. {
  137. throw new \Exception('HTTP Error: '.$errorInfo['http_code']);
  138. }
  139. }
  140. if ($result && !$this->isJson($result))
  141. {
  142. throw new \Exception($result);
  143. }
  144. return json_decode($result);
  145. }
  146. }