Refactor Guzzle HTTP requests for flexibility and proxy support

Restructured Guzzle HTTP requests to allow for additional options like proxy support, enhancing flexibility in handling different types of requests.
This commit is contained in:
Jeroen De Meerleer 2024-06-03 19:48:39 +02:00
parent a726fa024c
commit 638e6d9346
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6

View File

@ -11,15 +11,28 @@ if($_SERVER["REQUEST_URI"] == '/metrics') {
$output = implode("\n", $output); $output = implode("\n", $output);
} elseif(isset($c['httpurl'])) { } elseif(isset($c['httpurl'])) {
$client = new GuzzleHttp\Client(); $client = new GuzzleHttp\Client();
$res = $client->get($c['httpurl']);
$options = [];
if (isset($c['proxy'])) {
$options['proxy'] = $c['proxy'];
}
$res = $client->request('GET', $c['httpurl'], $options);
$output = $res->getBody()->getContents(); $output = $res->getBody()->getContents();
} elseif(isset($c['httpresponsetime'])) { } elseif(isset($c['httpresponsetime'])) {
$client = new GuzzleHttp\Client(); $client = new GuzzleHttp\Client();
$output = NULL; $output = NULL;
$res = $client->request('GET', $c['httpresponsetime'], [
$options = [
'on_stats' => function (GuzzleHttp\TransferStats $stats) use (&$output) { 'on_stats' => function (GuzzleHttp\TransferStats $stats) use (&$output) {
$output = $stats->getTransferTime(); $output = $stats->getTransferTime();
}]); }
];
if (isset($c['proxy'])) {
$options['proxy'] = $c['proxy'];
}
$res = $client->request('GET', $c['httpresponsetime'], $options);
} }
if(isset($c['jsonelem'])) { if(isset($c['jsonelem'])) {
$c['value'] = getArrayValue($c['jsonelem'], json_decode($output, true)); $c['value'] = getArrayValue($c['jsonelem'], json_decode($output, true));