Tho*_*yth 2 php post curl recaptcha
我正在通过一个小清单来升级我已经完成的网络系统部分的内容,其中之一是确保我的 Google reCaptcha 的安全性是正确的。
目前,我使用此代码:
//reCaptcha
$Url = "https://www.google.com/recaptcha/api/siteverify";
$SecretKey = "----Secret Key----";
$Response = file_get_contents($Url."?secret=".$SecretKey."&response=".$_POST['Response']);
$Robot = json_decode($Response);
Run Code Online (Sandbox Code Playgroud)
这很好用,但是谷歌的文档说你应该使用 POST 方法而不是 get,显然是为了确保有人没有得到我的密钥。但是,我不确定如何执行此操作,因此将不胜感激。我知道我可能不得不使用 cURL,但是,我对此一无所知,我不确定如何安装它(如果需要)。
谢谢,汤姆。
... 将变量发布到 Google 的 reCaptcha 服务器,而不是通过 GET 发送。
Run Code Online (Sandbox Code Playgroud)$Response = file_get_contents($Url."?secret=".$SecretKey."&response=".$_POST['Response']);
如果你想通过 HTTP POST 将数据发送到谷歌服务器,那么你必须使用客户端 URL 库,而不是将数据嵌入 URL(如上述 URL 中的密钥和响应)并通过 GET 发送。
这是参考:
你的服务器端 PHP 代码应该是这样的:
$Url = "https://www.google.com/recaptcha/api/siteverify";
$SecretKey = "----Secret Key----";
if(isset($_POST['Response']) && !empty($_POST['Response'])){
//get verified response data
$data = array('secret' => $secret, 'response' => $_POST['Response']);
$ch = curl_init($Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$verifyResponse = curl_exec($ch);
curl_close($ch);
$responseData = json_decode($verifyResponse);
// your code
}else{
echo "Please click on the reCAPTCHA box.";
}
Run Code Online (Sandbox Code Playgroud)
这里有几点需要注意,
CURLOPT_RETURNTRANSFER为true将传输作为返回值的字符串返回,curl_exec()而不是直接输出。CURLOPT_SSL_VERIFYPEER可用于验证对等方的证书。如果我们将其指定为false,它将接受任何服务器(对等)证书。CURLOPT_POST用于执行常规 HTTP POST。这种 POST 是普通application/x-www-form-urlencoded类型,最常用于 HTML 表单。CURLOPT_POSTFIELDS用于指定我们要通过此 POST 请求提交的完整数据。该$data数组应使用http_build_query()函数转换为 URL 编码的查询字符串,以便它可以作为application/x-www-form-urlencoded.| 归档时间: |
|
| 查看次数: |
8793 次 |
| 最近记录: |