要分别从国内和国外各测试一份结果
国内不行,国外 OK 才是被墙
另外结合网上的资料,给你优化了一份,这个我之前项目也有用到,你上面发的只能判断 HTTP 服务
<?php
function checkGfw($domainList, $timeout = 3)
{
$res = [];
foreach ($domainList as $domian) {
$domainArr = explode(':', $domian);
if (!isset($domainArr[1])) { //没有写端口则指定为 80
$domainArr[1] = '80';
}
$res[$domian] = socketTest($domainArr,$timeout);
}
return $res;
}
function socketTest($domainArr, $timeout)
{
$start = time();
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_nonblock($sock);
$res = false;
while (!@socket_connect($sock, $domainArr[0], $domainArr[1])) {
$err = socket_last_error($sock);
if ($err === 56) {
socket_close($sock);
$res = true;
break;
}
if ((time() - $start) >= $timeout) {
socket_close($sock);
$res = false;
break;
}
usleep(250000);
}
// socket_set_block($sock);
return $res;
}
$domainList = [
'
www.baidu.com',
'
www.google.com',
'
www.hao123.com:8099'
];
$res = checkGfw($domainList);
var_dump($res);