以PHP现有形式集成Google重新验证码

时间:2017-06-26 22:30:27

标签: php recaptcha

好吧,我一直在撞墙试图自己解决这个问题,但没有成功。我正在尝试重新验证以验证同一页面上的验证码。我宁愿在进入下一页之前检查验证码是否正确。我把google js文件的调用放在头部:

<script src='https://www.google.com/recaptcha/api.js'></script>

然后按照说明将第二部分放在表格的末尾,如下所示:

<div class="g-recaptcha" data-sitekey="***************************************"></div>

到目前为止一切顺利。现在,踢我***的部分是服务器端集成。他们提供的代码是:

When your users submit the form where you integrated reCAPTCHA, you'll get
as part of the payload a string with the name "g-recaptcha-response". In 
order to check whether Google has verified that user, send a POST request
with these parameters:

URL: https://www.google.com/recaptcha/api/siteverify
secret (required)   **********************************************
response (required) The value of 'g-recaptcha-response'.
remoteip    The end user's ip address.

我真的很难过这个部分应该如何工作,比如它实际上去了哪里?我有checkout.php提交到billing-checkout.php。第二部分是否会进入billing-checkout.php?如果是这样,我该如何让它完全正常工作?我讨厌感觉像一个菜鸟,但我对这件事感到非常沮丧。任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:1)

您必须向提供的网址发出cURL请求,然后再回复。

<?php
$cp = curl_init("https://www.google.com/recaptcha/api/siteverify");
$fields = array(
    'secret' => YOUR_RECAPTCHA_PRIVATE_KEY,
    'response' => $_POST['g-recaptcha-response'],
    'remoteip' => $_SERVER['REMOTE_ADDR']
);
curl_setopt($cp, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cp, CURLOPT_POST, 1);
curl_setopt($cp, CURLOPT_POSTFIELDS, $fields);
curl_setopt($cp, CURLOPT_TIMEOUT, 15);

$data = curl_exec($cp); // The response

curl_close($cp);

?>