2015-07-11 55 views
10

我有什麼捲曲的Recaptcha不工作PHP

$data = array(
      'secret' => "my-app-secret", 
      'response' => "the-response" 
     ); 

$verify = curl_init(); 
curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify"); 
curl_setopt($verify, CURLOPT_POST, true); 
curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data)); 
curl_setopt($verify, CURLOPT_RETURNTRANSFER, true); 
$response = curl_exec($verify); 

var_dump($response); 

我該怎麼bool(false)(這意味着curl_exec()失敗)

我希望什麼:一個JSON對象響應

請幫忙。謝謝。

回答

21

因爲您嘗試通過SSL進行連接,您需要調整cURL選項來處理它。如果你添加curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);

CURLOPT_SSL_VERIFYPEER設置爲false會使它接受給予它的任何證書而不是驗證它們。

<?php 

$data = array(
      'secret' => "my-secret", 
      'response' => "my-response" 
     ); 

$verify = curl_init(); 
curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify"); 
curl_setopt($verify, CURLOPT_POST, true); 
curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data)); 
curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($verify, CURLOPT_RETURNTRANSFER, true); 
$response = curl_exec($verify); 

var_dump($response); 
+2

請刪除SSL_VERIFYPEER =假啄。 Google總是使用有效的證書。 –

+0

它用於爲我工作超過2年,沒有你提出的額外線路('CURLOPT_SSL_VERIFYPEER'),但今天我面臨着與報道相同的問題,並通過添加此線路問題消失了。所以謝謝你。但我真的很想知道現在的問題在哪裏?這是我的服務器還是Google?什麼證書現在沒有通過哪一方驗證? – Peter

3

這是另一種cURL方法,我發現它可以幫助任何人。明顯輸入$祕密$迴應變量正確傳遞它。對不起,問題是要求cURL解決方案,但這是我見過的最快捷的方法,所以我認爲它會添加它,因爲我知道它會幫助有人在那裏。 :)

$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$response); 
$response = json_decode($response, true); 
if($response["success"] === true){ 
    // actions if successful 
}else{ 
    // actions if failed 
} 
+1

如果在PHP配置中allow_url_fopen = 0,cURL可能是唯一選項。 – Palantir

1

它更容易使用 「的file_get_contents」 與POST:

$postdata = http_build_query(
      array(
       'secret' => [YOUR_SECRET_KEY], 
       'response' => $_POST["g-recaptcha-response"] 
      ) 
     ); 

     $opts = array('http' => 
      array(
       'method' => 'POST', 
       'header' => 'Content-type: application/x-www-form-urlencoded', 
       'content' => $postdata 
      ) 
     ); 

     $context = stream_context_create($opts); 

     $result = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context); 

     $check = json_decode($result); 

     if($check->success) { 
      echo "validate"; 
     } else { 
      echo "wrong recaptcha"; 
     }