Refactor HTTP request handling for response status check

- Added try-catch block to handle GuzzleException
- Updated logic to check for response status before processing
This commit is contained in:
Jeroen De Meerleer 2024-06-03 13:55:46 +02:00
parent 98fa29d900
commit d0be3daa89
No known key found for this signature in database
GPG Key ID: 3B23D1B3498D7641

View File

@ -23,10 +23,18 @@ if($_SERVER["REQUEST_URI"] == '/metrics' || php_sapi_name() == 'cli') {
}; };
} }
$hasresponse = NULL;
try{
$res = $client->request('GET', $c['http']['url'], $options); $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') { if (isset($c['http']['data']) && $c['http']['data'] == 'responsebody') {
$output = $res->getBody()->getContents(); $output = $res->getBody()->getContents();
} elseif (isset($c['http']['data']) && $c['http']['data'] == 'hasresponse') {
$output = (int)$hasresponse;
} }
} }