[英]efficient way to get http status response code
Is there a quicker and more efficient way to check if a website shows status codes 200 etc?是否有更快更有效的方法来检查网站是否显示状态代码 200 等?
Basically I want to check specific pages whether they're online of offline.基本上我想检查特定页面是否在线或离线。 Some of the pages I check go down sporadically with 502 errors, so I need a way to also make sure those are displayed as "offline" thus using the header response check.
我检查 go 的某些页面偶尔会出现 502 错误,因此我需要一种方法来确保这些页面也显示为“离线”,从而使用 header 响应检查。
when using the below function on say 5-10 pages, it slows the page loading time dramatically.当在 5-10 页上使用下面的 function 时,它会显着减慢页面加载时间。
<?php
function getHttpResponseCode( string $url ) //: int
{
$headers = get_headers( $url );
return substr( $headers[ 0 ], 9, 3 );
}
if (getHttpResponseCode('https://www.google.com') !='200') {
echo 'offline';
} else {
echo 'online';
}
?>
**UPDATED CODE (not working atm) ** **更新的代码(不工作的atm)**
<?php
function checkDomain($host) {
if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) {
echo 'online!';
fclose($socket);
} else {
echo 'offline.';
}
}
echo checkDomain('https://www.google.com');
?>
The speed should inprove considerably using this:使用这个速度应该会大大提高:
$host = 'google.com';
if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) {
echo 'online!';
fclose($socket);
} else {
echo 'offline.';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.