Php 在本地主机上测试recaptcha
我一直在尝试在Php 在本地主机上测试recaptcha,php,recaptcha,Php,Recaptcha,我一直在尝试在localhost(xampp使用PHP5.3)上测试recaptcha;下面的代码(取自)在我的远程主机上工作并返回'success':'true',但在本地主机上返回{“success”:false,“错误代码”:[“missing input response”]}。但是,如果我将POST请求更改为GET请求(基本上切换2$result=行的注释),那么我将得到一个成功的调用 为什么会这样?!虽然我很高兴它能在我的实际站点上运行,但我想了解为什么GET可以运行,而POST不能
localhost
(xampp
使用PHP5.3)上测试recaptcha;下面的代码(取自)在我的远程主机上工作并返回'success':'true'
,但在本地主机上返回{“success”:false,“错误代码”:[“missing input response”]}
。但是,如果我将POST请求更改为GET请求(基本上切换2$result=
行的注释),那么我将得到一个成功的调用
为什么会这样?!虽然我很高兴它能在我的实际站点上运行,但我想了解为什么GET可以运行,而POST不能在localhost上运行
<html><head></head><body><script src='https://www.google.com/recaptcha/api.js' async defer>
if(isset($_POST['g-recaptcha-response'])) {
$url = 'https://www.google.com/recaptcha/api/siteverify';
$data = array('secret' => 'my_secret_key',
'response' => $_POST['g-recaptcha-response']);
$options = array(
'http' => array(
'method' => "POST",
'header' => "Content-type: application/x-www-form-urlencoded" . PHP_EOL,
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
//$result = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=my_secretkey&response=' . $_POST['g-recaptcha-response']);
$result = file_get_contents($url, false, $context);
echo $result;
}
?>
<form method="post" action='recaptchatest.php'>
<input id="test" name="test" />
<div style="left:100px;" class="g-recaptcha" data-sitekey="my_key"></div>
<input type="submit" value="Send" id="submit" class="buttons" />
</form>
</body></html>
如果(isset($_POST['g-recaptcha-response'])){
$url='1https://www.google.com/recaptcha/api/siteverify';
$data=array('secret'=>'my\u secret\u key',
“response”=>$_POST['g-recaptcha-response'];
$options=array(
“http'=>数组(
'方法'=>“发布”,
“header”=>“内容类型:application/x-www-form-urlencoded”。PHP_EOL,
'content'=>http\u build\u查询($data)
)
);
$context=stream\u context\u create($options);
//$result=file\u get\u contents($result)https://www.google.com/recaptcha/api/siteverify?secret=my_secretkey&response=“.$”后[g-recaptcha-response];
$result=file\u get\u contents($url,false,$context);
回声$结果;
}
?>
为完整起见,
http\u build\u query($data)
的输出是格式正确的查询字符串(secret=&response=
),而stream\u context\u create($options)
的输出是Resource id\3
本地主机上是否安装了流函数?@Dagon yes(当我回显$context
时,我得到resourceid#9
)流函数是否安装在localhost上?@Dagon是(当我回显$context
时,我得到resourceid#9
)