[英]cURL Recaptcha not working PHP
我有什么:
$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);
工作的一個快速解決方法是添加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變量以正確傳遞它。 抱歉,問題是要求 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
}
使用帶有 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";
}
通常我會做這樣的事情
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();
}
}
對於那些喜歡使用 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.