1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @version 1.0 |
||
5 | * @author Vermeulen Maxime (bulton-fr) <[email protected]> |
||
6 | * @package bultonFr |
||
7 | */ |
||
8 | |||
9 | namespace bultonFr\CallCurl; |
||
10 | |||
11 | use \Exception; |
||
12 | use \bultonFr\CallCurl\Parser as ParserInterface; |
||
13 | use \bultonFr\CallCurl\Parser\DefaultParser; |
||
14 | |||
15 | class CallCurl |
||
16 | { |
||
17 | /** |
||
18 | * @var resource $curl Curl resource |
||
19 | */ |
||
20 | protected $curl = null; |
||
21 | |||
22 | /** |
||
23 | * @var Object $parserInput PHP Class used for parse data before call |
||
24 | */ |
||
25 | protected $parserOutput = null; |
||
26 | |||
27 | /** |
||
28 | * @var Object $parserOutput PHP Class used for parse data after call |
||
29 | */ |
||
30 | protected $parserInput = null; |
||
31 | |||
32 | /** |
||
33 | * @var string $url The url to call with curl |
||
34 | */ |
||
35 | protected $url = ''; |
||
36 | |||
37 | /** |
||
38 | * @var mixed $datas Datas to send with curl |
||
39 | */ |
||
40 | protected $datas = ''; |
||
41 | |||
42 | /** |
||
43 | * @var string $httpMethod The HTTP method to use for curl call |
||
44 | */ |
||
45 | protected $httpMethod = 'GET'; |
||
46 | |||
47 | /** |
||
48 | * @var boolean $debug Ask to curl more datas to return for help debug |
||
49 | */ |
||
50 | protected $debug = false; |
||
51 | |||
52 | /** |
||
53 | * @var boolean $checkSSL If curl doing check SSL certificate |
||
54 | */ |
||
55 | protected $checkSSL = true; |
||
56 | |||
57 | /** |
||
58 | * @var mixed $returnDatas : Datas return by curl call |
||
59 | */ |
||
60 | protected $returnDatas; |
||
61 | |||
62 | /** |
||
63 | * @var boolean|array $curlCallInfos : Informations returned by curl |
||
64 | */ |
||
65 | protected $curlCallInfos; |
||
66 | |||
67 | /** |
||
68 | * @var array $httpHeaders : Custom headers to send with http request |
||
69 | */ |
||
70 | protected $httpHeaders = []; |
||
71 | |||
72 | /** |
||
73 | * Constructor |
||
74 | * |
||
75 | * Check dependancy |
||
76 | * Initialise curl connection |
||
77 | * Check parsers |
||
78 | */ |
||
79 | public function __construct(&$parserOutput = null, &$parserInput = null) |
||
80 | { |
||
81 | //Check Dependancy |
||
82 | $this->checkLib(); |
||
83 | |||
84 | //Initialise curl |
||
85 | $this->curl = curl_init(); |
||
86 | |||
87 | //Check and initialise Parsers |
||
88 | $this->checkParser($parserOutput); |
||
89 | $this->checkParser($parserInput); |
||
90 | |||
91 | $this->parserOutput = $parserOutput; |
||
92 | $this->parserInput = $parserInput; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Check if all php librairie required is active |
||
97 | * |
||
98 | * @throws Exception |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | protected function checkLib() |
||
103 | { |
||
104 | //Check curl php extension |
||
105 | if(!function_exists('curl_init')) { |
||
106 | throw new Exception('Please install or activate php-curl extension for use CallCurl plugin.'); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Check if datas parser is a object and implement parser interface |
||
112 | * |
||
113 | * @param mixed &$parser Parser to use |
||
114 | * |
||
115 | * @throws Exception If error is find on parser definition |
||
116 | */ |
||
117 | protected function checkParser(&$parser) |
||
118 | { |
||
119 | if(is_null($parser)) { |
||
120 | $parser = new DefaultParser; |
||
121 | } |
||
122 | |||
123 | if(!is_object($parser)) { |
||
124 | throw new Exception('Parser should be an object instance.'); |
||
125 | } |
||
126 | |||
127 | if(!($parser instanceof ParserInterface)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
128 | throw new Exception('Parser should implement \bultonFr\Parser interface.'); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Set url to call |
||
134 | * |
||
135 | * @param string $url : Url to call |
||
136 | * |
||
137 | * @return \bultonFr\CallCurl : Current class instance |
||
138 | * |
||
139 | * @throws Exception : If parameter is not a string |
||
140 | */ |
||
141 | public function setUrl($url) |
||
142 | { |
||
143 | if(!is_string($url)) { |
||
0 ignored issues
–
show
|
|||
144 | throw new Exception('Url send to CallCurl should be a string variable'); |
||
145 | } |
||
146 | |||
147 | $this->url = $url; |
||
148 | |||
149 | return $this; |
||
0 ignored issues
–
show
|
|||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Getter to url to call |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | public function getUrl() |
||
158 | { |
||
159 | return $this->url; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Set datas to send with curl |
||
164 | * |
||
165 | * @param mixed $datas |
||
166 | * |
||
167 | * @return \bultonFr\CallCurl : Current class instance |
||
168 | */ |
||
169 | public function setDatas($datas) |
||
170 | { |
||
171 | $this->datas = $datas; |
||
172 | |||
173 | return $this; |
||
0 ignored issues
–
show
|
|||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Getter to datas send with curl |
||
178 | * |
||
179 | * @return mixed |
||
180 | */ |
||
181 | public function getDatas() |
||
182 | { |
||
183 | return $this->datas; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Set HTTP method to use to call |
||
188 | * ex: GET, POST, PUT, DELETE etc |
||
189 | * |
||
190 | * @param string $method : The HTTP method to use |
||
191 | * |
||
192 | * @return \bultonFr\CallCurl : Current class instance |
||
193 | * |
||
194 | * @throws Exception : If parameter is not a string |
||
195 | */ |
||
196 | public function setHttpMethod($method) |
||
197 | { |
||
198 | if(!is_string($method)) { |
||
0 ignored issues
–
show
|
|||
199 | throw new Exception('HTTP method send to CallCurl should be a string variable'); |
||
200 | } |
||
201 | |||
202 | $this->httpMethod = strtoupper($method); |
||
203 | |||
204 | return $this; |
||
0 ignored issues
–
show
|
|||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Set debug mode. |
||
209 | * Curl call return more informations to help |
||
210 | * |
||
211 | * @param boolean $debug : Debug mode status |
||
212 | * |
||
213 | * @return \bultonFr\CallCurl : Current class instance |
||
214 | * |
||
215 | * @throws Exception : If parameter is not a boolean |
||
216 | */ |
||
217 | public function setDebug($debug) |
||
218 | { |
||
219 | if(!is_bool($debug)) { |
||
0 ignored issues
–
show
|
|||
220 | throw new Exception('Debug status send to CallCurl should be a boolean variable'); |
||
221 | } |
||
222 | |||
223 | $this->debug = $debug; |
||
224 | |||
225 | return $this; |
||
0 ignored issues
–
show
|
|||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Set if curl check SSL certificate |
||
230 | * |
||
231 | * @param type $checkSSL : check SSL status |
||
0 ignored issues
–
show
The type
bultonFr\CallCurl\type was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
232 | * |
||
233 | * @return \bultonFr\CallCurl : Current class instance |
||
234 | * |
||
235 | * @throws Exception : If parameter is not a boolean |
||
236 | */ |
||
237 | public function setCheckSSL($checkSSL) |
||
238 | { |
||
239 | if(!is_bool($checkSSL)) { |
||
0 ignored issues
–
show
|
|||
240 | throw new Exception('CheckSSL status send to CallCurl should be a boolean variable'); |
||
241 | } |
||
242 | |||
243 | $this->checkSSL = $checkSSL; |
||
244 | |||
245 | return $this; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Get curl returned datas after parser formating |
||
250 | * |
||
251 | * @return mixed |
||
252 | */ |
||
253 | public function getCurlReturnDatas() |
||
254 | { |
||
255 | return $this->returnDatas; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Get curl call information |
||
260 | * |
||
261 | * @see http://php.net/manual/fr/function.curl-getinfo.php |
||
262 | * |
||
263 | * @return boolean|array : false if error. else, array to information |
||
264 | */ |
||
265 | public function getCurlCallInfos() |
||
266 | { |
||
267 | return $this->curlCallInfos; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Http header to add on the request |
||
272 | * |
||
273 | * @param array $header |
||
274 | */ |
||
275 | public function addHttpHeader($header) |
||
276 | { |
||
277 | $this->httpHeaders[] = $header; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Run curl call stack |
||
282 | * * Formating datas before call |
||
283 | * * Generating curl options array |
||
284 | * * Run call |
||
285 | * * Formating datas returned by curl |
||
286 | * |
||
287 | * @return mixed Datas returned by curl |
||
288 | */ |
||
289 | public function runCall() |
||
290 | { |
||
291 | $this->parserOutput->preFormatDatas($this->datas); |
||
292 | |||
293 | $options = $this->curlSetOptions(); |
||
294 | curl_setopt_array($this->curl, $options); |
||
295 | |||
296 | $this->getFromCurl(); |
||
297 | $this->parserInput->formatCallReturn($this->returnDatas); |
||
298 | |||
299 | return $this->returnDatas; |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Define curl options array |
||
304 | * |
||
305 | * @return array : Options array to curl call |
||
306 | */ |
||
307 | protected function curlSetOptions() |
||
308 | { |
||
309 | $options = [ |
||
310 | CURLOPT_URL => $this->url, |
||
311 | CURLOPT_RETURNTRANSFER => true |
||
312 | ]; |
||
313 | |||
314 | $this->curlOptionsAddDatas($options); |
||
315 | $this->curlOptionsAddHttpHeaders($options); |
||
316 | |||
317 | if($this->debug === true) { |
||
318 | $options[CURLOPT_HEADER] = true; |
||
319 | $options[CURLOPT_VERBOSE] = true; |
||
320 | $options[CURLINFO_HEADER_OUT] = true; |
||
321 | } |
||
322 | |||
323 | if($this->checkSSL === false) { |
||
324 | $options[CURLOPT_SSL_VERIFYHOST] = false; |
||
325 | $options[CURLOPT_SSL_VERIFYPEER] = false; |
||
326 | } |
||
327 | |||
328 | return $options; |
||
329 | } |
||
330 | |||
331 | protected function curlOptionsAddDatas(&$options) |
||
332 | { |
||
333 | if(empty($this->datas)) { |
||
334 | return; |
||
335 | } |
||
336 | |||
337 | if($this->httpMethod === 'GET') { |
||
338 | |||
339 | if(is_string($this->datas)) { |
||
340 | $datasUrl = urlencode($this->datas); |
||
341 | } else { |
||
342 | $datasUrl = http_build_query($this->datas); |
||
343 | } |
||
344 | |||
345 | if(strpos($this->url, '?') !== false) { |
||
346 | $datasUrl = '&'.$datasUrl; |
||
347 | } |
||
348 | else { |
||
349 | $datasUrl = '?'.$datasUrl; |
||
350 | } |
||
351 | |||
352 | $options[CURLOPT_URL] .= $datasUrl; |
||
353 | return; |
||
354 | } |
||
355 | |||
356 | if($this->httpMethod === 'POST') { |
||
357 | $options[CURLOPT_POST] = true; |
||
358 | $options[CURLOPT_POSTFIELDS] = $this->datas; |
||
359 | |||
360 | return; |
||
361 | } |
||
362 | |||
363 | //@TODO : Other http status |
||
364 | } |
||
365 | |||
366 | protected function curlOptionsAddHttpHeaders(&$options) |
||
367 | { |
||
368 | $httpHeader = []; |
||
369 | |||
370 | foreach ($this->httpHeaders as $header) { |
||
371 | if (is_string($header)) { |
||
372 | $httpHeader[] = $header; |
||
373 | continue; |
||
374 | } elseif (is_array($header)) { |
||
375 | $httpHeader[] = $header[0].': '.$header[1]; |
||
376 | continue; |
||
377 | } |
||
378 | } |
||
379 | |||
380 | if ($httpHeader === []) { |
||
381 | return; |
||
382 | } |
||
383 | |||
384 | $options[CURLOPT_HTTPHEADER] = $httpHeader; |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Run curl call and get informations about call |
||
389 | * |
||
390 | * @return void |
||
391 | */ |
||
392 | protected function getFromCurl() |
||
393 | { |
||
394 | $this->returnDatas = curl_exec($this->curl); |
||
395 | $this->curlCallInfos = curl_getinfo($this->curl); |
||
396 | |||
397 | curl_close($this->curl); |
||
398 | } |
||
399 | } |
||
400 |