我正在嘗試將這個Recaptcha請求實現到我的註冊表單中,但它不起作用。當我在我的網站上成功驗證Recaptcha時,cURL/JSON請求返回null
。Recaptcha請求正在返回空
我試着用var_dump
對"error-codes":
從JSON請求,它只返回null;而在this document中則顯示它顯然意味着要在JSON請求中輸出兩個項目。
在此先感謝,我沒有用JSON/cURL做很多工作,所以對我來說很簡單。
這裏是我的代碼:
PHP
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
$recaptcha = $_POST['g-recaptcha-response'];
if(!empty($recaptcha)) {
function getCurlData($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
$curlData = curl_exec($curl);
curl_close($curl);
return $curlData;
}
$google_url = "https://www.google.com/recaptcha/api/siteverify";
$secret = 'You will never know >:D';
$ip = $_SERVER['REMOTE_ADDR'];
$url = $google_url . "?secret=" . $secret . "&response=" . $recaptcha . "&remoteip=" . $ip;
$res = getCurlData($url);
$res = json_decode($res, true);
// var_dumping returns null
var_dump($res);
//reCaptcha success check
if($res['success'] == true) {
echo "Recaptcha was successfully validated";
} else {
echo "Recaptcha was not validated, please try again";
}
} else {
echo "You didn't validate the Recaptcha";
}
}
?>
HTML
<form action="home.php" method="post">
<div class="g-recaptcha" data-sitekey="I removed it for this post"></div>
<input class="btn btn-primary" type="submit" name="submit" value="SIGN UP" />
</form>
它說我在使用GET請求?我什麼時候可以實現'CURLOPT_POSTFIELDS',我不熟悉這個,所以我不知道該怎麼做。 – mightyspaj3 2015-03-02 08:15:37
默認請求是''GET'',您不指定請求類型=>您正在進行GET請求。檢查**文檔**和**示例**,至少有3種方式指定POST字段。 – 2015-03-02 08:24:47
如果你看我的代碼,我有POST指定,我沒有看到我做錯了什麼。你能否詳細說明一下? – mightyspaj3 2015-03-02 09:18:36