我有一个集成了不可见reCaptcha的表单。
对控制器内的功能进行验证。
对google的调用是使用file_get_content进行的,如果没有得到响应,则使用curl进行调用。
这是的功能
public function verify_captcha()
{
$recaptcha_response = $_POST['recaptchaResponse'];
log_message('info', $recaptcha_response);
// Build POST request:
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_secret = 'My KEY';
$recaptcha_response = $_POST['recaptchaResponse'];
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
$recaptcha = json_decode($recaptcha,true);
if(!$recaptcha)
{
// call curl to POST request
log_message('info', 'Call CURL');
$data = array( 'secret' => $recaptcha_secret, 'response' => $recaptcha_response);
//$curlConfig = array( CURLOPT_URL => $recaptcha_url, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => $data );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $recaptcha_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$recaptcha = json_decode($response, true);
ob_start();
var_dump($recaptcha);
$result = ob_get_contents(); //or ob_get_clean()
log_message('info', $result);
if (array_key_exists('error-codes', $recaptcha))
{
log_message('error', 'Error reCaptcha '.$recaptcha['error-codes'][0]);
}
if ($recaptcha["success"] == '1')
{
if ($recaptcha["score"] >= 0.5)
{
}
}
else
{
log_message('error', 'Error reCaptcha no Success');
}
else
{
log_message('info', 'Call file_get_content');
}
}
这些是日志文件的消息
ERROR - 2020-03-18 09:54:31 --> Severity: Warning --> file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known /mysite/application/controllers/captcha.php 1362
ERROR - 2020-03-18 09:54:31 --> Severity: Warning --> file_get_contents(https://www.google.com/recaptcha/api/siteverify?secret=6LfP26QUAAAAAHilJfguEgIcgOBkTg2soD7oCQIh&response=03AERD8XpOL7956DMd7dhiqasH4fK2iNjtBFBJdw3OynXGeAFBMmSqqtjsqXFW97rv-kD_H-y6aLrL1VLMkwg222Y7BoNnaB_zQ7y2NzXVtlIsWYwIw9BSbUdFdSylq4dNjO5j5Jo1xvjPotvMFuddnC5YVRC1wnk7HESqv8hvRU40x9pNpoQ-sIaXcAN8BdBgleXFufmmNoMzuh3PCvgT3RkIj1TsTs-ltM9LyVbLtFnFPbTkHZqpQjppMkHCcw87u3xqbr23EJkusR_U2vFwJTAJU9p-Z27sDuiKmEMsjJ2O1i3Wnxm9yq4HiEI2vnh420VDnPZEYRbXuLLSGhGuPciGQ3mtp07tjn265oyYbcFp2s9GentdUpPWRCxWfySTa6du7dzzSHkqPMKcPf6LmfVtICkTJf4y-w): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known /mysite/application/controllers/captcha.php 1362
INFO - 2020-03-18 09:54:31 --> Call CURL
INFO - 2020-03-18 09:54:31 --> NULL
对file_get_content的调用显示错误,并且不返回curl。
可能会发生什么?
感谢
如果您完全、绝对需要使用file_get_contents,我将与您分享我的助手功能,您可以根据自己的需求进行调整
function validate_recaptcha_response($recaptcha_response)
{
$api_url = 'https://www.google.com/recaptcha/api/siteverify';
$api_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$remoteip = '';
$data = array('secret' => $api_secret, 'response' => $recaptcha_response);
$options = array(
'http' => array(
'header' => "Content-Type: application/x-www-form-urlencodedrn",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($api_url, false, $context);
$captcha_response = json_decode($result, true);
$r = array(
'success' => $captcha_response['success'],
'timestamp' => $captcha_response['challenge_ts'],
'hostname' => $captcha_response['hostname'],
'error_codes' => (isset($captcha_response['error-codes'])) ? $captcha_response['error-codes'] : null,
);
return $r;
}
我从任何一个使用之类的东西得到repatcha响应的控制器调用它(助手是自动加载的(
$recaptcha_check = validate_recaptcha_response($var_where_you_store_the_recaptcha_response);
请注意,我添加了$options
数组来构建POST
,定义了标头、方法,并在file_get_contents()
之前使用http_build_query()
和stream_context_create()
来查询数据。
请按照以下步骤将Recaptcha v3集成到代码点火器版本4(3.1.9(或其他版本中。
Step #1: Create Recaptcha v3 for your domain and keep the site_key & secret_key.
[https://cloud.google.com/recaptcha-enterprise/docs/create-key][1]
Step #2: Add the bellow Javascirpt codes with replacement of your site_key & secret_key inside Head section of your Form page.
<script type="text/javascript">
var review_recaptcha_widget;
var onloadCallback = function() {
if($('#review_recaptcha').length) {
review_recaptcha_widget = grecaptcha.render('review_recaptcha', {
'sitekey' : 'recaptcha_site_key_v3',
'secretkey' : 'recaptcha_secret_key_v3'
});
}
};
</script>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>
Step #3:Browse the page, you will see the new recaptcha in right bottom corner of page, Done, thats it.