/src/CoreApi.php

https://gitlab.com/U-Solutions/core-api · PHP · 334 lines · 152 code · 52 blank · 130 comment · 10 complexity · 3890582ec183b108d09d15de99f2e37e MD5 · raw file

  1. <?php
  2. namespace usolutions\core\api;
  3. use Exception;
  4. /**
  5. * Class CoreApi
  6. * @todo delete apiName & apiPassword
  7. * @package usolutions\core\api
  8. */
  9. class CoreApi
  10. {
  11. /**
  12. * Support methods
  13. */
  14. const METHOD_GET = 'get';
  15. const METHOD_POST = 'post';
  16. const METHOD_PUT = 'put';
  17. const METHOD_DELETE = 'delete';
  18. /**
  19. * @var string API URL
  20. */
  21. public $server;
  22. /**
  23. * @var string API name
  24. */
  25. public $apiName;
  26. /**
  27. * @var string API password
  28. */
  29. public $apiPass;
  30. /**
  31. * @var string
  32. */
  33. public $apiToken;
  34. /**
  35. * Timeout on request
  36. * @var integer
  37. */
  38. public $timeout = 20;
  39. /**
  40. * @var boolean
  41. */
  42. public $verifySSL = false;
  43. /**
  44. * @var integer
  45. */
  46. public $verifyHost = 2;
  47. /**
  48. * @var string
  49. */
  50. private $_method;
  51. /**
  52. * @var string
  53. */
  54. private $_request;
  55. /**
  56. * @var array
  57. */
  58. private $_data;
  59. /**
  60. * @var string Content language
  61. */
  62. private $_language = 'en-US';
  63. /**
  64. * Make get request
  65. * @param string $request API request
  66. * @param array $data Request params
  67. * @return string
  68. * @throws Exception
  69. */
  70. public function get($request, $data = [])
  71. {
  72. $this->_method = self::METHOD_GET;
  73. if (!empty($data)) {
  74. $request .= '?' . http_build_query($data);
  75. }
  76. $this->_request = $request;
  77. return $this->send();
  78. }
  79. /**
  80. * Make post request
  81. * @param string $request API request
  82. * @param array $data Request params
  83. * @return string
  84. * @throws Exception
  85. */
  86. public function post($request, $data)
  87. {
  88. $this->_method = self::METHOD_POST;
  89. $this->_data = $data;
  90. $this->_request = $request;
  91. return $this->send();
  92. }
  93. /**
  94. * Make put request
  95. * @param string $request API request
  96. * @param array $data Request params
  97. * @return string
  98. * @throws Exception
  99. */
  100. public function put($request, $data)
  101. {
  102. $this->_method = self::METHOD_PUT;
  103. $this->_data = $data;
  104. $this->_request = $request;
  105. return $this->send();
  106. }
  107. /**
  108. * Make delete request
  109. * @param string $request API request
  110. * @return string
  111. * @throws Exception
  112. */
  113. public function delete($request)
  114. {
  115. $this->_method = self::METHOD_DELETE;
  116. $this->_request = $request;
  117. return $this->send();
  118. }
  119. /**
  120. * Send request
  121. * @return object
  122. * @throws Exception
  123. */
  124. public function send()
  125. {
  126. if (!function_exists('curl_version')) {
  127. throw new Exception('To work correctly, you need to install cURL library - https://php.net/curl');
  128. }
  129. $ch = curl_init($this->server . '/' . $this->_request);
  130. curl_setopt($ch, CURLOPT_HEADER, true);
  131. curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json', 'Accept-Language: ' . $this->_language]);
  132. $ch = $this->setCurlMethod($ch);
  133. $ch = $this->setCurlPostData($ch);
  134. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  135. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verifySSL);
  136. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->verifyHost);
  137. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  138. if ($this->apiToken !== null) {
  139. curl_setopt($ch, CURLOPT_USERPWD, $this->apiToken . ':');
  140. } else {
  141. curl_setopt($ch, CURLOPT_USERPWD, $this->apiName . ':' . $this->apiPass);
  142. }
  143. curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  144. $response = curl_exec($ch);
  145. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  146. $curlInfo = curl_getinfo($ch);
  147. $curl_err = curl_error($ch);
  148. if ($response === false) {
  149. curl_close($ch);
  150. throw new Exception(var_export($curl_err, 1));
  151. }
  152. if (!in_array($httpCode, $this->getSuccessHttpCodeList())) {
  153. curl_close($ch);
  154. throw new Exception(var_export($response, 1) . "\n" . var_export($curl_err, 1) . "\n" . var_export($curlInfo, 1));
  155. }
  156. $response = trim(substr($response, $curlInfo['header_size']));
  157. curl_close($ch);
  158. return $response;
  159. }
  160. /**
  161. * Set content language
  162. * @param string $language Language
  163. * @return $this
  164. */
  165. public function setLanguage($language)
  166. {
  167. $this->_language = $language;
  168. return $this;
  169. }
  170. /**
  171. * Set access token
  172. * @param string $token
  173. * @return $this
  174. */
  175. public function setAuthToken($token)
  176. {
  177. $this->apiToken = $token;
  178. return $this;
  179. }
  180. /**
  181. * Set params for base authentication
  182. * @param string $apiName
  183. * @param string $apiPass
  184. * @return $this
  185. */
  186. public function setAuthParams($apiName, $apiPass)
  187. {
  188. $this->apiName = $apiName;
  189. $this->apiPass = $apiPass;
  190. return $this;
  191. }
  192. /**
  193. * Get success HTTP codes
  194. * @return array
  195. */
  196. protected function getSuccessHttpCodeList()
  197. {
  198. return [200, 201, 204, 302, 401, 403, 404, 405, 422];
  199. }
  200. /**
  201. * Set POST data
  202. * @param resource $ch
  203. * @return resource
  204. * @return mixed
  205. */
  206. protected function setCurlPostData($ch)
  207. {
  208. if (!empty($this->_data)) {
  209. if ($this->_method === self::METHOD_PUT) {
  210. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->_data));
  211. } else {
  212. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_data);
  213. }
  214. }
  215. return $ch;
  216. }
  217. /**
  218. * Set CURL method
  219. * @param resource $ch
  220. * @return resource
  221. * @throws Exception
  222. */
  223. protected function setCurlMethod($ch)
  224. {
  225. if (!in_array($this->_method, array_keys($this->methodMap()))) {
  226. throw new Exception();
  227. }
  228. return call_user_func_array([$this, $this->methodMap()[$this->_method]], [$ch]);
  229. }
  230. /**
  231. * Get methods map
  232. * @return array
  233. */
  234. protected function methodMap()
  235. {
  236. return [
  237. self::METHOD_GET => 'setCurlGet',
  238. self::METHOD_POST => 'setCurlPost',
  239. self::METHOD_PUT => 'setCurlPut',
  240. self::METHOD_DELETE => 'setCurlDelete',
  241. ];
  242. }
  243. /**
  244. * Create GET request
  245. * @param resource $ch
  246. * @return resource
  247. */
  248. protected function setCurlGet($ch)
  249. {
  250. return $ch;
  251. }
  252. /**
  253. * Create POST request
  254. * @param resource $ch
  255. * @return resource
  256. */
  257. protected function setCurlPost($ch)
  258. {
  259. curl_setopt($ch, CURLOPT_POST, true);
  260. return $ch;
  261. }
  262. /**
  263. * Create PUT request
  264. * @param resource $ch
  265. * @return resource
  266. */
  267. protected function setCurlPut($ch)
  268. {
  269. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  270. return $ch;
  271. }
  272. /**
  273. * Create DELETE request
  274. * @param resource $ch
  275. * @return resource
  276. */
  277. protected function setCurlDelete($ch)
  278. {
  279. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  280. return $ch;
  281. }
  282. }