在PHP中,可以使用curl
函数来判断网络连接超时。以下是一个示例代码:
function isNetworkTimeout($url, $timeout) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode == 200) {
return false;
} else {
return true;
}
}
$url = "http://www.example.com";
$timeout = 5; // 设置超时时间为5秒
if (isNetworkTimeout($url, $timeout)) {
echo "网络连接超时";
} else {
echo "网络连接正常";
}
在上述代码中,isNetworkTimeout
函数会发送一个curl
请求到指定的URL,并设置超时时间为指定的秒数。如果请求返回的HTTP状态码为200,则表示网络连接正常;否则,表示网络连接超时。
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站