无法连接到的reCAPTCHA服务器使用PHP和卷曲

问题描述:

我想验证码添加到我的网站托管在SourceForge上,但它只是当我尝试验证用户在后台响应不起作用。无法连接到的reCAPTCHA服务器使用PHP和卷曲

这里是我的代码:

<?php 
$secret = '****'; 
$recaptcha_response = $_POST["recaptcha_response"]; 
$url = 'https://www.google.com/recaptcha/api/siteverify'; 
$post_data = "secret=".$secret."&response=".$recaptcha_response; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); 
curl_setopt($ch, CURLOPT_CAINFO, '****/GeoTrust.cer'); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
$response_json = curl_exec($ch); 
if (curl_errno($ch)) { 
    var_dump(curl_errno($ch)); 
} 
curl_close($ch); 
...... 
?> 

结果是INT(7),意思是 “卷曲无法连接”。

有谁能帮我解决这个问题呢?非常感谢。

+0

尝试没有证书,并验证主机。也不知道是否有问题,但后字段可能需要数组而不是字符串。 – TheFallen

+0

不应该是'../../../ GeoTrust.cer'而不是'....../GeoTrust.cer'吗? – Justinas

+0

@Justinas我想这只是审查,他可能考虑的路径机密 – hanshenrik

既不是你的秘密,也没有你的反应是url编码,所以如果他们包含任何特殊字符,服务器将接收到错误的数据,解决这个问题。此外,如果您发送客户的IP地址,reCaptcha阻止垃圾邮件的能力可能会得到改善,所以,虽然不是必需的,但我建议您发送IP。而不是多次调用urlencode,你可以给一个数组给一个单一的http_build_query调用,这通常会导致比多个urlencode()调用更漂亮的代码。试试这个:

$post_data=http_build_query (array (
     'secret' => '???', 
     'response' => $_POST ['g-recaptcha-response'], 
     'remoteip' => $_SERVER ['REMOTE_ADDR'] 
)); 

,但这并不能解释为什么curl_exec失败。要调试,添加CURLOPT_VERBOSE = 1 ......再与冗长的数据

也更新了你的问题,不要忽视curl_setopt的返回值,如果有一个问题设置你的选择,curl_setopt返回布尔(假的),你就忽略..使用类似

function ecurl_setopt (/*resource*/$ch , int $option , /*mixed*/ $value){ 
    $ret=curl_setopt($ch,$option,$value); 
    if($ret!==true){ 
     //option should be obvious by stack trace 
     throw new RuntimeException ('curl_setopt() failed. curl_errno: ' . curl_errno ($ch) .'. curl_error: '.curl_error($ch)); 
    } 
} 

例如,如果curl_setopt($ch, CURLOPT_CAINFO, '....../GeoTrust.cer'); 失败,则返回布尔(假),并且您的curl_exec失败,因为它无法验证si gnature

+0

谢谢,我已将CURLOPT_VERBOSE设置为true,但没有从中获得任何信息。有什么问题? – NN708

+0

似乎所有的curl_setopt()都返回true。 – NN708

+0

然后你没有看到STDERR,只有STDOUT,因为CURLOPT_VERBOSE默认打印到STDERR。它可能是打印到您的PHP错误日志,但。反正卷曲的VERBOSE信息重定向到标准输出/浏览器,做'ecurl_setopt($ CH,CURLOPT_STDERR,STDOUT);' - 然后再试一次。现在有来自CURLOPT_VERBOSE的任何信息? – hanshenrik