【发布时间】:2020-10-08 08:36:02
【问题描述】:
当我尝试连接到离线或不存在的主机时,出现异常:
cURL error 6: Could not resolve host: somedomain.com
那么你如何使用这些东西呢? (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;
我需要知道主机是否在线/离线。
编辑:
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';
}
}
它对我不起作用,我总是得到:cURL 错误 6:无法解析主机:somedomain.com
【问题讨论】:
-
你有离线主机测试吗?
-
嗯。事实上,我没有。我会得到一个域名或ip地址,需要检查服务器是否在线。有时我得到一个不存在的域。