我有什么:
$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);
Run Code Online (Sandbox Code Playgroud)
我得到了什么 :( bool(false)
这意味着curl_exec()
失败)
我的期望:JSON对象响应
请帮忙.谢谢.
小智 40
因为您尝试通过SSL进行连接,所以需要调整cURL选项来处理它.如果你添加,快速解决这个问题就可以了curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
设置CURLOPT_SSL_VERIFYPEER
为false将使其接受任何给予它的证书而不是验证它们.
<?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);
Run Code Online (Sandbox Code Playgroud)
这是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
}
Run Code Online (Sandbox Code Playgroud)
小智 5
使用带有 POST 的“file_get_contents”更容易:
$postdata = http_build_query( [
'secret' => YOUR_SECRET_KEY,
'response' => $_POST[ 'g-recaptcha-response' ]
] );
$opts = [
'http' => [
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
]
];
$context = stream_context_create( $opts );
$result = file_get_contents(
'https://www.google.com/recaptcha/api/siteverify', false, $context
);
$check = json_decode( $result );
if( $check->success ) {
echo "validate";
} else {
echo "wrong recaptcha";
}
Run Code Online (Sandbox Code Playgroud)