1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zengine\Zurl; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Zurl. |
7
|
|
|
*/ |
8
|
|
|
class Zurl |
9
|
|
|
{ |
10
|
|
|
protected $ch; |
11
|
|
|
|
12
|
|
|
protected $headers = []; |
13
|
|
|
|
14
|
|
|
protected $defaultOptions = [ |
15
|
|
|
CURLOPT_FOLLOWLOCATION => true, |
16
|
|
|
CURLOPT_RETURNTRANSFER => true, |
17
|
|
|
CURLOPT_HEADER => true, |
18
|
|
|
CURLOPT_CONNECTTIMEOUT => 15, |
19
|
|
|
CURLOPT_TIMEOUT => 25, |
20
|
|
|
CURLOPT_SSL_VERIFYHOST => 2, |
21
|
|
|
CURLOPT_SSL_VERIFYPEER => true, |
22
|
|
|
CURLOPT_USERAGENT => 'Zurl', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
public function __construct() |
26
|
|
|
{ |
27
|
|
|
$this->ch = curl_init(); |
28
|
|
|
curl_setopt_array($this->ch, $this->defaultOptions); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function get($url, $vars = []) |
32
|
|
|
{ |
33
|
|
|
$completeUrl = $url . (strpos($url, '?') === false ? '?' : '') . http_build_query($vars); |
34
|
|
|
$this->withOption(CURLOPT_URL, $completeUrl); |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function post($url, $payload = []) |
40
|
|
|
{ |
41
|
|
|
$this->withOption(CURLOPT_URL, $url); |
42
|
|
|
$this->withOption(CURLOPT_CUSTOMREQUEST, 'POST'); |
43
|
|
|
$this->withPayload($payload); |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function withUserAgent($agentName) |
49
|
|
|
{ |
50
|
|
|
curl_setopt($this->ch, CURLOPT_USERAGENT, $agentName); |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function withOption($option, $value) |
56
|
|
|
{ |
57
|
|
|
curl_setopt($this->ch, $option, $value); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function withHeader($name, $value) |
61
|
|
|
{ |
62
|
|
|
$this->headers[$name] = $value; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function withHeaders(array $headers) |
68
|
|
|
{ |
69
|
|
|
foreach ($headers as $name => $value) { |
70
|
|
|
$this->withHeader($name, $value); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function withoutSSLVerification() |
77
|
|
|
{ |
78
|
|
|
$this->withOption(CURLOPT_SSL_VERIFYHOST, false); |
79
|
|
|
$this->withOption(CURLOPT_SSL_VERIFYPEER, false); |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function withPayload($payload) |
85
|
|
|
{ |
86
|
|
|
$data_string = json_encode($payload); |
87
|
|
|
$this->withHeader('Content-Length', strlen($data_string)); |
88
|
|
|
$this->withOption(CURLOPT_POSTFIELDS, $data_string); |
89
|
|
|
$this->withHeader('Content-Type', 'application/json'); |
90
|
|
|
$this->withHeader('Accept', 'application/json'); |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function execute() |
96
|
|
|
{ |
97
|
|
|
return new ZurlResponse($this->ch); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function close() |
101
|
|
|
{ |
102
|
|
|
curl_close($this->ch); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|