[英]recaptcha error; Notice: file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in
I am using recaptcha 2 and getting this weird error: Notice: file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in... on line 38.我正在使用 recaptcha 2 并收到这个奇怪的错误:注意:file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded in... 在第 38 行。
the code is:代码是:
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<?php
$page="contact";
include("includes/navbar.php");
echo '<div id="wrapper">';
$response = $_POST["g-recaptcha-response"];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
'secret' => '6LfJnWQUAAAAANMRQApsnetVjggDqnn4hx7Ltbyz',
'response' => $_POST["g-recaptcha-response"]
);
$options = array(
'http' => array (
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captcha_success=json_decode($verify);
if ($captcha_success->success==false) {
echo "<h1>You did not prove you are human.</h1><h2> Please go back and complete the form!</h2>";
exit();
}
else if ($captcha_success->success==true) {
more code here to execute if captcha successfull
line 38 that triggers the error message is:触发错误消息的第 38 行是:
$verify = file_get_contents($url, false, $context);
The error message appears whether or not the robot box has been ticked.无论是否勾选了机器人框,都会出现错误消息。 If the box has not been ticked, the "You did not prove you are human" message appears and if the robot box has been ticked the code is correctly processed, although the error message still appears.如果未勾选该框,则会出现“你没有证明你是人类”消息,如果已勾选机器人框,则代码将得到正确处理,但仍会出现错误消息。
How can I remove the error message?如何删除错误消息? The site has an SSL certificate so I tried changing:该站点有一个 SSL 证书,所以我尝试更改:
$options = array(
'http' => array (
'method' => 'POST',
'content' => http_build_query($data)
)
);
to:到:
$options = array(
'https' => array (
'method' => 'POST',
'content' => http_build_query($data)
)
);
and that removes the error message, but then the "You did not prove you are human" message appears, even if the robot box is checked.并且删除了错误消息,但是即使选中了机器人框,也会出现“你没有证明你是人”的消息。
I am stumped.我很难过。
Regards问候
Tog托格
I think there is a parameter missing inside your options, try something like this: 我认为您的选项中缺少一个参数,请尝试如下操作:
$options = array(
'http' => array (
'method' => 'POST',
'content' => http_build_query($data),
'header' => 'Content-Type: application/x-www-form-urlencoded'
)
);
Yes, I fixed it with: 是的,我通过以下方式修复了该问题:
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<?php
$page="contact";
include("includes/navbar.php");
echo '<div id="wrapper">';
$response = $_POST["g-recaptcha-response"];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array(
'secret' => '6LfJnWQUAAAAANMRQApsnetVjggDqnn4hx7Ltbyz',
'response' => $_POST["g-recaptcha-response"]
);
$query = http_build_query($data);
$options = array(
'http' => array (
'header' => "Content-Type: application/x-www-form-urlencoded\r\n",
"Content-Length: ".strlen($query)."\r\n".
"User-Agent:MyAgent/1.0\r\n",
'method' => 'POST',
'content' => $query
)
);
$context = stream_context_create($options);
$verify = file_get_contents($url, false, $context);
$captcha_success=json_decode($verify);
if ($captcha_success->success==false) {
echo "<h1>You did not prove you are human.</h1><h2> Please go back and complete the form!</h2>";
exit();
}
else if ($captcha_success->success==true) {
more code here to execute if captcha successfull
$secretKey = '------------Your S-Key---------------';
$token = $_POST["g-token"];
$ip = $_SERVER['REMOTE_ADDR'];
/* ======================= POST METHOD =====================*/
$url = "https://www.google.com/recaptcha/api/siteverify?";
$data = array('secret' => $secretKey, 'response' => $token, 'remoteip'=> $ip);
// use key 'http' even if you send the request to https://...
$options = array('http' => array(
'method' => 'POST',
'content' => http_build_query($data),
'header' => 'Content-Type: application/x-www-form-urlencoded'
));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result);
if($response->success)
{
echo '<center><h1>Validation Success!</h1></center>';
}
else
{
echo '<center><h1>Captcha Validation Failed..!</h1></center>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.