From 1dfddd64ba53bc1741fb377f053e5724f06eb429 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Mon, 3 Jun 2024 13:33:43 +0200 Subject: [PATCH] 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. --- index.php | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/index.php b/index.php index 6a0b74d..1316c67 100644 --- a/index.php +++ b/index.php @@ -11,15 +11,28 @@ if($_SERVER["REQUEST_URI"] == '/metrics') { $output = implode("\n", $output); } elseif(isset($c['httpurl'])) { $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(); } elseif(isset($c['httpresponsetime'])) { $client = new GuzzleHttp\Client(); $output = NULL; - $res = $client->request('GET', $c['httpresponsetime'], [ - 'on_stats' => function (GuzzleHttp\TransferStats $stats) use (&$output) { - $output = $stats->getTransferTime(); - }]); + + $options = [ + 'on_stats' => function (GuzzleHttp\TransferStats $stats) use (&$output) { + $output = $stats->getTransferTime(); + } + ]; + if (isset($c['proxy'])) { + $options['proxy'] = $c['proxy']; + } + + $res = $client->request('GET', $c['httpresponsetime'], $options); } if(isset($c['jsonelem'])) { $c['value'] = getArrayValue($c['jsonelem'], json_decode($output, true));