1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* php-guard/curl <https://github.com/php-guard/curl> |
4
|
|
|
* Copyright (C) ${YEAR} by Alexandre Le Borgne <[email protected]>. |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace PhpGuard\Curl; |
21
|
|
|
|
22
|
|
|
use PhpGuard\Curl\RequestModifier\FileRequestModifier; |
23
|
|
|
use PhpGuard\Curl\RequestModifier\PlainTextRequestModifier; |
24
|
|
|
|
25
|
|
|
class Curl |
26
|
|
|
{ |
27
|
|
|
private $requestModifierPipeline; |
28
|
|
|
private $curlResponseFactory; |
29
|
|
|
private $curlRequestFactory; |
30
|
|
|
|
31
|
|
|
public function __construct(?string $host = null) |
32
|
|
|
{ |
33
|
|
|
$this->requestModifierPipeline = new RequestModifierPipeline(); |
34
|
|
|
$this->curlResponseFactory = new CurlResponseFactory(); |
35
|
|
|
$this->curlRequestFactory = new CurlRequestFactory($this, $host); |
36
|
|
|
|
37
|
|
|
$this->requestModifierPipeline |
38
|
|
|
->pipe(new FileRequestModifier()) |
39
|
|
|
->pipe(new PlainTextRequestModifier()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $url |
44
|
|
|
* @param null|array|string $query |
45
|
|
|
* @param array $headers |
46
|
|
|
* |
47
|
|
|
* @return CurlRequest |
48
|
|
|
*/ |
49
|
5 |
|
public function get(string $url, $query = null, array $headers = []) |
50
|
|
|
{ |
51
|
5 |
|
return $this->curlRequestFactory->create('GET', $url, null, $query, $headers); |
52
|
|
|
} |
53
|
|
|
|
54
|
6 |
|
public function post(string $url, $data = null, $query = null, array $headers = []) |
55
|
|
|
{ |
56
|
6 |
|
return $this->curlRequestFactory->create('POST', $url, $data, $query, $headers); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function put(string $url, $data = null, $query = null, array $headers = []) |
60
|
|
|
{ |
61
|
1 |
|
return $this->curlRequestFactory->create('PUT', $url, $data, $query, $headers); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
public function patch(string $url, $data = null, $query = null, array $headers = []) |
65
|
|
|
{ |
66
|
1 |
|
return $this->curlRequestFactory->create('PATCH', $url, $data, $query, $headers); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
public function delete(string $url, $data = null, $query = null, array $headers = []) |
70
|
|
|
{ |
71
|
1 |
|
return $this->curlRequestFactory->create('DELETE', $url, $data, $query, $headers); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param CurlRequest[] $requests |
76
|
|
|
* @param array $options Définit les options pour le gestionnaire multiple cURL |
77
|
|
|
* |
78
|
|
|
* @return CurlResponse[] |
79
|
|
|
* |
80
|
|
|
* @throws CurlError |
81
|
|
|
*/ |
82
|
1 |
|
public function multi(array $requests, array $options = []) |
83
|
|
|
{ |
84
|
1 |
|
$mh = curl_multi_init(); |
85
|
|
|
|
86
|
1 |
|
foreach ($options as $key => $value) { |
87
|
|
|
curl_multi_setopt($mh, $key, $value); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
$chs = []; |
91
|
1 |
|
foreach ($requests as $request) { |
92
|
1 |
|
$ch = $this->prepare($request); |
93
|
1 |
|
$chs[] = $ch; |
94
|
1 |
|
curl_multi_add_handle($mh, $ch); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
$active = null; |
98
|
|
|
|
99
|
|
|
do { |
100
|
1 |
|
$code = curl_multi_exec($mh, $active); |
101
|
1 |
|
curl_multi_select($mh); |
102
|
1 |
|
} while ($active > 0); |
103
|
|
|
|
104
|
|
|
try { |
105
|
1 |
|
if ($code > CURLM_OK) { |
106
|
|
|
throw new CurlError(curl_multi_strerror($code), $code); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
$responses = []; |
110
|
1 |
|
foreach ($chs as $ch) { |
111
|
1 |
|
$responses[] = $this->curlResponseFactory->create($ch, curl_multi_getcontent($ch)); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
return $responses; |
115
|
|
|
} finally { |
116
|
1 |
|
foreach ($chs as $ch) { |
117
|
1 |
|
curl_multi_remove_handle($mh, $ch); |
118
|
|
|
} |
119
|
1 |
|
curl_multi_close($mh); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param CurlRequest $request |
125
|
|
|
* |
126
|
|
|
* @return CurlResponse |
127
|
|
|
* |
128
|
|
|
* @throws CurlError |
129
|
|
|
*/ |
130
|
12 |
|
public function execute(CurlRequest $request): CurlResponse |
131
|
|
|
{ |
132
|
12 |
|
$ch = $this->prepare($request); |
133
|
|
|
|
134
|
|
|
try { |
135
|
12 |
|
return $this->curlResponseFactory->create($ch, curl_exec($ch)); |
136
|
|
|
} finally { |
137
|
12 |
|
curl_close($ch); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param CurlRequest $request |
143
|
|
|
* |
144
|
|
|
* @return resource |
145
|
|
|
*/ |
146
|
13 |
|
protected function prepare(CurlRequest $request) |
147
|
|
|
{ |
148
|
13 |
|
$request = $this->requestModifierPipeline->process($request); |
149
|
|
|
|
150
|
|
|
// create curl resource |
151
|
13 |
|
$ch = curl_init(); |
152
|
|
|
|
153
|
13 |
|
curl_setopt($ch, CURLOPT_URL, $request->getUrl()); |
154
|
13 |
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getData()); |
155
|
13 |
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request->getMethod()); |
156
|
13 |
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $request->getHeaders()->toHttp()); |
157
|
|
|
|
158
|
13 |
|
foreach ($request->getCurlOptions()->all() as $key => $value) { |
159
|
13 |
|
curl_setopt($ch, $key, $value); |
160
|
|
|
} |
161
|
|
|
|
162
|
13 |
|
return $ch; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return RequestModifierPipeline |
167
|
|
|
*/ |
168
|
|
|
public function getRequestModifierPipeline(): RequestModifierPipeline |
169
|
|
|
{ |
170
|
|
|
return $this->requestModifierPipeline; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return CurlRequestFactory |
175
|
|
|
*/ |
176
|
|
|
public function getCurlRequestFactory(): CurlRequestFactory |
177
|
|
|
{ |
178
|
|
|
return $this->curlRequestFactory; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return CurlResponseFactory |
183
|
|
|
*/ |
184
|
|
|
public function getCurlResponseFactory(): CurlResponseFactory |
185
|
|
|
{ |
186
|
|
|
return $this->curlResponseFactory; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|