Recaptcha Request is returning Null

左心房为你撑大大i 提交于 2019-12-04 03:41:01

Here is my code running without problem:

Client Side:

<div class="g-recaptcha" data-sitekey="PUBLIC_KEY"></div>

Server Side:

if (isset($_POST['g-recaptcha-response'])) {
    $captcha = $_POST['g-recaptcha-response'];
    $privatekey = "SECRET_KEY";
    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $data = array(
        'secret' => $privatekey,
        'response' => $captcha,
        'remoteip' => $_SERVER['REMOTE_ADDR']
    );

    $curlConfig = array(
        CURLOPT_URL => $url,
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => $data
    );

    $ch = curl_init();
    curl_setopt_array($ch, $curlConfig);
    $response = curl_exec($ch);
    curl_close($ch);
}

$jsonResponse = json_decode($response);

if ($jsonResponse->success === true) {
    doSomething();
}
else {
    doSomeOtherThing();
}

Working here! :)

Api says: METHOD: POST, you are sending "GET" request. Check CURLOPT_POST and CURLOPT_POSTFIELDS option on http://php.net/manual/en/function.curl-setopt.php

EDIT: I just checked the API, it work with GET too. I don't see any error in your code. Could you temporary change your function to:

   function getCurlData($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
        $curlData = curl_exec($curl);

        if (!$curlData) {
            throw new \Exception('Curl error: ' . curl_error($curl));
        }
        curl_close($curl);


        return $curlData;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!