我是Web开发(或开发)的新手,我正在使用Laravel(如果它以某种方式重要的话)。我的表格中有一个重新验证码。它可以工作,但现在我必须在验证码中验证用户的关键字。
文档:https://developers.google.com/recaptcha/docs/verify 听起来很简单,但不知何故,我无法编写代码行进行验证。我正在寻找一些示例,但它们不是针对 Laravel,而是用于纯 PHP,我没有看到示例中使用的"https://www.google.com/recaptcha/api/siteverify?secret=your_secret&response=response_string&remoteip=user_ip_address"URL 在哪里。
我的控制器是:
public function postMessage() {
require_once('recaptchalib.php');
$privatekey = "MY_PRIVATE_KEY";
?????
// this should be executed only after validation
DB::table('messages')->insert(['name' => Input::get('name'),
'email' => Input::get('email'),
'message' => Input::get('message'),
'datetime' => 'Carbon'Carbon::now()->toDateTimeString()]);
所以请指导我,我如何获得带有响应的 JSON 对象,或者我应该写什么而不是?????
好吧,最后我能够自己找到解决方案:
public function postMessage() {
$secret = '7LdL3_4SBAAAAKe5iE-RWUya-nD6ZT7_NnsjjOzc';
$response = Input::get('g-recaptcha-response');
$url = 'https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$response;
$jsonObj = file_get_contents($url);
$json = json_decode($jsonObj, true);
if ($json['success']==true) {
DB::table('messages')->insert(['name' => Input::get('name'),
'email' => Input::get('email'),
'message' => Input::get('message'),
'datetime' => 'Carbon'Carbon::now()->toDateTimeString()]);
return Redirect::route('contact')->with('okmessage', 'Ďakujeme. Vaša správa bola odoslaná');
} else {
return Redirect::route('contact')->with('errormessage', 'Chyba! Zaškrtnite "Nie som robot" a zadajte správny kód.');
}
}