Reputation: 465
While trying to implement a recaptcha check on form submission I cannot seem to obtain the correct success property of the response. (Server running PHP native 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.');
}
Even when I check the box the if statement always resolves and runs the echo and throws the exception
{ "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 />
I also tried using the following
$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.');
}
which returns
<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 />
Upvotes: 0
Views: 854
Reputation: 503
You have to json_decode result.
$result = json_decode(curl_exec($curl));
Upvotes: 1