【问题标题】:Google reCAPTCHA returning incorrect json responseGoogle reCAPTCHA 返回不正确的 json 响应
【发布时间】:2016-10-13 12:00:13
【问题描述】:

我有一个包含 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);

【问题讨论】:

  • 你的问题是什么?
  • 我做错了吗?因为这不是我期望的响应格式
  • 与您的预期有何不同?您将“challenge_ts”作为时间戳返回。其他参数也一样。
  • 嗨。这不是challenge_ts。看看响应中有另一个 { } 块不应该在那里?
  • 嗨,已经解决了这个问题。我没有注意到有一些代码冲突。

标签: php recaptcha


【解决方案1】:

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

 "error-codes": [...]

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 2017-08-10
    • 2022-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-29
    相关资源
    最近更新 更多