Andrew Wang
Andrew Wang

Reputation: 41

Json returned from recaptcha is ... bad

When doing
$response = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);
$result = json_decode($response);

The response is
)]}'
["uvresp","03AJpayVHarkP_b40i5...stuff...VOrIy6m3ws",1,120]

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

The )]}' breaks things. I have no idea where that is coming from. $g_recaptcha_response looks fine to me (what I am sending to google to verify).

The function to verify recaptcha

function verify_recaptcha ($g_recaptcha_response, $remote_address) {
    # Verify captcha
    $post_data = http_build_query(
        array(
            'secret' => 'secret',
            'response' => $g_recaptcha_response,
            'remoteip' => $remote_address
        )
   );

    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $post_data
        )
    );

    $context  = stream_context_create($opts);
    $response = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);
    $result   = json_decode($response);

    return $result->success;
}

Thanks.

Upvotes: 3

Views: 916

Answers (1)

Andrew Wang
Andrew Wang

Reputation: 41

Ok my bad. The above code works, I had a problem further downstream (query was trying to use a column that did not exist).

Upvotes: 1

Related Questions