重新验证错误;注意:file_get_contents():假设未在application / x-www-form-urlencoded中指定内容类型

时间:2018-07-17 10:29:05

标签: php recaptcha

我正在使用Recaptcha 2并收到此奇怪的错误: 注意:file_get_contents():假设application / x-www-form-urlencode在...的第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

2 个答案:

答案 0 :(得分:1)

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

$options = array(
    'http' => array (
                'method' => 'POST',
                'content' => http_build_query($data),
                'header' => 'Content-Type: application/x-www-form-urlencoded'
            )
        );

答案 1 :(得分:1)

是的,我通过以下方式修复了该问题:

<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