【发布时间】: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。看看响应中有另一个 { } 块不应该在那里?
-
嗨,已经解决了这个问题。我没有注意到有一些代码冲突。