1 | <?php |
||
12 | class CurlHttpClient implements HttpClient |
||
13 | { |
||
14 | /** |
||
15 | * Custom CURL options for requests. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $options = [ |
||
20 | CURLOPT_USERAGENT => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36' |
||
21 | ]; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $headers = []; |
||
27 | |||
28 | /** |
||
29 | * Contains the curl instance. |
||
30 | * |
||
31 | * @var resource |
||
32 | */ |
||
33 | protected $curl; |
||
34 | |||
35 | /** |
||
36 | * Path to cookies file |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $cookieJar; |
||
40 | |||
41 | /** |
||
42 | * Cookies container |
||
43 | * |
||
44 | * @var Cookies |
||
45 | */ |
||
46 | protected $cookies; |
||
47 | |||
48 | /** |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $currentUrl; |
||
52 | |||
53 | /** |
||
54 | * Path to directory to store cookie file |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $cookiesPath; |
||
58 | |||
59 | public function __construct(Cookies $cookies) |
||
63 | |||
64 | /** |
||
65 | * Load cookies for specified username |
||
66 | * |
||
67 | * @param string $username |
||
68 | * @return HttpClient |
||
69 | */ |
||
70 | public function loadCookies($username = '') |
||
76 | |||
77 | /** |
||
78 | * Executes curl request. |
||
79 | * |
||
80 | * @param string $url |
||
81 | * @param string $postString |
||
82 | * @param array $headers |
||
83 | * @return string |
||
84 | */ |
||
85 | public function execute($url, $postString = '', array $headers = []) |
||
91 | |||
92 | /** |
||
93 | * @return mixed |
||
94 | */ |
||
95 | protected function callCurl() |
||
107 | |||
108 | /** |
||
109 | * Initializes curl resource with options. |
||
110 | * |
||
111 | * @param string $url |
||
112 | * @param string $postString |
||
113 | * @param array $headers |
||
114 | * @return $this |
||
115 | */ |
||
116 | protected function init($url, $postString, $headers) |
||
130 | |||
131 | /** |
||
132 | * @return array |
||
133 | */ |
||
134 | protected function getDefaultHttpOptions() |
||
148 | |||
149 | /** |
||
150 | * Adds necessary curl options for query. |
||
151 | * |
||
152 | * @param string $postString POST query string |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | protected function makeHttpOptions($postString = '') |
||
171 | |||
172 | /** |
||
173 | * Set custom Curl options to override default |
||
174 | * |
||
175 | * @codeCoverageIgnore |
||
176 | * @param array $options |
||
177 | * @return HttpClient |
||
178 | */ |
||
179 | public function setOptions(array $options) |
||
185 | |||
186 | /** |
||
187 | * Get a cookie value by name |
||
188 | * |
||
189 | * @param $name |
||
190 | * @return mixed |
||
191 | */ |
||
192 | public function cookie($name) |
||
196 | |||
197 | /** |
||
198 | * Get all cookies |
||
199 | * |
||
200 | * @return array |
||
201 | */ |
||
202 | public function cookies() |
||
206 | |||
207 | /** |
||
208 | * Set directory to store all cookie files. |
||
209 | * |
||
210 | * @codeCoverageIgnore |
||
211 | * @param string $path |
||
212 | * @return $this |
||
213 | */ |
||
214 | public function setCookiesPath($path) |
||
220 | |||
221 | /** |
||
222 | * @return $this |
||
223 | */ |
||
224 | public function removeCookies() |
||
234 | |||
235 | /** |
||
236 | * Init cookie file for a specified username. If username is empty we use |
||
237 | * common cookie file for all sessions. If file does not exist it will |
||
238 | * be created in system temp directory. |
||
239 | * |
||
240 | * @param $username |
||
241 | * @return $this |
||
242 | */ |
||
243 | protected function initCookieJar($username = '') |
||
249 | |||
250 | /** |
||
251 | * Returns cookie file name by username. If username is empty we use a |
||
252 | * random cookie name, to be sure we have different cookies |
||
253 | * in parallel sessions. |
||
254 | * |
||
255 | * @param string $username |
||
256 | * @return string |
||
257 | */ |
||
258 | protected function initCookieFile($username = '') |
||
273 | |||
274 | /** |
||
275 | * @return string |
||
276 | */ |
||
277 | public function getCookiesPath() |
||
281 | |||
282 | /** |
||
283 | * @return $this |
||
284 | */ |
||
285 | protected function fillCookies() |
||
291 | |||
292 | /** |
||
293 | * @codeCoverageIgnore |
||
294 | * @return string |
||
295 | */ |
||
296 | public function getCurrentUrl() |
||
300 | |||
301 | /** |
||
302 | * @param string $host '192.168.1.1' |
||
303 | * @param string $port '12345' |
||
304 | * @param string $auth Authentication string: 'username:password' |
||
305 | * @param string $type HTTP|SOCKS |
||
306 | * @return HttpClient |
||
307 | */ |
||
308 | public function useProxy($host, $port, $auth = null, $type = null) |
||
320 | |||
321 | public function dontUseProxy() |
||
330 | |||
331 | /** |
||
332 | * @return bool |
||
333 | */ |
||
334 | public function usesProxy() |
||
338 | |||
339 | /** |
||
340 | * @codeCoverageIgnore |
||
341 | * @param string $host |
||
342 | * @param string $port |
||
343 | * @param null $auth |
||
344 | * @return HttpClient |
||
345 | */ |
||
346 | public function useSocksProxy($host, $port, $auth = null) |
||
350 | } |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: