1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the core-library package. |
5
|
|
|
* |
6
|
|
|
* (c) 2019 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Library\Core\Network\CURL\Helper; |
13
|
|
|
|
14
|
|
|
use DateTime; |
15
|
|
|
use Exception; |
16
|
|
|
use WBW\Library\Core\Network\CURL\Configuration\CurlConfiguration; |
17
|
|
|
use WBW\Library\Core\Network\HTTP\HttpInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* cURL helper. |
21
|
|
|
* |
22
|
|
|
* @author webeweb <https://github.com/webeweb> |
23
|
|
|
* @package WBW\Library\Core\Network\CURL\Helper |
24
|
|
|
*/ |
25
|
|
|
class CurlHelper { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Initialize a cURL stream. |
29
|
|
|
* |
30
|
|
|
* @param string $url The URL. |
31
|
|
|
* @param CurlConfiguration $config The cURL configuration. |
32
|
|
|
* @return resource Returns the cURL stream. |
33
|
|
|
*/ |
34
|
|
|
public static function initStream(string $url, CurlConfiguration $config) { |
35
|
|
|
|
36
|
|
|
$stream = curl_init(); |
37
|
|
|
|
38
|
|
|
curl_setopt($stream, CURLOPT_URL, $url); |
39
|
|
|
|
40
|
|
|
if (true === $config->getAllowEncoding()) { |
41
|
|
|
curl_setopt($stream, CURLOPT_ENCODING, ""); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (0 < $config->getConnectTimeout()) { |
45
|
|
|
curl_setopt($stream, CURLOPT_CONNECTTIMEOUT, $config->getConnectTimeout()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $stream; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set the cURL headers. |
53
|
|
|
* |
54
|
|
|
* @param resource $stream The stream. |
55
|
|
|
* @param array $headers The headers. |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public static function setHeaders($stream, array $headers): void { |
59
|
|
|
curl_setopt($stream, CURLOPT_HEADER, 1); |
60
|
|
|
curl_setopt($stream, CURLOPT_HTTPHEADER, $headers); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Set cURL POST. |
65
|
|
|
* |
66
|
|
|
* @param resource $stream The stream. |
67
|
|
|
* @param string $method The HTTP method. |
68
|
|
|
* @param string $postData The POST data. |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public static function setPost($stream, string $method, string $postData): void { |
72
|
|
|
|
73
|
|
|
switch ($method) { |
74
|
|
|
|
75
|
|
|
case HttpInterface::HTTP_METHOD_DELETE: |
76
|
|
|
case HttpInterface::HTTP_METHOD_OPTIONS: |
77
|
|
|
case HttpInterface::HTTP_METHOD_PATCH: |
78
|
|
|
case HttpInterface::HTTP_METHOD_PUT: |
79
|
|
|
curl_setopt($stream, CURLOPT_CUSTOMREQUEST, $method); |
80
|
|
|
curl_setopt($stream, CURLOPT_POSTFIELDS, $postData); |
81
|
|
|
break; |
82
|
|
|
|
83
|
|
|
case HttpInterface::HTTP_METHOD_HEAD: |
84
|
|
|
curl_setopt($stream, CURLOPT_NOBODY, true); |
85
|
|
|
break; |
86
|
|
|
|
87
|
|
|
case HttpInterface::HTTP_METHOD_POST: |
88
|
|
|
curl_setopt($stream, CURLOPT_POST, true); |
89
|
|
|
curl_setopt($stream, CURLOPT_POSTFIELDS, $postData); |
90
|
|
|
break; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set cURL proxy. |
96
|
|
|
* |
97
|
|
|
* @param resource $stream The stream. |
98
|
|
|
* @param CurlConfiguration $config The cURL configuration. |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
public static function setProxy($stream, CurlConfiguration $config): void { |
102
|
|
|
|
103
|
|
|
if (null !== $config->getProxyHost()) { |
104
|
|
|
curl_setopt($stream, CURLOPT_PROXY, $config->getProxyHost()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (null !== $config->getProxyPort()) { |
108
|
|
|
curl_setopt($stream, CURLOPT_PROXYPORT, $config->getProxyPort()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (null !== $config->getProxyType()) { |
112
|
|
|
curl_setopt($stream, CURLOPT_PROXYTYPE, $config->getProxyType()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (null !== $config->getProxyUsername()) { |
116
|
|
|
curl_setopt($stream, CURLOPT_PROXYUSERPWD, implode(":", [$config->getProxyUsername(), $config->getProxyPassword()])); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set cURL return transfer. |
122
|
|
|
* |
123
|
|
|
* @param resource $stream The stream. |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public static function setReturnTransfer($stream): void { |
127
|
|
|
curl_setopt($stream, CURLOPT_RETURNTRANSFER, true); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set cURL SSL. |
132
|
|
|
* |
133
|
|
|
* @param resource $stream The stream. |
134
|
|
|
* @param CurlConfiguration $config The cURL configuration. |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
|
|
public static function setSsl($stream, CurlConfiguration $config): void { |
138
|
|
|
if (false === $config->getSslVerification()) { |
139
|
|
|
curl_setopt($stream, CURLOPT_SSL_VERIFYHOST, 0); |
140
|
|
|
curl_setopt($stream, CURLOPT_SSL_VERIFYPEER, 0); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set cURL timeout. |
146
|
|
|
* |
147
|
|
|
* @param resource $stream The stream. |
148
|
|
|
* @param CurlConfiguration $config The cURL configuration. |
149
|
|
|
* @return void |
150
|
|
|
*/ |
151
|
|
|
public static function setTimeout($stream, CurlConfiguration $config): void { |
152
|
|
|
if (0 < $config->getRequestTimeout()) { |
153
|
|
|
curl_setopt($stream, CURLOPT_TIMEOUT, $config->getRequestTimeout()); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Set cURL user agent. |
159
|
|
|
* |
160
|
|
|
* @param resource $stream The stream. |
161
|
|
|
* @param CurlConfiguration $config The cURL configuration. |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
|
|
public static function setUserAgent($stream, CurlConfiguration $config): void { |
165
|
|
|
curl_setopt($stream, CURLOPT_USERAGENT, $config->getUserAgent()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set cURL verbose. |
170
|
|
|
* |
171
|
|
|
* @param resource $stream The stream. |
172
|
|
|
* @param CurlConfiguration $config The cURL configuration. |
173
|
|
|
* @param string $url The URL. |
174
|
|
|
* @param string $postData The POST data. |
175
|
|
|
* @return void |
176
|
|
|
* @throws Exception Throws an exception if an error occurs. |
177
|
|
|
*/ |
178
|
|
|
public static function setVerbose($stream, CurlConfiguration $config, string $url, string $postData): void { |
179
|
|
|
|
180
|
|
|
if (true === $config->getDebug()) { |
181
|
|
|
|
182
|
|
|
curl_setopt($stream, CURLOPT_STDERR, fopen($config->getDebugFile(), "a")); |
183
|
|
|
curl_setopt($stream, CURLOPT_VERBOSE, 0); |
184
|
|
|
|
185
|
|
|
$msg = (new DateTime())->format("c") . " [DEBUG] {$url}" . PHP_EOL . "HTTP request body ~BEGIN~" . PHP_EOL . print_r($postData, true) . PHP_EOL . "~END~" . PHP_EOL; |
186
|
|
|
error_log($msg, 3, $config->getDebugFile()); |
187
|
|
|
} else { |
188
|
|
|
|
189
|
|
|
if (true === $config->getVerbose()) { |
190
|
|
|
curl_setopt($stream, CURLOPT_VERBOSE, 1); |
191
|
|
|
} else { |
192
|
|
|
curl_setopt($stream, CURLOPT_VERBOSE, 0); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|