简体   繁体   English

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

[英]Can't Connect to reCAPTCHA's Server Using PHP and cURL

I want to add reCAPTCHA to my website hosted on SourceForge, but it just doesn't work when I try to verify the user's response in the backend. 我想将reCAPTCHA添加到我在SourceForge上托管的网站上,但是当我尝试在后端验证用户的响应时,它不起作用。

Here's my code: 这是我的代码:

<?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);
......
?>

The result is int(7), means "cURL couldn't connect". 结果为int(7),表示“ cURL无法连接”。

Could anybody help me with this problem? 有人可以帮我解决这个问题吗? Thanks a lot. 非常感谢。

neither your secret nor your response is urlencoded, so if they contain any special characters, the server will receive the wrong data, fix that. 既不会对您的秘密也不会对您的回复进行代码编码,因此,如果它们包含任何特殊字符,服务器将接收到错误的数据,请解决此问题。 also, reCaptcha's ability to block spammers will probably be improved if you send the client's ip address too, so, while not required, i suggest you send the ip. 另外,如果您也发送客户端的IP地址,则reCaptcha阻止垃圾邮件发送者的功能可能会得到改善,因此,尽管不是必需的,我还是建议您发送IP。 and instead of multiple calls to urlencode, you can give an array to a single http_build_query call, which often results in prettier code than multiple urlencode() calls. 您可以为单个http_build_query调用提供一个数组,而不是多次调用urlencode,这通常会导致代码比多次urlencode()调用更漂亮。 try this: 尝试这个:

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

but this doesn't explain why curl_exec fail.. to debug that, add CURLOPT_VERBOSE=1 ... then update your question with the VERBOSE data 但这不能解释为什么curl_exec失败..进行调试,添加CURLOPT_VERBOSE = 1 ...然后使用VERBOSE数据更新您的问题

also, don't ignore the return value of curl_setopt, if there was a problem setting your options, curl_setopt returns bool(false), which you just ignore.. use something like 另外,不要忽略curl_setopt的返回值,如果设置选项时出现问题,curl_setopt返回bool(false),您可以忽略它。

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) );
    }
}

for example, if curl_setopt($ch, CURLOPT_CAINFO, '....../GeoTrust.cer'); 例如,如果curl_setopt($ch, CURLOPT_CAINFO, '....../GeoTrust.cer'); fails, it returns bool(false) , and your curl_exec fail because it can't validate the signature 失败,它返回bool(false),并且curl_exec失败,因为它无法验证签名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM