Api.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace Cloudflare;
  3. use Cloudflare\Exception\AuthenticationException;
  4. use Cloudflare\Exception\UnauthorizedException;
  5. /**
  6. * CloudFlare API wrapper
  7. *
  8. * A work in progress library for the Cloudflare API. The documentation for the API can be found at https://www.cloudflare.com/docs/.
  9. *
  10. * @author James Bell <james@james-bell.co.uk>
  11. *
  12. * @version 1
  13. */
  14. class Api
  15. {
  16. /**
  17. * Holds the provided email address for API authentication
  18. *
  19. * @var string
  20. */
  21. public $email;
  22. /**
  23. * Holds the provided auth_key for API authentication
  24. *
  25. * @var string
  26. */
  27. public $auth_key;
  28. /**
  29. * Holds the curl options
  30. *
  31. * @var array
  32. */
  33. public $curl_options;
  34. /**
  35. * Make a new instance of the API client
  36. * This can be done via providing the email address and api key as seperate parameters
  37. * or by passing in an already instantiated object from which the details will be extracted
  38. */
  39. public function __construct()
  40. {
  41. $num_args = func_num_args();
  42. if ($num_args === 1) {
  43. $parameters = func_get_args();
  44. $client = $parameters[0];
  45. $this->email = $client->email;
  46. $this->auth_key = $client->auth_key;
  47. $this->curl_options = $client->curl_options;
  48. } elseif ($num_args === 2) {
  49. $parameters = func_get_args();
  50. $this->email = $parameters[0];
  51. $this->auth_key = $parameters[1];
  52. }
  53. }
  54. /**
  55. * Setter to allow the setting of the email address
  56. *
  57. * @param string $email The email address associated with the Cloudflare account
  58. */
  59. public function setEmail($email)
  60. {
  61. $this->email = $email;
  62. }
  63. /**
  64. * Setter to allow the setting of the Authentication Key
  65. *
  66. * @param string $token Authentication key, this can be retrieve from the 'My Account' section of the Cloudflare account
  67. */
  68. public function setAuthKey($token)
  69. {
  70. $this->auth_key = $token;
  71. }
  72. /**
  73. * Setter to allow the adding / changing of the Curl options that will be used within the HTTP requests
  74. *
  75. * @param int $key The CURLOPT_XXX option to set e.g. CURLOPT_TIMEOUT
  76. * @param mixed $value The value to be set on option e.g. 10
  77. */
  78. public function setCurlOption($key, $value)
  79. {
  80. $this->curl_options[$key] = $value;
  81. }
  82. /**
  83. * API call method for sending requests using GET
  84. *
  85. * @param string $path Path of the endpoint
  86. * @param array|null $data Data to be sent along with the request
  87. *
  88. * @return mixed
  89. */
  90. public function get($path, array $data = null)
  91. {
  92. return $this->request($path, $data, 'get');
  93. }
  94. /**
  95. * API call method for sending requests using POST
  96. *
  97. * @param string $path Path of the endpoint
  98. * @param array|null $data Data to be sent along with the request
  99. *
  100. * @return mixed
  101. */
  102. public function post($path, array $data = null)
  103. {
  104. return $this->request($path, $data, 'post');
  105. }
  106. /**
  107. * API call method for sending requests using PUT
  108. *
  109. * @param string $path Path of the endpoint
  110. * @param array|null $data Data to be sent along with the request
  111. *
  112. * @return mixed
  113. */
  114. public function put($path, array $data = null)
  115. {
  116. return $this->request($path, $data, 'put');
  117. }
  118. /**
  119. * API call method for sending requests using DELETE
  120. *
  121. * @param string $path Path of the endpoint
  122. * @param array|null $data Data to be sent along with the request
  123. *
  124. * @return mixed
  125. */
  126. public function delete($path, array $data = null)
  127. {
  128. return $this->request($path, $data, 'delete');
  129. }
  130. /**
  131. * API call method for sending requests using PATCH
  132. *
  133. * @param string $path Path of the endpoint
  134. * @param array|null $data Data to be sent along with the request
  135. *
  136. * @return mixed
  137. */
  138. public function patch($path, array $data = null)
  139. {
  140. return $this->request($path, $data, 'patch');
  141. }
  142. /**
  143. * @codeCoverageIgnore
  144. *
  145. * API call method for sending requests using GET, POST, PUT, DELETE OR PATCH
  146. *
  147. * @param string $path Path of the endpoint
  148. * @param array|null $data Data to be sent along with the request
  149. * @param string|null $method Type of method that should be used ('GET', 'POST', 'PUT', 'DELETE', 'PATCH')
  150. *
  151. * @return mixed
  152. */
  153. protected function request($path, array $data = null, $method = null)
  154. {
  155. if (!isset($this->email, $this->auth_key) || false === filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
  156. throw new AuthenticationException('Authentication information must be provided');
  157. }
  158. $data = (is_null($data) ? [] : $data);
  159. $method = (is_null($method) ? 'get' : $method);
  160. //Removes null entries
  161. $data = array_filter($data, function ($val) {
  162. return !is_null($val);
  163. });
  164. $url = 'https://api.cloudflare.com/client/v4/'.$path;
  165. $default_curl_options = [
  166. CURLOPT_VERBOSE => false,
  167. CURLOPT_FORBID_REUSE => true,
  168. CURLOPT_RETURNTRANSFER => 1,
  169. CURLOPT_HEADER => false,
  170. CURLOPT_TIMEOUT => 30,
  171. CURLOPT_SSL_VERIFYPEER => true
  172. ];
  173. $curl_options = $default_curl_options;
  174. if (isset($this->curl_options) && is_array($this->curl_options)) {
  175. $curl_options = array_replace($default_curl_options, $this->curl_options);
  176. }
  177. $headers = ["X-Auth-Email: {$this->email}", "X-Auth-Key: {$this->auth_key}", "User-Agent: {__FILE__}"];
  178. $ch = curl_init();
  179. curl_setopt_array($ch, $curl_options);
  180. $headers[] = 'Content-type: application/json';
  181. $json_data = json_encode($data);
  182. if ($method === 'post') {
  183. curl_setopt($ch, CURLOPT_POST, true);
  184. curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
  185. } elseif ($method === 'put') {
  186. curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
  187. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  188. } elseif ($method === 'delete') {
  189. curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
  190. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  191. } elseif ($method === 'patch') {
  192. curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
  193. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
  194. } else {
  195. $url .= '?'.http_build_query($data);
  196. }
  197. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  198. curl_setopt($ch, CURLOPT_URL, $url);
  199. $http_result = curl_exec($ch);
  200. $error = curl_error($ch);
  201. $information = curl_getinfo($ch);
  202. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  203. if (in_array($http_code, [401, 403])) {
  204. throw new UnauthorizedException('You do not have permission to perform this request');
  205. }
  206. $response = json_decode($http_result);
  207. curl_close($ch);
  208. if ($response->success !== true) {
  209. $response->error = $error;
  210. $response->http_code = $http_code;
  211. $response->method = $method;
  212. $response->information = $information;
  213. }
  214. return $response;
  215. }
  216. }