【问题标题】:Google recaptcha API POST request giving 403 Forbidden ErrorGoogle recaptcha API POST 请求给出 403 Forbidden Error
【发布时间】:2019-12-21 16:47:54
【问题描述】:

POST 请求给出以下请求的禁止错误。

来自谷歌代码存档https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/recaptcha/recaptcha-php-1.11.zip的代码

函数调用如下

$response = _recaptcha_http_post (RECAPTCHA_VERIFY_SERVER, "/recaptcha/api/siteverify",
                                      array (
                                             'privatekey' => $privkey,
                                             'remoteip' => $remoteip,
                                             'response' => $response
                                             ) + $extra_params
                                      );

函数低于

function _recaptcha_http_post($host, $path, $data, $port = 80) {

    $req = _recaptcha_qsencode ($data);

    $http_request  = "POST $path HTTP/1.0\r\n";
    $http_request .= "Host: $host\r\n";
    $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
    $http_request .= "Content-Length: " . strlen($req) . "\r\n";
    $http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
    $http_request .= "\r\n";
    $http_request .= $req;

    echo "HTTP_Request - ";
    echo $http_request;

    $response = '';
    if( false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) {
            die ('Could not open socket');
    }

    fwrite($fs, $http_request);

    while ( !feof($fs) )
            $response .= fgets($fs, 1160); // One TCP-IP packet
    fclose($fs);
    echo "HTTP_Response - ";
    echo $response;
    $response = explode("\r\n\r\n", $response, 2);


    return $response; }

请求和响应如下

HTTP_Request = POST /recaptcha/api/siteverify HTTP/1.0 主机:www.google.com 内容类型:application/x-www-form-urlencoded;内容长度:418的User-Agent:验证码/ PHP专用密钥= 6LdWx7AUAAAAANcawRDmFaZ3mto2mR9BPxuO5Q7V&REMOTEIP = 91.75.176.244&响应= 03AOLTBLRSaTq0r_J2gVWzsmxtidDnGAIeIpfM8jga9qmMD9rfKFm-cymLoT6dWhJjAimn7RmPOx1IPTLoUeiAXS1m8qobI8Yk_kPtmPHRm17lxsFbOKMG6b5-nldm4sSNohW02zz9D0sDkymEnGWXej3bTczvv2ah1PePJCIBG-3Ae0IuJP2EwvD4AXtj5mFXZdomcBqe1zGLny4aiO6-KcRy73MRkvGLYxiWGd8BeV3wQvwTPJWhlsoAQjEtBqwFOnbc9ifeWGrMlXCkcXJRGJBsWUqKZ9VkqeKfs00O4RWN9TYDp7d1DGbQFdj7d5iUIpK4MIIRWJeE P>

HTTP_Response = HTTP/1.0 403 禁止内容类型:文本/html; charset=UTF-8 日期:2019 年 8 月 15 日星期四 07:30:28 GMT 过期时间:2019 年 8 月 15 日星期四 07:30:28 GMT 缓存控制:私有,max-age=0 X-Content-Type-Options: nosniff X-Frame-Options:SAMEORIGIN X-XSS-Protection:1;模式=块服务器:GSE 接受范围:无变化:接受编码

【问题讨论】:

  • 出于好奇。为什么不使用请求库,例如狂饮或卷曲?它对正确的请求格式有很大帮助
  • 以上代码来自谷歌代码存档中 reCAPTCHA 的 PHP 客户端库
  • 我强烈建议找一个更新的库来使用

标签: php post recaptcha http-status-code-403 fsockopen


【解决方案1】:

403 错误是由于请求服务器阻止了出站 https 连接。 在服务器设置中允许 https 连接时。错误消失了。

在下面的代码中使用而不是可达性库。

function isValid() {try {

    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $data = ['secret'   => 'YOUR SECRET KEY',
             'response' => $_POST['g-recaptcha-response'],
             'remoteip' => $_SERVER['REMOTE_ADDR']];

    $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;
}
catch (Exception $e) {
    echo "Exception";
    return null;
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多