Recaptcha请求返回Null

时间:2015-03-02 07:18:23

标签: php json curl recaptcha

我试图将此Recaptcha请求实施到我的注册表单中,但它不起作用。当我在我的网站上成功验证Recaptcha时,cURL / JSON请求返回null

我尝试在JSON请求的var_dump上使用"error-codes":,它只返回null;而在this document中,它表明它显然意味着在JSON请求中输出两个项目。

先谢谢,我还没有完成JSON / cURL的工作,所以对我来说很容易。

这是我的代码:

PHP

<?php

    if($_SERVER["REQUEST_METHOD"] == "POST") {
        $recaptcha = $_POST['g-recaptcha-response'];
        if(!empty($recaptcha)) {

            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);
                curl_close($curl);
                return $curlData;
            }

            $google_url = "https://www.google.com/recaptcha/api/siteverify";
            $secret = 'You will never know >:D';
            $ip = $_SERVER['REMOTE_ADDR'];
            $url = $google_url . "?secret=" . $secret . "&response=" . $recaptcha . "&remoteip=" . $ip;
            $res = getCurlData($url);
            $res = json_decode($res, true);

            // var_dumping returns null
            var_dump($res);

            //reCaptcha success check 
            if($res['success'] == true) {
                echo "Recaptcha was successfully validated";
            } else {
                echo "Recaptcha was not validated, please try again";
            }
        } else {
            echo "You didn't validate the Recaptcha";
        }
    }
?>

HTML

<form action="home.php" method="post">
    <div class="g-recaptcha" data-sitekey="I removed it for this post"></div>
    <input class="btn btn-primary" type="submit" name="submit" value="SIGN UP" />
</form>

3 个答案:

答案 0 :(得分:12)

这是我的代码运行没有问题:

客户端:

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

服务器端:

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();
}

工作here! :)

答案 1 :(得分:0)

Api说:方法:POST,你发送“GET”请求。 查看http://php.net/manual/en/function.curl-setopt.php

上的CURLOPT_POSTCURLOPT_POSTFIELDS选项

编辑: 我刚检查了API,它也适用于GET。我没有在您的代码中看到任何错误。你可以临时改变你的职能:

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

答案 2 :(得分:0)

您的问题可能与SSL证书有关。 您可以将这一行放到curl忽略它:

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false)

但是,您可以在本地主机上对其进行测试,但是在上载网站时,最好具有SSL证书并删除此行。