1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cloudflare; |
4
|
|
|
|
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 <[email protected]> |
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
|
|
|
/** |
24
|
|
|
* Holds the provided auth_key for API authentication |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $auth_key; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Holds the request object to perform API requests |
32
|
|
|
* |
33
|
|
|
* @var RequestInterface |
34
|
|
|
*/ |
35
|
|
|
public $request; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Holds the curl options |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
public $curl_options; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Make a new instance of the API client |
46
|
|
|
* This can be done via providing the email address and api key as seperate parameters |
47
|
|
|
* or by passing in an already instantiated object from which the details will be extracted |
48
|
|
|
*/ |
49
|
|
|
public function __construct() |
50
|
|
|
{ |
51
|
|
|
$num_args = func_num_args(); |
52
|
|
|
if ($num_args === 1) { |
53
|
|
|
$parameters = func_get_args(); |
54
|
|
|
$client = $parameters[0]; |
55
|
|
|
$this->email = $client->email; |
56
|
|
|
$this->auth_key = $client->auth_key; |
57
|
|
|
$this->request = $client->request; |
58
|
|
|
$this->curl_options = $client->curl_options; |
59
|
|
|
} elseif ($num_args === 2) { |
60
|
|
|
$parameters = func_get_args(); |
61
|
|
|
$this->email = $parameters[0]; |
62
|
|
|
$this->auth_key = $parameters[1]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($this->request === null) { |
66
|
|
|
$this->request = new Request(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Setter to allow the setting of the email address |
72
|
|
|
* |
73
|
|
|
* @param string $email The email address associated with the Cloudflare account |
74
|
|
|
*/ |
75
|
|
|
public function setEmail($email) |
76
|
|
|
{ |
77
|
|
|
$this->email = $email; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Setter to allow the setting of the Authentication Key |
82
|
|
|
* |
83
|
|
|
* @param string $token Authentication key, this can be retrieve from the 'My Account' section of the Cloudflare account |
84
|
|
|
*/ |
85
|
|
|
public function setAuthKey($token) |
86
|
|
|
{ |
87
|
|
|
$this->auth_key = $token; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Setter to change request object |
92
|
|
|
* |
93
|
|
|
* @param RequestInterface $request The request object to perform API requests |
94
|
|
|
*/ |
95
|
|
|
public function setRequest($request) |
96
|
|
|
{ |
97
|
|
|
$this->request = $request; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Setter to allow the adding / changing of the Curl options that will be used within the HTTP requests |
102
|
|
|
* |
103
|
|
|
* @param int $key The CURLOPT_XXX option to set e.g. CURLOPT_TIMEOUT |
104
|
|
|
* @param mixed $value The value to be set on option e.g. 10 |
105
|
|
|
*/ |
106
|
|
|
public function setCurlOption($key, $value) |
107
|
|
|
{ |
108
|
|
|
$this->curl_options[$key] = $value; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* API call method for sending requests using GET |
113
|
|
|
* |
114
|
|
|
* @param string $path Path of the endpoint |
115
|
|
|
* @param array|null $data Data to be sent along with the request |
116
|
|
|
* |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
|
|
public function get($path, array $data = null) |
120
|
|
|
{ |
121
|
|
|
return $this->request($path, $data, 'get'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* API call method for sending requests using POST |
126
|
|
|
* |
127
|
|
|
* @param string $path Path of the endpoint |
128
|
|
|
* @param array|null $data Data to be sent along with the request |
129
|
|
|
* |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
public function post($path, array $data = null) |
133
|
|
|
{ |
134
|
|
|
return $this->request($path, $data, 'post'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* API call method for sending requests using PUT |
139
|
|
|
* |
140
|
|
|
* @param string $path Path of the endpoint |
141
|
|
|
* @param array|null $data Data to be sent along with the request |
142
|
|
|
* |
143
|
|
|
* @return mixed |
144
|
|
|
*/ |
145
|
|
|
public function put($path, array $data = null) |
146
|
|
|
{ |
147
|
|
|
return $this->request($path, $data, 'put'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* API call method for sending requests using DELETE |
152
|
|
|
* |
153
|
|
|
* @param string $path Path of the endpoint |
154
|
|
|
* @param array|null $data Data to be sent along with the request |
155
|
|
|
* |
156
|
|
|
* @return mixed |
157
|
|
|
*/ |
158
|
|
|
public function delete($path, array $data = null) |
159
|
|
|
{ |
160
|
|
|
return $this->request($path, $data, 'delete'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* API call method for sending requests using PATCH |
165
|
|
|
* |
166
|
|
|
* @param string $path Path of the endpoint |
167
|
|
|
* @param array|null $data Data to be sent along with the request |
168
|
|
|
* |
169
|
|
|
* @return mixed |
170
|
|
|
*/ |
171
|
|
|
public function patch($path, array $data = null) |
172
|
|
|
{ |
173
|
|
|
return $this->request($path, $data, 'patch'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @codeCoverageIgnore |
178
|
|
|
* |
179
|
|
|
* API call method for sending requests using GET, POST, PUT, DELETE OR PATCH |
180
|
|
|
* |
181
|
|
|
* @param string $path Path of the endpoint |
182
|
|
|
* @param array|null $data Data to be sent along with the request |
183
|
|
|
* @param string|null $method Type of method that should be used ('GET', 'POST', 'PUT', 'DELETE', 'PATCH') |
184
|
|
|
* |
185
|
|
|
* @throws \Cloudflare\Exception\UnauthorizedException |
186
|
|
|
* @throws \Cloudflare\Exception\AuthenticationException |
187
|
|
|
* |
188
|
|
|
* @return mixed |
189
|
|
|
*/ |
190
|
|
|
protected function request($path, array $data = null, $method = null) |
191
|
|
|
{ |
192
|
|
|
return $this->request->perform($this, $path, $data, $method); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|