Conditions | 5 |
Paths | 12 |
Total Lines | 27 |
Code Lines | 18 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
18 | function __invoke(string $url, array $header=[], mixed $body=null):array { |
||
19 | if ($body != null) $body = json_encode($body); |
||
20 | |||
21 | $ch = curl_init($url); |
||
22 | |||
23 | // Defaults. |
||
24 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
25 | curl_setopt($ch, CURLINFO_HEADER_OUT, true); |
||
26 | // Header. |
||
27 | $header[] = 'Content-Type: application/json'; |
||
28 | $header[] = 'User-Agent: '.$this->userAgent; |
||
29 | if ($body != null) $header[] = 'Content-Length: '.strlen($body); |
||
30 | curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
||
31 | // Request Method and Body. |
||
32 | if ($this->method == self::POST) { |
||
33 | curl_setopt($ch, CURLOPT_POST, true); |
||
34 | if ($body != null) { |
||
35 | curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
||
36 | } |
||
37 | } |
||
38 | // Exec. |
||
39 | $result = curl_exec($ch); |
||
40 | $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
41 | curl_close($ch); |
||
42 | return [ |
||
43 | 'code' => $code, |
||
44 | 'result' => $result |
||
45 | ]; |
||
48 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.