PHP Curl Recaptcha无法获取响应成功属性

时间:2017-04-24 18:36:40

标签: php curl recaptcha

在尝试对表单提交执行重新检查时,我似乎无法获得响应的正确成功属性。 (运行PHP本机5.3的服务器)

$data = array(
       'secret' => $secret,
       'response' => $_POST['g_recaptcha'],
       'remoteip' => $_SERVER['REMOTE_ADDR']
    );

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);

if ($result->success===false) {
    $errorMSG .= "Please complete reCAPTCHA";
    echo $result;
    throw new exception('Gah! We seemed to have encountered an error. Please try again.');
} 

即使我在复选框中,if语句也会解析并运行echo并抛出异常

{ "success": true, "challenge_ts": "2017-04-24T18:28:48Z", "hostname": "mysite.com" }<br /> <b>Fatal error</b>: Uncaught exception 'Exception' with message 'Gah! We seemed to have encountered an error. Please try again.' in ...process.php:23 Stack trace: #0 {main} thrown in <b>...process.php</b> on line <b>23</b><br /> 

我也尝试使用以下

$g_result = json_decode($result, true);
if ($g_result->success == false) {
    $errorMSG .= "Please complete reCAPTCHA";
    echo $result;
    throw new exception('Gah! We seemed to have encountered an error. Please try again.');
} 

返回

<br /> <b>Catchable fatal error</b>: Object of class stdClass could not be converted to string in <b>mysite.com/test/portfolio/php/process.php</b> on line <b>21</b><br /> 

1 个答案:

答案 0 :(得分:1)

你必须得到json_decode结果。

$result = json_decode(curl_exec($curl));