From d0be3daa89cf7b236ef7f6e4bbf22ce78571d1e6 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Mon, 3 Jun 2024 13:55:46 +0200 Subject: [PATCH] Refactor HTTP request handling for response status check - Added try-catch block to handle GuzzleException - Updated logic to check for response status before processing --- index.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index 1310042..92aa467 100644 --- a/index.php +++ b/index.php @@ -22,11 +22,19 @@ if($_SERVER["REQUEST_URI"] == '/metrics' || php_sapi_name() == 'cli') { $output = $stats->getTransferTime(); }; } - - $res = $client->request('GET', $c['http']['url'], $options); + + $hasresponse = NULL; + try{ + $res = $client->request('GET', $c['http']['url'], $options); + $hasresponse = true; + } catch(GuzzleHttp\Exception\GuzzleException $e) { + $hasresponse = false; + } if (isset($c['http']['data']) && $c['http']['data'] == 'responsebody') { $output = $res->getBody()->getContents(); + } elseif (isset($c['http']['data']) && $c['http']['data'] == 'hasresponse') { + $output = (int)$hasresponse; } }