1 | <?php |
||||
2 | |||||
3 | namespace Bludata\Helpers; |
||||
4 | |||||
5 | class CurlHelper |
||||
6 | { |
||||
7 | protected $init; |
||||
8 | protected $headers = []; |
||||
9 | protected $response; |
||||
10 | protected $info; |
||||
11 | protected $baseUrl; |
||||
12 | protected $posFixUrl; |
||||
13 | protected $options = []; |
||||
14 | |||||
15 | public function __construct($baseUrl, array $headers = []) |
||||
16 | { |
||||
17 | $this->init = curl_init(); |
||||
18 | |||||
19 | $this->baseUrl = $baseUrl; |
||||
20 | |||||
21 | $this->headers = $headers; |
||||
22 | } |
||||
23 | |||||
24 | protected function exec() |
||||
25 | { |
||||
26 | curl_setopt($this->init, CURLOPT_URL, trim($this->baseUrl.$this->posFixUrl)); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
27 | curl_setopt($this->init, CURLOPT_RETURNTRANSFER, true); |
||||
28 | curl_setopt($this->init, CURLOPT_HTTPHEADER, $this->headers); |
||||
29 | curl_setopt($this->init, CURLOPT_SSL_VERIFYPEER, false); |
||||
30 | |||||
31 | foreach ($this->options as $key => $value) { |
||||
32 | curl_setopt($this->init, $key, $value); |
||||
33 | } |
||||
34 | |||||
35 | $this->response = curl_exec($this->init); |
||||
0 ignored issues
–
show
It seems like
$this->init can also be of type boolean ; however, parameter $ch of curl_exec() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
36 | |||||
37 | $this->info = curl_getinfo($this->init); |
||||
0 ignored issues
–
show
It seems like
$this->init can also be of type boolean ; however, parameter $ch of curl_getinfo() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
38 | } |
||||
39 | |||||
40 | public function send($close = true) |
||||
41 | { |
||||
42 | $this->exec(); |
||||
43 | |||||
44 | if ($close === true) { |
||||
45 | curl_close($this->init); |
||||
0 ignored issues
–
show
It seems like
$this->init can also be of type boolean ; however, parameter $ch of curl_close() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
46 | } |
||||
47 | |||||
48 | return $this; |
||||
49 | } |
||||
50 | |||||
51 | public function addHeader($header) |
||||
52 | { |
||||
53 | $this->headers[] = $header; |
||||
54 | |||||
55 | return $this; |
||||
56 | } |
||||
57 | |||||
58 | public function getResponse() |
||||
59 | { |
||||
60 | return [ |
||||
61 | 'code' => $this->info['http_code'], |
||||
62 | 'data' => $this->response, |
||||
63 | ]; |
||||
64 | } |
||||
65 | |||||
66 | public function getInfo() |
||||
67 | { |
||||
68 | return $this->info; |
||||
69 | } |
||||
70 | |||||
71 | public function setBaseUrl($baseUrl) |
||||
72 | { |
||||
73 | $this->baseUrl = $baseUrl; |
||||
74 | |||||
75 | return $this; |
||||
76 | } |
||||
77 | |||||
78 | public function setPosFixUrl($posFixUrl) |
||||
79 | { |
||||
80 | $this->posFixUrl = $posFixUrl; |
||||
81 | |||||
82 | return $this; |
||||
83 | } |
||||
84 | |||||
85 | public function post(array $data) |
||||
86 | { |
||||
87 | $this->options[CURLOPT_POST] = true; |
||||
88 | $this->options[CURLOPT_POSTFIELDS] = json_encode($data); |
||||
89 | |||||
90 | return $this; |
||||
91 | } |
||||
92 | |||||
93 | public function put(array $data) |
||||
94 | { |
||||
95 | $this->options[CURLOPT_CUSTOMREQUEST] = 'PUT'; |
||||
96 | $this->options[CURLOPT_POSTFIELDS] = json_encode($data); |
||||
97 | |||||
98 | return $this; |
||||
99 | } |
||||
100 | |||||
101 | public function delete() |
||||
102 | { |
||||
103 | $this->options[CURLOPT_CUSTOMREQUEST] = 'DELETE'; |
||||
104 | |||||
105 | return $this; |
||||
106 | } |
||||
107 | } |
||||
108 |