重述错误;注意:file_get_contents():未指定内容类型,假设在中编码了application/x-www



我正在使用repatcha 2并得到这个奇怪的错误:注意:file_get_contents((:未指定内容类型,假设application/x-www-form-urlencoded in。。。在线38上。

代码是:

<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

触发错误消息的第38行是:

$verify = file_get_contents($url, false, $context);

无论是否勾选了机器人框,都会显示错误消息。如果没有勾选方框,则会显示"你没有证明你是人"的消息,如果勾选了机器人方框,则代码会得到正确处理,尽管错误消息仍然会出现。

如何删除错误消息?该网站有SSL证书,所以我尝试更改:

$options = array(
'http' => array (
'method' => 'POST',
'content' => http_build_query($data)
)
);

至:

$options = array(
'https' => array (
'method' => 'POST',
'content' => http_build_query($data)
)
);

这会删除错误消息,但随后会出现"你没有证明你是人"的消息,即使选中了机器人框。

我被难住了。

问候

Tog

是的,我用修复了它

<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-urlencodedrn", 
"Content-Length: ".strlen($query)."rn".
"User-Agent:MyAgent/1.0rn",
'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

我认为您的选项中缺少一个参数,请尝试以下操作:

$options = array(
'http' => array (
'method' => 'POST',
'content' => http_build_query($data),
'header' => 'Content-Type: application/x-www-form-urlencoded'
)
);
$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>';
}

最新更新