When I try to connect to an offline or non-existent host, I get an exception:
cURL error 6: Could not resolve host: somedomain.com
How do you use these things then? (https://laravel.com/docs/8.x/http-client)
$response->body() : string;
$response->json() : array|mixed;
$response->status() : int;
$response->ok() : bool;
$response->successful() : bool;
$response->failed() : bool;
$response->serverError() : bool;
$response->clientError() : bool;
$response->header($header) : string;
$response->headers() : array;
I need to know if the host is online / offline.
Edit:
use Illuminate\Support\Facades\Http;
use GuzzleHttp\Exception\RequestException;
public function checkSiteStatus($host)
{
try {
return Http::timeout(2)->get($host);
} catch (RequestException $e) {
//Access the message or other type of errors and react to them
if (!$e->hasResponse()) {
//No response from server. Assume the host is offline or server is overloaded.
return 'offline';
}
return 'offline';
}
}
It doesn't work for me, I always get: cURL error 6: Could not resolve host: somedomain.com