mig*_*aj3 8 php json curl recaptcha
我正在尝试将此Recaptcha请求实施到我的注册表单中,但它无效.null当我在我的网站上成功验证Recaptcha时,cURL/JSON请求会返回.
我尝试使用var_dump在"error-codes":从JSON请求,它只返回null; 而在本文档中,它表明它显然意味着在JSON请求中输出两个项目.
在此先感谢,我没有做过很多JSON/cURL的工作,所以对我来说很容易.
这是我的代码:
PHP
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
$recaptcha = $_POST['g-recaptcha-response'];
if(!empty($recaptcha)) {
function getCurlData($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
$curlData = curl_exec($curl);
curl_close($curl);
return $curlData;
}
$google_url = "https://www.google.com/recaptcha/api/siteverify";
$secret = 'You will never know >:D';
$ip = $_SERVER['REMOTE_ADDR'];
$url = $google_url . "?secret=" . $secret . "&response=" . $recaptcha . "&remoteip=" . $ip;
$res = getCurlData($url);
$res = json_decode($res, true);
// var_dumping returns null
var_dump($res);
//reCaptcha success check
if($res['success'] == true) {
echo "Recaptcha was successfully validated";
} else {
echo "Recaptcha was not validated, please try again";
}
} else {
echo "You didn't validate the Recaptcha";
}
}
?>
Run Code Online (Sandbox Code Playgroud)
HTML
<form action="home.php" method="post">
<div class="g-recaptcha" data-sitekey="I removed it for this post"></div>
<input class="btn btn-primary" type="submit" name="submit" value="SIGN UP" />
</form>
Run Code Online (Sandbox Code Playgroud)
ism*_*tro 12
这是我的代码运行没有问题:
客户端:
<div class="g-recaptcha" data-sitekey="PUBLIC_KEY"></div>
Run Code Online (Sandbox Code Playgroud)
服务器端:
if (isset($_POST['g-recaptcha-response'])) {
$captcha = $_POST['g-recaptcha-response'];
$privatekey = "SECRET_KEY";
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
'secret' => $privatekey,
'response' => $captcha,
'remoteip' => $_SERVER['REMOTE_ADDR']
);
$curlConfig = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $data
);
$ch = curl_init();
curl_setopt_array($ch, $curlConfig);
$response = curl_exec($ch);
curl_close($ch);
}
$jsonResponse = json_decode($response);
if ($jsonResponse->success === true) {
doSomething();
}
else {
doSomeOtherThing();
}
Run Code Online (Sandbox Code Playgroud)
在这里工作!:)
Api 说:方法:POST,您正在发送“GET”请求。检查CURLOPT_POST并CURLOPT_POSTFIELDS选择http://php.net/manual/en/function.curl-setopt.php
编辑:我刚刚检查了 API,它也可以与 GET 一起使用。我在您的代码中没有看到任何错误。您可以暂时将您的功能更改为:
function getCurlData($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
$curlData = curl_exec($curl);
if (!$curlData) {
throw new \Exception('Curl error: ' . curl_error($curl));
}
curl_close($curl);
return $curlData;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6269 次 |
| 最近记录: |