Codeigniter reCaptcha v3 with cUrl

问题描述 投票:0回答:1

我有一个与隐形reCaptcha集成的表格。

对控制器内的功能进行验证。

对Google的调用是使用file_get_content进行的,如果未获得响应,则使用curl进行调用。

这是功能

public function verify_captcha()
        {

            $recaptcha_response = $_POST['recaptchaResponse'];

            log_message('info', $recaptcha_response);           

            // Build POST request:

            $recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify'; 
            $recaptcha_secret = 'My KEY'; 
            $recaptcha_response = $_POST['recaptchaResponse']; 
            $recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response); 
            $recaptcha = json_decode($recaptcha,true);      


            if(!$recaptcha)
            {
                // call curl to POST request

                log_message('info', 'Call CURL');       
                $data = array( 'secret' => $recaptcha_secret, 'response' => $recaptcha_response); 
                //$curlConfig = array( CURLOPT_URL => $recaptcha_url, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => $data ); 
                $ch = curl_init(); 

                curl_setopt($ch, CURLOPT_URL,  $recaptcha_url);
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);             

                $response = curl_exec($ch);                 

                curl_close($ch);
                $recaptcha = json_decode($response, true);  


                ob_start();
                var_dump($recaptcha);
                $result = ob_get_contents(); //or ob_get_clean()
                log_message('info', $result);               



                if (array_key_exists('error-codes', $recaptcha))
                {

                    log_message('error',  'Error reCaptcha '.$recaptcha['error-codes'][0]);

                }               



                if ($recaptcha["success"] == '1') 
                { 
                    if ($recaptcha["score"] >= 0.5) 
                    {

                    }
                }
                else
                {
                  log_message('error',  'Error reCaptcha no Success');
                }
       else
       {
         log_message('info', 'Call file_get_content');
       }    
}

这些是日志文件的消息

ERROR - 2020-03-18 09:54:31 --> Severity: Warning  --> file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known /mysite/application/controllers/captcha.php 1362
ERROR - 2020-03-18 09:54:31 --> Severity: Warning  --> file_get_contents(https://www.google.com/recaptcha/api/siteverify?secret=6LfP26QUAAAAAHilJfguEgIcgOBkTg2soD7oCQIh&response=03AERD8XpOL7956DMd7dhiqasH4fK2iNjtBFBJdw3OynXGeAFBMmSqqtjsqXFW97rv-kD_H-y6aLrL1VLMkwg222Y7BoNnaB_zQ7y2NzXVtlIsWYwIw9BSbUdFdSylq4dNjO5j5Jo1xvjPotvMFuddnC5YVRC1wnk7HESqv8hvRU40x9pNpoQ-sIaXcAN8BdBgleXFufmmNoMzuh3PCvgT3RkIj1TsTs-ltM9LyVbLtFnFPbTkHZqpQjppMkHCcw87u3xqbr23EJkusR_U2vFwJTAJU9p-Z27sDuiKmEMsjJ2O1i3Wnxm9yq4HiEI2vnh420VDnPZEYRbXuLLSGhGuPciGQ3mtp07tjn265oyYbcFp2s9GentdUpPWRCxWfySTa6du7dzzSHkqPMKcPf6LmfVtICkTJf4y-w): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known /mysite/application/controllers/captcha.php 1362
INFO  - 2020-03-18 09:54:31 --> Call CURL
INFO  - 2020-03-18 09:54:31 --> NULL

对file_get_content的调用显示错误,并且curl不返回任何内容。

可能会发生什么?

谢谢

codeigniter recaptcha
1个回答
0
投票

如果您完全需要使用file_get_contents,我将与您共享一个帮助函数,您可以根据自己的需要进行调整

function validate_recaptcha_response($recaptcha_response)
{
    $api_url = 'https://www.google.com/recaptcha/api/siteverify';
    $api_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    $remoteip = '';

    $data = array('secret' => $api_secret, 'response' => $recaptcha_response);
    $options = array(
        'http'  => array(
            'header'    => "Content-Type: application/x-www-form-urlencoded\r\n",
            'method'    => 'POST',
            'content'   => http_build_query($data)
        )
    );

    $context = stream_context_create($options);
    $result = file_get_contents($api_url, false, $context);

    $captcha_response = json_decode($result, true);

    $r = array(
        'success'       => $captcha_response['success'],
        'timestamp'     => $captcha_response['challenge_ts'],
        'hostname'      => $captcha_response['hostname'],
        'error_codes'   => (isset($captcha_response['error-codes'])) ? $captcha_response['error-codes'] : null,
    );

    return $r;  
}

我从任何使用诸如此类的方法获得Recaptcha响应(自动加载帮助程序)的控制器中调用此方法>

$recaptcha_check = validate_recaptcha_response($var_where_you_store_the_recaptcha_response);

[请注意,我正在添加$options数组以构建POST,定义标题,方法,并在http_build_query()之前使用stream_context_create()file_get_contents()来查询数据。

© www.soinside.com 2019 - 2024. All rights reserved.