【发布时间】:2018-12-21 10:53:10
【问题描述】:
我正在尝试在我的网站中实现 reCAPTCHA,除了 file_get_contents() 的返回之外,一切似乎都运行良好。
这是我的代码:
if ($_REQUEST["send"] == 1){
// access
$secretKey = 'my_key';
$captcha = $_POST['g-recaptcha-response'];
$ip = $_SERVER['REMOTE_ADDR'];
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
echo ($responseKeys);exit;
if(intval($responseKeys["success"]) !== 1) {
$message = 'Invalid reCAPTCHA';
} else {
$msg = 'content';
send_mail('send_to',"Subject",$msg);
header("location:index.php?send=1");exit;
}
}
我的变量响应返回空。
我试图打开https://www.google.com/recaptcha/api/siteverify?手动插入变量,它似乎工作正常。
我是不是忘记了什么?
谢谢
【问题讨论】:
-
你应该对你的参数进行 url 编码。
fopen wrappers是否已启用(参见 php 手册:php.net/manual/en/function.file-get-contents.php)? -
我认为要将 file_get_contents 与外部 url 一起使用,您必须在服务器中将 allow_url_fopen 配置为 true。不知道是不是你的问题。
-
检查您的 PHP 错误日志,看看您是否收到来自
file_get_contents()的错误。 -
仅供参考,应该通过 POST 请求完成。见developers.google.com/recaptcha/docs/verify