php 检测网络连接情况,限制超时时间

使用 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 命令超时时间设置生效。

 

posted @ 2024-04-28 14:37  imzhi  阅读(2)  评论(0编辑  收藏  举报