我有什么:
$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对象响应
请帮助。谢谢。
因为您尝试通过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);
这里是我发现的另一种cURL方法,如果它对任何人都有帮助的话。显然,输入$secret和$response变量可以正确传递它。不好意思,这个问题要求的是旋度解,但这是我见过的最快的方法,所以我想无论如何都要加上它,因为我知道它会对那里的人有所帮助。:)
$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
}
使用"file_get_contents"与POST:
$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";
}
通常我会这样做
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();
}
}
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.google.com/recaptcha/api/siteverify",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "secret=$secret&response=$response",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded"
),
));
$responsedata = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $responsedata;
}
对于那些喜欢使用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;
}