【问题标题】:reCAPTCHA: file_get_contents(): Content-type not specified assuming application/x-www-form-urlencodedreCAPTCHA:file_get_contents():假设应用程序/x-www-form-urlencoded 未指定内容类型
【发布时间】:2019-02-19 10:25:10
【问题描述】:

我昨天才注意到,我的 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(): 内容类型未指定假设 应用程序/x-www-form-urlencoded

第 37 行是:

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

【问题讨论】:

    标签: recaptcha file-get-contents


    【解决方案1】:

    我已经解决了这个问题,这是我的代码:

        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
            ];
    

    【讨论】:

    • 您已将您的密钥留给大家查看,强烈建议您删除它:)
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-15
      • 2021-02-03
      • 2015-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多