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
|
|
|
|
23
|
|
|
class Curl |
24
|
|
|
{ |
25
|
|
|
private $requestModifierPipeline; |
26
|
|
|
private $curlResponseFactory; |
27
|
|
|
private $curlRequestFactory; |
28
|
|
|
|
29
|
|
|
public function __construct(?string $host = null) |
30
|
|
|
{ |
31
|
|
|
$this->requestModifierPipeline = new RequestModifierPipeline(); |
32
|
|
|
$this->curlResponseFactory = new CurlResponseFactory(); |
33
|
|
|
$this->curlRequestFactory = new CurlRequestFactory($this, $host); |
34
|
|
|
|
35
|
|
|
$this->requestModifierPipeline |
36
|
|
|
->pipe(new FileRequestModifier()) |
37
|
|
|
->pipe(new PlainTextRequestModifier()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $url |
42
|
|
|
* @param null|array|string $query |
43
|
|
|
* @param array $headers |
44
|
|
|
* @return CurlRequest |
45
|
|
|
*/ |
46
|
4 |
|
public function get(string $url, $query = null, array $headers = []) |
47
|
|
|
{ |
48
|
4 |
|
return $this->curlRequestFactory->create('GET', $url, null, $query, $headers); |
49
|
|
|
} |
50
|
|
|
|
51
|
5 |
|
public function post(string $url, $data = null, $query = null, array $headers = []) |
52
|
|
|
{ |
53
|
5 |
|
return $this->curlRequestFactory->create('POST', $url, $data, $query, $headers); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public function put(string $url, $data = null, $query = null, array $headers = []) |
57
|
|
|
{ |
58
|
1 |
|
return $this->curlRequestFactory->create('PUT', $url, $data, $query, $headers); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public function patch(string $url, $data = null, $query = null, array $headers = []) |
62
|
|
|
{ |
63
|
1 |
|
return $this->curlRequestFactory->create('PATCH', $url, $data, $query, $headers); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
public function delete(string $url, $data = null, $query = null, array $headers = []) |
67
|
|
|
{ |
68
|
1 |
|
return $this->curlRequestFactory->create('DELETE', $url, $data, $query, $headers); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param CurlRequest $request |
73
|
|
|
* @return resource |
74
|
|
|
*/ |
75
|
12 |
|
protected function prepare(CurlRequest $request) |
76
|
|
|
{ |
77
|
12 |
|
$request = $this->requestModifierPipeline->process($request); |
78
|
|
|
|
79
|
|
|
// create curl resource |
80
|
12 |
|
$ch = curl_init(); |
81
|
|
|
|
82
|
12 |
|
curl_setopt($ch, CURLOPT_URL, $request->getUrl()); |
83
|
12 |
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getData()); |
84
|
12 |
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request->getMethod()); |
85
|
12 |
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $request->getHeaders()->toHttp()); |
86
|
|
|
|
87
|
12 |
|
foreach ($request->getCurlOptions()->all() as $key => $value) { |
88
|
12 |
|
curl_setopt($ch, $key, $value); |
89
|
|
|
} |
90
|
|
|
|
91
|
12 |
|
return $ch; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param CurlRequest $request |
96
|
|
|
* @return CurlResponse |
97
|
|
|
* @throws CurlError |
98
|
|
|
*/ |
99
|
12 |
|
public function execute(CurlRequest $request): CurlResponse |
100
|
|
|
{ |
101
|
12 |
|
$ch = $this->prepare($request); |
102
|
|
|
|
103
|
|
|
// $output contains the output string |
104
|
12 |
|
$output = curl_exec($ch); |
105
|
|
|
|
106
|
12 |
|
if ($output === false) { |
107
|
|
|
$message = curl_error($ch); |
108
|
|
|
$code = curl_errno($ch); |
109
|
|
|
|
110
|
|
|
// close curl resource to free up system resources |
111
|
|
|
curl_close($ch); |
112
|
|
|
throw new CurlError($message, $code); |
113
|
|
|
} |
114
|
|
|
|
115
|
12 |
|
$info = curl_getinfo($ch); |
116
|
12 |
|
curl_close($ch); |
117
|
|
|
|
118
|
12 |
|
return $this->curlResponseFactory->create($output, $info); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param CurlRequest[] $requests |
123
|
|
|
* @return CurlResponse[] |
124
|
|
|
*/ |
125
|
|
|
public function executeMulti(array $requests): array |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
return [new CurlResponse()]; |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return RequestModifierPipeline |
132
|
|
|
*/ |
133
|
|
|
public function getRequestModifierPipeline(): RequestModifierPipeline |
134
|
|
|
{ |
135
|
|
|
return $this->requestModifierPipeline; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return CurlRequestFactory |
140
|
|
|
*/ |
141
|
|
|
public function getCurlRequestFactory(): CurlRequestFactory |
142
|
|
|
{ |
143
|
|
|
return $this->curlRequestFactory; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return CurlResponseFactory |
148
|
|
|
*/ |
149
|
|
|
public function getCurlResponseFactory(): CurlResponseFactory |
150
|
|
|
{ |
151
|
|
|
return $this->curlResponseFactory; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.