forked from RobinTheHood/ModifiedModuleLoaderClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequest.php
More file actions
219 lines (182 loc) · 6.97 KB
/
HttpRequest.php
File metadata and controls
219 lines (182 loc) · 6.97 KB
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
<?php
declare(strict_types=1);
/*
* This file is part of MMLC - ModifiedModuleLoaderClient.
*
* (c) Robin Wieschendorf <mail@robinwieschendorf.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace RobinTheHood\ModifiedModuleLoaderClient\Api\V1;
use RuntimeException;
use RobinTheHood\ModifiedModuleLoaderClient\Logger\LogLevel;
use RobinTheHood\ModifiedModuleLoaderClient\Logger\StaticLogger;
class HttpRequest
{
public function isServerAvailable(string $url): bool
{
$headers = @get_headers($url);
if ($headers) {
return true;
}
return false;
}
/**
* @throws RuntimeException
*/
public function sendPostRequest(string $url, $data): string
{
//$result = $this->sendCurlPostRequest($url, $data);
$result = $this->sendFileGetContentsPostRequest($url, $data);
return $result;
}
/**
* @throws RuntimeException
*/
public function sendGetRequest(string $url): string
{
//$result = $this->sendCurlGetRequest($url);
$result = $this->sendFileGetContentsGetRequest($url);
return $result;
}
public static function createQuery(array $queryValues): string
{
$query = '';
foreach ($queryValues as $name => $value) {
$query .= $name . '=' . urlencode((string) $value) . '&';
}
return $query;
}
/**
* @throws RuntimeException
*/
private function sendCurlGetRequest(string $url): string
{
// HTTP GET-Request konfigurieren
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Modified Module Loader Client');
StaticLogger::log(
LogLevel::DEBUG,
"Send GET request to $url"
);
// Request senden und Ergebnis erhalten
$result = curl_exec($curl);
// HTTP-Statuscode und Fehler prüfen
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($result === false) {
$error = curl_error($curl);
curl_close($curl);
StaticLogger::log(LogLevel::ERROR, "$httpCode Error-Response from $url\n$error");
throw new RuntimeException('Error sending the GET request: ' . $error);
} elseif ($httpCode < 200 || $httpCode >= 300) {
curl_close($curl);
StaticLogger::log(LogLevel::ERROR, "$httpCode Error-Response from $url");
throw new RuntimeException('Error sending the GET request: HTTP-Statuscode ' . $httpCode);
}
// Request beenden
curl_close($curl);
StaticLogger::log(LogLevel::DEBUG, "$httpCode Response from $url\n" . print_r($result, true));
return (string) $result;
}
/**
* @throws RuntimeException
*/
private function sendCurlPostRequest(string $url, $data): string
{
// HTTP POST-Request konfigurieren
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Modified Module Loader Client');
StaticLogger::log(
LogLevel::DEBUG,
"Send POST request to $url\n[DATA]\n" . print_r($data, true)
);
// Request senden und Ergebnis erhalten
$result = curl_exec($curl);
// HTTP-Statuscode und Fehler prüfen
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($result === false) {
$error = curl_error($curl);
curl_close($curl);
StaticLogger::log(LogLevel::ERROR, "$httpCode Error-Response from $url\n$error");
throw new RuntimeException('Error sending the POST request: ' . $error);
} elseif ($httpCode < 200 || $httpCode >= 300) {
curl_close($curl);
StaticLogger::log(LogLevel::ERROR, "$httpCode Error-Response from $url");
throw new RuntimeException('Error sending the POST request: HTTP-Statuscode ' . $httpCode);
}
// Request beenden
curl_close($curl);
return (string) $result;
}
/**
* @throws RuntimeException
*/
private function sendFileGetContentsPostRequest(string $url, $data): string
{
// http verwenden, auch wenn die Url mit https://... beginnt
$options = [
'http' => [
'user_agent' => 'Modified Module Loader Client',
'method' => 'POST',
'header' => implode("\r\n", [
'Content-type: application/x-www-form-urlencoded'
]),
'content' => http_build_query($data)
]
];
$context = stream_context_create($options);
StaticLogger::log(
LogLevel::DEBUG,
"Send POST request to $url\n[OPTIONS]\n" . print_r($options, true) . "[DATA]\n" . print_r($data, true)
);
$timeBeforeRequest = microtime(true);
$result = @file_get_contents($url, false, $context);
if ($result === false) {
$error = error_get_last();
$errorMessage = $error['message'] ?? '';
if (empty($errorMessage) && !\extension_loaded('openssl')) {
$errorMessage = 'The OpenSSL PHP extension is required for https requests.';
}
StaticLogger::log(LogLevel::ERROR, "Error-Response from $url\n$errorMessage");
throw new RuntimeException('Error sending the POST request: ' . $errorMessage);
}
$time = microtime(true) - $timeBeforeRequest;
StaticLogger::log(LogLevel::DEBUG, "Response from $url ($time sec)\n" . print_r($result, true));
return $result;
}
/**
* @throws RuntimeException
*/
private function sendFileGetContentsGetRequest(string $url): string
{
// http verwenden, auch wenn die Url mit https://... beginnt
$options = [
'http' => [
'user_agent' => 'Modified Module Loader Client',
'method' => "GET",
'header' => implode("\r\n", [
'Content-type: text/plain'
])
]
];
$context = stream_context_create($options);
StaticLogger::log(
LogLevel::DEBUG,
"Send GET request to $url\n[OPTIONS]\n" . print_r($options, true)
);
$result = @file_get_contents($url, false, $context);
if ($result === false) {
$error = error_get_last();
$errorMessage = $error['message'] ?? '';
StaticLogger::log(LogLevel::ERROR, "Error-Response from $url\n$errorMessage");
throw new RuntimeException('Error sending the GET request: ' . $errorMessage);
}
StaticLogger::log(LogLevel::DEBUG, "Response from $url\n" . print_r($result, true));
return $result;
}
}