我正在尝试在我的网站中实现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;
}
}
我的变量响应返回空。
我试图打开Configuring Eclipse for Linux Kernel module development?手动插入变量,它似乎可以正常工作。
我忘记了什么吗?
谢谢
答案 0 :(得分:0)
他们的API等待POST请求。您的代码发送GET请求。
答案 1 :(得分:0)
我的包装器被禁用,这是为什么我无法访问URL并获得退货的原因。
由于我无权访问php.ini,因此解决方法是使用curl发送请求,以下是代码:
$url = "https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo curl_error($ch);
echo "\n<br />";
$response = '';
} else {
curl_close($ch);
}
if (!is_string($response) || !strlen($response)) {
echo "Failed to get contents.";
$contents = '';
}
$responseKeys = json_decode($response,true);