cURL Recaptcha 不工作 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31354633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cURL Recaptcha not working PHP
提问by Kenth John Israel
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.
请帮忙。谢谢。
回答by davids3
Because you're attempting to connect via SSL, you need to adjust your cURL options to handle it. A quick fix to get this to work is if you add curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
因为您正在尝试通过 SSL 进行连接,所以您需要调整您的 cURL 选项来处理它。一个快速解决这个问题的方法是,如果你添加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);
回答by thecommonthread
Here's an alternative method to cURL I found if it helps anyone. Obviously input $secretand $responsevariables to pass it correctly. 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 的替代方法,如果它对任何人有帮助的话。显然输入$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
}
回答by Andre Rodrigues
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";
}
回答by Fathur Rohim
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;
}
回答by jerryurenaa
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();
}
}