使用 dig 命令,並限制超時時間2秒:
public function check_connection()
{
$response = shell_exec('dig +retries=1 +timeout=2 www.sina.com.cn');
if (strpos($response, 'Got answer') !== false) {
return 1;
}
return 0;
}
試了 fsockopen
和調用 curl
命令,超時時間設置都不生效。用 dig
命令超時時間設置生效。