forked from yiisoft/yii2-httpclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurlTransport.php
235 lines (199 loc) · 7.52 KB
/
CurlTransport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\httpclient;
use Yii;
/**
* CurlTransport sends HTTP messages using [Client URL Library (cURL)](http://php.net/manual/en/book.curl.php)
*
* Note: this transport requires PHP 'curl' extension installed.
*
* For this transport, you may setup request options as [cURL Options](http://php.net/manual/en/function.curl-setopt.php)
*
* @author Paul Klimov <[email protected]>
* @since 2.0
*/
class CurlTransport extends Transport
{
/**
* @inheritdoc
*/
public function send($request)
{
$request->beforeSend();
$curlOptions = $this->prepare($request);
$curlResource = $this->initCurl($curlOptions);
$responseHeaders = [];
$this->setHeaderOutput($curlResource, $responseHeaders);
$token = $request->client->createRequestLogToken($request->getMethod(), $curlOptions[CURLOPT_URL], $curlOptions[CURLOPT_HTTPHEADER], $request->getContent());
Yii::info($token, __METHOD__);
Yii::beginProfile($token, __METHOD__);
try {
$responseContent = curl_exec($curlResource);
} catch (\Exception $e) {
Yii::endProfile($token, __METHOD__);
throw new Exception($e->getMessage(), $e->getCode(), $e);
}
Yii::endProfile($token, __METHOD__);
// check cURL error
$errorNumber = curl_errno($curlResource);
$errorMessage = curl_error($curlResource);
curl_close($curlResource);
if ($errorNumber > 0) {
throw new Exception('Curl error: #' . $errorNumber . ' - ' . $errorMessage);
}
$response = $request->client->createResponse($responseContent, $responseHeaders);
$request->afterSend($response);
return $response;
}
/**
* @inheritdoc
*/
public function batchSend(array $requests)
{
$curlBatchResource = curl_multi_init();
$token = '';
$curlResources = [];
$responseHeaders = [];
foreach ($requests as $key => $request) {
/* @var $request Request */
$request->beforeSend();
$curlOptions = $this->prepare($request);
$curlResource = $this->initCurl($curlOptions);
$token .= $request->client->createRequestLogToken($request->getMethod(), $curlOptions[CURLOPT_URL], $curlOptions[CURLOPT_HTTPHEADER], $request->getContent()) . "\n\n";
$responseHeaders[$key] = [];
$this->setHeaderOutput($curlResource, $responseHeaders[$key]);
$curlResources[$key] = $curlResource;
curl_multi_add_handle($curlBatchResource, $curlResource);
}
Yii::info($token, __METHOD__);
Yii::beginProfile($token, __METHOD__);
try {
$isRunning = null;
do {
// See https://bugs.php.net/bug.php?id=61141
if (curl_multi_select($curlBatchResource) === -1) {
usleep(100);
}
do {
$curlExecCode = curl_multi_exec($curlBatchResource, $isRunning);
} while ($curlExecCode === CURLM_CALL_MULTI_PERFORM);
} while ($isRunning > 0 && $curlExecCode === CURLM_OK);
} catch (\Exception $e) {
Yii::endProfile($token, __METHOD__);
throw new Exception($e->getMessage(), $e->getCode(), $e);
}
Yii::endProfile($token, __METHOD__);
$responseContents = [];
foreach ($curlResources as $key => $curlResource) {
$responseContents[$key] = curl_multi_getcontent($curlResource);
curl_multi_remove_handle($curlBatchResource, $curlResource);
}
curl_multi_close($curlBatchResource);
$responses = [];
foreach ($requests as $key => $request) {
$response = $request->client->createResponse($responseContents[$key], $responseHeaders[$key]);
$request->afterSend($response);
$responses[$key] = $response;
}
return $responses;
}
/**
* Prepare request for execution, creating cURL resource for it.
* @param Request $request request instance.
* @return array cURL options.
*/
private function prepare($request)
{
$request->prepare();
$curlOptions = $this->composeCurlOptions($request->getOptions());
$method = strtoupper($request->getMethod());
switch ($method) {
case 'POST':
$curlOptions[CURLOPT_POST] = true;
break;
default:
$curlOptions[CURLOPT_CUSTOMREQUEST] = $method;
}
$content = $request->getContent();
if ($content === null) {
if ($method === 'HEAD') {
$curlOptions[CURLOPT_NOBODY] = true;
}
} else {
$curlOptions[CURLOPT_POSTFIELDS] = $content;
}
$curlOptions[CURLOPT_RETURNTRANSFER] = true;
$curlOptions[CURLOPT_URL] = $request->getFullUrl();
$curlOptions[CURLOPT_HTTPHEADER] = $request->composeHeaderLines();
return $curlOptions;
}
/**
* Initializes cURL resource.
* @param array $curlOptions cURL options.
* @return resource prepared cURL resource.
*/
private function initCurl(array $curlOptions)
{
$curlResource = curl_init();
foreach ($curlOptions as $option => $value) {
curl_setopt($curlResource, $option, $value);
}
return $curlResource;
}
/**
* Composes cURL options from raw request options.
* @param array $options raw request options.
* @return array cURL options, in format: [curl_constant => value].
*/
private function composeCurlOptions(array $options)
{
static $optionMap = [
'protocolVersion' => CURLOPT_HTTP_VERSION,
'maxRedirects' => CURLOPT_MAXREDIRS,
'sslCapath' => CURLOPT_CAPATH,
'sslCafile' => CURLOPT_CAINFO,
];
$curlOptions = [];
foreach ($options as $key => $value) {
if (is_int($key)) {
$curlOptions[$key] = $value;
} else {
if (isset($optionMap[$key])) {
$curlOptions[$optionMap[$key]] = $value;
} else {
$key = strtoupper($key);
if (strpos($key, 'SSL') === 0) {
$key = substr($key, 3);
$constantName = 'CURLOPT_SSL_' . $key;
if (!defined($constantName)) {
$constantName = 'CURLOPT_SSL' . $key;
}
} else {
$constantName = 'CURLOPT_' . strtoupper($key);
}
$curlOptions[constant($constantName)] = $value;
}
}
}
return $curlOptions;
}
/**
* Setup a variable, which should collect the cURL response headers.
* @param resource $curlResource cURL resource.
* @param array $output variable, which should collection headers.
*/
private function setHeaderOutput($curlResource, array &$output)
{
curl_setopt($curlResource, CURLOPT_HEADERFUNCTION, function($resource, $headerString) use (&$output) {
$header = trim($headerString, "\n\r");
if (strlen($header) > 0) {
$output[] = $header;
}
return mb_strlen($headerString, '8bit');
});
}
}