Google reCAPTCHA返回错误的json响应

时间:2016-06-13 06:54:28

标签: php recaptcha

我的表格中有Google的reCAPTCHA。但是我遇到了它返回的json格式的问题:在文档中,

https://developers.google.com/recaptcha/docs/verify#api-request

{
 "success": true|false,
 "challenge_ts": timestamp,  // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
 "hostname": string,         // the hostname of the site where the reCAPTCHA was solved
 "error-codes": [...]        // optional
}

但我收到了这个回复:

{
 "success": true,
 "challenge_ts": "2016-06-13T06:46:40Z",
 "hostname": "devs.mysite.com.au"
} {"success":false,"errors":[null]}'

导致一些错误。我的代码是:

$is_valid = $_POST['g-recaptcha-response'];
$url = 'https://www.google.com/recaptcha/api/siteverify';
$fields = array(
    'secret' => 'mysecretcode',
    'response' => $is_valid,
    'remoteip', ''
);

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);

1 个答案:

答案 0 :(得分:0)

Google API的json输出符合预期。我对json不太熟悉,但我希望[...]参数的error-codes指定它包含任何东西:

 "error-codes": [...]

它返回的是一个json对象(这是花括号的意思,见the json specification)。实际上,它返回一个具有两个属性的对象:successerrors

{"success":false,"errors":[null]}'