|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bludata\Curl\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)); |
|
|
|
|
|
|
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); |
|
36
|
|
|
|
|
37
|
|
|
$this->info = curl_getinfo($this->init); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function send($close = true) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->exec(); |
|
43
|
|
|
|
|
44
|
|
|
if ($close === true) { |
|
45
|
|
|
curl_close($this->init); |
|
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
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.