reCAPTCHA:file_get_contents():假设应用程序/ x-www-form-urlencoded未指定内容类型

时间:2019-02-19 10:25:10

标签: recaptcha file-get-contents

我昨天才注意到,我的reCAPTCHA出了问题,去年还不错。

这是我的代码:

public function message(Request $request) {
    $response = $_POST["g-recaptcha-response"];
    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $data = array(
        'secret' => '6LcwXi8UAAAAAE9zNCVfqwDOIWNazNgdK-0wQv9L',
        'response' => $_POST["g-recaptcha-response"]
        );

    //For debug purpose (remove comments)
        //dd($request->all());

    $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) {
            return redirect('/')->with('success', 'You are a bot! Go away!');;
        } else if ($captcha_success->success==true) {

            $content = 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' => http_build_query($data)
                )
            );

当我提交联系表时,它给了我这个错误:

  

PagesController.php第37行中的ErrorException:

     

file_get_contents():假设未指定内容类型   application / x-www-form-urlencoded

第37行是:

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

2 个答案:

答案 0 :(得分:0)

我已解决此问题,这是我的代码:

    private function check_recaptcha($key){
    $secret = '6LdMgJIUAAAAAOvJPW8MHjumG2xQNNuRyw-WctqQ';
    $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$key);
    $responseData = json_decode($verifyResponse);
    return ($responseData->success)? true:false;
}

public function message(Request $request) {
    //Validate
    $validator = Validator::make($request->all(), [
        'name' => 'required',
        'subject' => 'required',
        'message' => 'required',
        'email' => 'required|email',
        'g-recaptcha-response' => 'required',
    ]);

    //If validator failed
    if ($validator->fails()) {
        return redirect('/')
            ->withErrors($validator)
            ->withInput();
    }

    //Declare variable
    $name = $request->input("name");
    $email = $request->input("email");
    $subject = $request->input("subject");
    $message = $request->input("message");
    $captchaKey = $request->input("g-recaptcha-response");

    //Test reCAPTCHA
    if (!$this->check_recaptcha($captchaKey)) {//captcha gagal
        return redirect('/')->with('success', 'You are a bot! Go away!');
    } else{//captcha sukses
        $content = [
            'name' => $name,
            'email' => $email,
            'subject' => $subject,
            'message' => $message
        ];

答案 1 :(得分:0)

在我的情况下,这个标题丢失了,试试这个

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