2017-04-24 112 views
0

雖然試圖對錶單提交實施recaptcha檢查,但似乎無法獲得響應的正確成功屬性。 (服務器運行PHP原生5.3)PHP Curl Recaptcha無法獲得響應成功屬性

$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.'); 
} 

即使當我檢查,如果語句總是解析和運行回聲和拋出異常

{ "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 /> 
+0

嘗試使用$ g_result = json_decode($ result,true)時給我print_r($ g_result); –

+0

'Array([success] => 1 [challenge_ts] => 2017-04-24T19:16:41Z [hostname] => tedmoke.com)成功' 現在是唯一一個echo'ed –

+0

我的服務器似乎運行本地PHP 5.3,這可能是問題的一部分? –

回答

1

你必須json_decode結果。

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

我已經試過了,但只收到'
捕獲的致命錯誤:類stdClass的客體不能上線
被轉換爲字符串mysite.com/test/portfolio/php/process.php' –

+1

一旦我將服務器的PHP版本從5.3更新到5.6,這解決了我的問題 –