1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace ddlzz\AmoAPI\Request; |
5
|
|
|
|
6
|
|
|
use ddlzz\AmoAPI\Exceptions\CurlException; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* An abstraction layer for interacting with cURL library. |
11
|
|
|
* @package ddlzz\AmoAPI |
12
|
|
|
* @author ddlzz |
13
|
|
|
*/ |
14
|
|
|
class Curl |
15
|
|
|
{ |
16
|
|
|
/** @var resource */ |
17
|
|
|
private $curl; |
18
|
|
|
|
19
|
|
|
/** Curl constructor. */ |
20
|
3 |
|
public function init() |
21
|
|
|
{ |
22
|
3 |
|
$this->curl = curl_init(); |
23
|
3 |
|
} |
24
|
|
|
|
25
|
2 |
|
public function close() |
26
|
|
|
{ |
27
|
2 |
|
curl_close($this->curl); |
28
|
2 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return array|false |
32
|
|
|
* @throws CurlException |
33
|
|
|
*/ |
34
|
1 |
|
public function exec() |
35
|
|
|
{ |
36
|
1 |
|
if (!is_resource($this->curl)) { |
37
|
|
|
throw new CurlException('Curl class is not properly initialized'); |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
return curl_exec($this->curl); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/////////////////////////////// |
44
|
|
|
// The list of possible options |
45
|
|
|
/////////////////////////////// |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $url |
49
|
|
|
* @return bool |
50
|
|
|
* @throws CurlException |
51
|
|
|
*/ |
52
|
1 |
|
public function setUrl($url) |
53
|
|
|
{ |
54
|
1 |
|
return $this->setOpt(CURLOPT_URL, $url); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param bool $value |
59
|
|
|
* @return bool |
60
|
|
|
* @throws CurlException |
61
|
|
|
*/ |
62
|
1 |
|
public function setReturnTransfer($value) |
63
|
|
|
{ |
64
|
1 |
|
return $this->setOpt(CURLOPT_RETURNTRANSFER, $value); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $agent |
69
|
|
|
* @return bool |
70
|
|
|
* @throws CurlException |
71
|
|
|
*/ |
72
|
|
|
public function setUserAgent($agent) |
73
|
|
|
{ |
74
|
|
|
return $this->setOpt(CURLOPT_USERAGENT, $agent); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $header |
79
|
|
|
* @return bool |
80
|
|
|
* @throws CurlException |
81
|
|
|
*/ |
82
|
|
|
public function setHttpHeader(array $header) |
83
|
|
|
{ |
84
|
|
|
return $this->setOpt(CURLOPT_HTTPHEADER, $header); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param bool $value |
89
|
|
|
* @return bool |
90
|
|
|
* @throws CurlException |
91
|
|
|
*/ |
92
|
|
|
public function setHeader($value) |
93
|
|
|
{ |
94
|
|
|
return $this->setOpt(CURLOPT_HEADER, $value); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $cookie |
99
|
|
|
* @return bool |
100
|
|
|
* @throws CurlException |
101
|
|
|
*/ |
102
|
|
|
public function setCookieFile($cookie) |
103
|
|
|
{ |
104
|
|
|
return $this->setOpt(CURLOPT_COOKIEFILE, $cookie); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $cookieJar |
109
|
|
|
* @return bool |
110
|
|
|
* @throws CurlException |
111
|
|
|
*/ |
112
|
|
|
public function setCookieJar($cookieJar) |
113
|
|
|
{ |
114
|
|
|
return $this->setOpt(CURLOPT_COOKIEJAR, $cookieJar); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param bool $value |
119
|
|
|
* @return bool |
120
|
|
|
* @throws CurlException |
121
|
|
|
*/ |
122
|
|
|
public function setSSLVerifyPeer($value) |
123
|
|
|
{ |
124
|
|
|
return $this->setOpt(CURLOPT_SSL_VERIFYPEER, $value); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param bool $value |
129
|
|
|
* @return bool |
130
|
|
|
* @throws CurlException |
131
|
|
|
*/ |
132
|
|
|
public function setSSLVerifyHost($value) |
133
|
|
|
{ |
134
|
|
|
return $this->setOpt(CURLOPT_SSL_VERIFYHOST, $value); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $request |
139
|
|
|
* @return bool |
140
|
|
|
* @throws CurlException |
141
|
|
|
*/ |
142
|
|
|
public function setCustomRequest($request) |
143
|
|
|
{ |
144
|
|
|
return $this->setOpt(CURLOPT_CUSTOMREQUEST, $request); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param mixed $fields |
149
|
|
|
* @return bool |
150
|
|
|
* @throws CurlException |
151
|
|
|
*/ |
152
|
|
|
public function setPostFields($fields) |
153
|
|
|
{ |
154
|
|
|
return $this->setOpt(CURLOPT_POSTFIELDS, $fields); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
////////////////////// |
158
|
|
|
// The end of the list |
159
|
|
|
////////////////////// |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return int |
163
|
|
|
*/ |
164
|
|
|
public function getHttpCode() |
165
|
|
|
{ |
166
|
|
|
return curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Closes the connection in case of abnormal termination |
171
|
|
|
* @throws CurlException |
172
|
|
|
*/ |
173
|
1 |
|
public function __destruct() |
174
|
|
|
{ |
175
|
1 |
|
if (is_resource($this->curl)) { |
176
|
1 |
|
curl_close($this->curl); |
177
|
1 |
|
throw new CurlException('Curl class was not properly closed'); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Sets various cURL options. |
183
|
|
|
* |
184
|
|
|
* @param string $option |
185
|
|
|
* @param mixed $value |
186
|
|
|
* @return bool |
187
|
|
|
* @throws CurlException |
188
|
|
|
*/ |
189
|
1 |
|
private function setOpt($option, $value) |
190
|
|
|
{ |
191
|
1 |
|
if (!is_resource($this->curl)) { |
192
|
|
|
throw new CurlException('Curl class is not properly initialized'); |
193
|
|
|
} |
194
|
|
|
|
195
|
1 |
|
return curl_setopt($this->curl, $option, $value); |
|
|
|
|
196
|
|
|
} |
197
|
|
|
} |