2017-06-15 3 views
0

Ich versuche, meine $_POST['g-recaptcha-response'] auf https://www.google.com/recaptcha/api/siteverify, um zu überprüfen, aber ich erhalte das folgende Ergebnis:Invisible recaptcha SiteVerify - Fehlercodes

"success": false, 
    "error-codes": [ 
    "missing-input-response", 
    "missing-input-secret" 
    ] 

Mein Code:

if($has_errors == false) { 
    $result = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, stream_context_create(array(
     'http' => array(
      'header' => "Content-type: application/x-www-form-urlencoded\r\n", 
      'method' => 'POST', 
      'content' => http_build_query(array(
      'response' => $_POST['g-recaptcha-response'], 
      'secret' => variable_get('google_recaptcha_secret', '') 
     )), 
    ), 
    ))); 

    var_dump($result); 

    $result = json_decode($result); 

    if($result->success == false) { 
     form_set_error('name', t('Submission blocked by Google Invisible Captcha.')); 
    } 
    } 

ich meine Variable geprüft google_recaptcha_secret, das ist richtig.

Antwort

0

ich nie file_get_contents Daten schreiben wie die verwendeten gesehen habe, ich sage nicht, es ist nicht möglich, aber ich würde empfehlen, mit cURL versuchen:

if ($has_errors == false) { 
    $data = [ 
     'response' => $_POST['g-recaptcha-response'], 
     'secret' => variable_get('google_recaptcha_secret', ''), 
    ]; 

    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify'); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); 
    curl_setopt($curl, CURLOPT_TIMEOUT, 10); 
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 
    $result = curl_exec($curl); 
    $error = !$result ? curl_error($curl) : null; 
    curl_close($curl); 

    var_dump($result); 

    $result = json_decode($result); 

    if ($error || $result->success == false) { 
     form_set_error('name', t('Submission blocked by Google Invisible Captcha.')); 
    } 
} 
+0

ich den file_get_contents Code in einem Online-Beispiel gefunden, aber mit curl anstatt das Problem behoben - danke! – Paul