-
Notifications
You must be signed in to change notification settings - Fork 0
/
Curl.php
211 lines (166 loc) · 6.4 KB
/
Curl.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
<?php
namespace rogoss\Curl;
require_once __DIR__ . "/FetchResult.php";
class Curl
{
// BM: Builders
//==========================================================================
public static function GET(): static
{
return new static("GET");
}
public static function POST(): static
{
return new static("POST");
}
public static function PUT(): static
{
return new static("PUT");
}
public static function DELETE(): static
{
return new static("DELETE");
}
// BM: Props/Methods
//==========================================================================
protected $oHandler;
private string $sRequestMethod = "GET";
private string $sURL = "";
private bool $bDebugEnabled = false;
private array $aHeaders = [];
private array $aRequestparams = [];
private string $sRequestBody = "";
protected function __construct($sMethod)
{
$this->oHandler = curl_init();
$this->setCurlOpt("Method", CURLOPT_CUSTOMREQUEST, $sMethod);
$this->sRequestMethod = $sMethod;
}
/**
* Set the URL to which to send the Request
* @param string $sURL = URL to send the Request to
* @return static = builder-pattern return
*/
public function url($sURL): static
{
$this->setCurlOpt("URL", CURLOPT_URL, $sURL);
$this->sURL = $sURL;
return $this;
}
/**
* Sets the Request-Body / Get-Params to the given array
* @param array $aPostFields = your Request-Body
* @return static = builder-pattern return
*/
public function postFields(array $aPostFields): static
{
$this->aRequestparams = $aPostFields;
return $this;
}
public function param($sParamName, $mParamValue): static
{
$this->aRequestparams[$sParamName] = $mParamValue;
return $this;
}
public function body(string $sRequestBody): static {
$this->sRequestBody = $sRequestBody;
return $this;
}
public function debug($bEnabled): static
{
$this->bDebugEnabled = $bEnabled == true;
return $this;
}
public function header(string $sHeaderName, string $sHeaderValue): static
{
$sNormalizedHeader = strtolower(trim($sHeaderName));
$sNormalizedHeaderValue = str_replace(["\r\n", "\n"], " ", $sHeaderValue);
if (strcmp($sNormalizedHeaderValue, $sHeaderValue))
trigger_error("header values should not contain any line breaks", E_USER_WARNING);
if (strcmp($sNormalizedHeader, $sHeaderName))
trigger_error("given header names should be lower case and not contain whitespaces at beginning and end", E_USER_WARNING);
$this->aHeaders[$sNormalizedHeader] = $sNormalizedHeaderValue;
return $this;
}
/**
* adds a CurlOpt to the current Curl-Request
* @param int $cUrlOpt = one of the CURLOPT_ - constants
* @param mixed $mValue = the value to set for that function
* @return static = builder-pattern return
*/
public function customCurlOpt($cUrlOpt, $mValue): static
{
$this->setCurlOpt("CustomCurlOpt", $cUrlOpt, $mValue);
return $this;
}
function exec(): FetchResult|null
{
if (empty($this->sURL))
throw new CurlException($this->oHandler, "missing URL", CurlException::CURL_NO_URL);
$this->setCurlOpt("exec", CURLOPT_RETURNTRANSFER, 1);
$this->setCurlOpt("exec", CURLOPT_SSL_VERIFYPEER, 0);
$this->setCurlOpt("exec", CURLOPT_HEADER, 1);
if ($this->bDebugEnabled)
$this->setCurlOpt("exec", CURLOPT_VERBOSE, true);
$this->setCurlOpt("exec", CURLOPT_HTTPHEADER, $aHeaders = array_map(
fn($i, $e) => "$i: $e",
array_keys($this->aHeaders),
array_values($this->aHeaders)
));
self::log("headers:", $aHeaders);
$url = $this->sURL;
$body = implode("&", array_map(
fn($i, $e) => urlencode($i) . "=" . urlencode($e),
array_keys($this->aRequestparams),
array_values($this->aRequestparams)
));
$bHasCustomReqBody = false;
switch ($this->sRequestMethod) {
case "POST":
case "PUT":
if(empty($this->sRequestBody)) {
$this->setCurlOpt("exec", CURLOPT_POSTFIELDS, $body);
break;
} else {
$this->setCurlOpt("exec", CURLOPT_POSTFIELDS, $this->sRequestBody);
$bHasCustomReqBody = true;
if(empty($body)) {
break;
} else trigger_error(
"passed Request-Params and a Request-Body to a POST/PUT-CURL Request. Request-Params will be converted into GET-Parameters",
E_USER_NOTICE
);
}
default:
$url .= ((strpos($url, "?") === false) ? "?" : "&") . $body;
$body = $bHasCustomReqBody ? $this->sRequestBody : "";
$this->setCurlOpt("exec", CURLOPT_URL, $url);
}
$this->log("Call: ", strtoupper($this->sRequestMethod) . " " . $url . "\n\nHeaders:\n" . implode("\n", $aHeaders) . "\n\nBody:", $body);
if ($this->bDebugEnabled) {
$streamVerboseHandle = fopen('php://temp', 'w+');
curl_setopt($this->oHandler, CURLOPT_STDERR, $streamVerboseHandle);
}
$result = curl_exec($this->oHandler);
if ($this->bDebugEnabled) {
rewind($streamVerboseHandle);
$verboseLog = stream_get_contents($streamVerboseHandle);
$this->log("Verbose information:", $verboseLog);
}
if ($result === false) return null;
return new FetchResult($result);
}
// BM: Private Helpers
//==========================================================================
private function setCurlOpt($sOptName, $cOpt, $mValue)
{
if (!curl_setopt($this->oHandler, $cOpt, $mValue) || curl_errno($this->oHandler))
throw new CurlException($this->oHandler, "failed ot set a curl_setopt '{$sOptName}' ", CurlException::CURL_SETOPTS_ERROR);
}
private function log()
{
if ($this->bDebugEnabled)
foreach (func_get_args() as $mArgs)
echo "[", __CLASS__, " Debug] ", var_export($mArgs, true), "<br />\n";
}
}