在linode服务器上重新捕获速度非常慢


recaptcha very slow on linode server

我正在使用以下PHP来验证谷歌摘要:

函数checkCapcha($response,$ip){

        try {

            $url = 'https://www.google.com/recaptcha/api/siteverify';
            $data = ['secret'   => $secret,
                     'response' => $response,
                     'remoteip' => $ip];
            $options = [
                'http' => [
                    'header'  => "Content-type: application/x-www-form-urlencoded'r'n",
                    'method'  => 'POST',
                    'content' => http_build_query($data) 
                ]
            ];
            $context  = stream_context_create($options);
            $result = file_get_contents($url, false, $context);
            //return json_decode($result)->success;
             //echo $result;
            if(json_decode($result)->success){
                return true;
            }else{
                return false;
            }
        }
        catch (Exception $e) {
            return null;
        }
}

它在我的家庭服务器上运行得很好,但当我在linode上使用相同的代码时,需要很长时间才能返回结果。结果是正确的,但大约需要3分钟。

有人能给我建议吗。

提前感谢

我使用Centos 7时遇到了同样的问题。

首先添加一个www.google.com /etc/hosts记录,如果这解决了您的问题,则关闭服务器上的IPV6。

  1. /etc/sysctl.conf:中的行后面追加

    net.ipv6.conf.all.disable_ipv6 = 1
    net.ipv6.conf.default.disable_ipv6 = 1
    
  2. 要使设置生效,请执行:

    sysctl -p
    

我也有同样的问题。从Google API获取响应平均需要2000毫秒。

问题是,如果我使用curl,它会很慢。如果我切换回普通的file_get_contents功能,它的工作速度会快得多(即正常计时)。

现在,当我认为curl可能是罪魁祸首时,并不是当我瞄准另一个HTTPS API时,它工作得非常快。

curl花费99%的时间等待google API回复内容,即[starttransfer_time] => 2.332313

这真的很奇怪。

因此,解决方案是在这个用例中避免使用curl,并切换到file_get_contents,即使它不是罪魁祸首!