請注意:g-recaptcha-respone
= g-recaptcha-response
谷歌reCatcha API,您可能需要指定其他參數的file_get_contents
函數調用,專門設置上下文選項SSL(如果網站具有SSL)。
// if submitted check response
if ($_POST["g-recaptcha-response"]) {
$secret = 'SECRET_KEY';
$response = $_POST['g-recaptcha-response'];
$remoteip = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify";
$post_data = http_build_query(
array(
'secret' => $secret,
'response' => $response,
'remoteip' => $remoteip
)
);
$options=array(
// If site has SSL then
'ssl'=>array(
// In my case its /etc/ssl/certs/cacert.pem
'cafile' => '/path/to/cacert.pem',
'verify_peer' => true,
'verify_peer_name' => true,
),
'http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $post_data
)
);
$context = stream_context_create($options);
$result_json = file_get_contents($url, false, $context)
$resulting = json_decode($result_json, true);
if($resulting['success']) {
//Success
}
}else
{
// action for no response
}
至少在Ubuntu的 - 如果網站有SSL
cd /usr/local/share/ca-certificates
sudo curl http://curl.haxx.se/ca/cacert.pem -o cacert.crt
sudo update-ca-certificates
sudo update-ca-certificates –fresh
和您的憑證檔案錯誤和路徑將是
capath=/etc/ssl/certs/
cafile=/etc/ssl/certs/cacert.pem
我的網站上沒有SSL,那麼放入$ options = array是什麼?或到$上下文? – Forlis
@Forlis:看到我的更新,你是否缺少's'?在'g-recaptcha-response'中 –
是的,這是工作。太多了! – Forlis