cURL Recaptcha 不工作 PHP

2022-08-30 17:26:38

我有什么

$data = array(
            'secret' => "my-app-secret",
            'response' => "the-response"
        );

$verify = curl_init();
curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($verify, CURLOPT_POST, true);
curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($verify);

var_dump($response);

我得到的:(这意味着失败)bool(false)curl_exec()

我期望:JSON对象响应

请帮忙。谢谢。


答案 1

由于您尝试通过 SSL 进行连接,因此需要调整 cURL 选项来处理它。使其正常工作的快速解决方法是,如果您添加curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);

设置为 false 将使它接受提供给它的任何证书,而不是验证它们。CURLOPT_SSL_VERIFYPEER

<?php

$data = array(
            'secret' => "my-secret",
            'response' => "my-response"
        );

$verify = curl_init();
curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($verify, CURLOPT_POST, true);
curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($verify);

var_dump($response);

答案 2

这是我找到的cURL的另一种方法,如果它可以帮助任何人。显然,输入$secret$response变量以正确传递它。很抱歉,这个问题要求一个cURL解决方案,但这是我见过的最快的方法,所以我认为它无论如何都会添加它,因为我知道它会帮助那里的某个人。:)

$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$response);
$response = json_decode($response, true);
if($response["success"] === true){
    // actions if successful
}else{
    // actions if failed
}

推荐