[英]cURL Recaptcha not working PHP
What I have :我有什么:
$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);
What I got : bool(false)
(which means the curl_exec()
failed)我得到的是: bool(false)
(这意味着curl_exec()
失败)
What I expect : a JSON object response我期望的是:JSON 对象响应
Please help.请帮忙。 Thanks.谢谢。
Because you're attempting to connect via SSL, you need to adjust your cURL options to handle it.由于您尝试通过 SSL 进行连接,因此您需要调整 cURL 选项来处理它。 A quick fix to get this to work is if you add curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
工作的一个快速解决方法是添加curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
Setting CURLOPT_SSL_VERIFYPEER
to false will make it so that it accepts any certificate given to it rather than verifying them.将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);
Here's an alternative method to cURL I found if it helps anyone.这是我发现的 cURL 的替代方法,如果它对任何人有帮助的话。 Obviously input $secret and $response variables to pass it correctly.显然输入$secret和$response变量以正确传递它。 Sorry the question is asking for a cURL solution, but this is the quickest method I've seen so I thought it would add it anyway because I know it will help somebody out there.抱歉,问题是要求 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
}
It's easier using "file_get_contents" with POST:使用带有 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";
}
usually I will do something like this通常我会做这样的事情
public static function validateRecaptcha($recaptcha)
{
$dataArr = [
'secret' => 'secret_key',
'response' => $recaptcha,
'remoteip'=> $_SERVER['REMOTE_ADDR'] //optional field
];
$url = "https://www.google.com/recaptcha/api/siteverify";
try
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($dataArr));
$response = curl_exec($curl);
//get error code
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($responseCode >= 400)
{
throw new Exception ("Response error code: {$responseCode}");
}
$response = json_decode($response, true);
if ($response['success'] === true)
{
// Success
return 'success';
}
throw new Exception ($response);
}
catch(Exception $e)
{
return $e->getMessage();
}
}
for those who prefer to use shell_exec curl :对于那些喜欢使用 shell_exec curl 的人:
$arrayku = array();
$arrayku['secret'] = 'Your_SECRET_KEY';
$arrayku['response'] = trim($_POST['g-recaptcha-response']);
$dataku = http_build_query($arrayku);
$respon_google = shell_exec('curl -s -L -X POST "https://www.google.com/recaptcha/api/siteverify" --data "'.$dataku.'" --max-time 10 --compressed');
$array_respon = json_decode($respon_google, true);
if($array_respon['success']===false){
echo 'Wrong recaptcha'; echo PHP_EOL;
//goto wrong_captcha;
exit;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.