经过半天的努力,我终于通过转换这个功能设法让reCAPTCHA工作了:
function _recaptcha_http_post($host,$path,$data,$port = 80) {
$req = _recaptcha_qsencode ($data);
$HTTP_Request = "POST $path HTTP/1.0\r\n";
$HTTP_Request .= "Host: $host\r\n";
$HTTP_Request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
$HTTP_Request .= "Content-Length: " . strlen($req) . "\r\n";
$HTTP_Request .= "User-Agent: reCAPTCHA/PHP\r\n";
$HTTP_Request .= "\r\n";
$HTTP_Request .= $req;
$response = "";
if( false == ( $fs = @fsockopen($host,$port,$errno,$errstr,10) ) ) {
die ("Could not open socket");
}
fwrite($fs,$HTTP_Request);
while ( !feof($fs) )
$response .= fgets($fs,1160); // One TCP-IP packet
fclose($fs);
$response = explode("\r\n\r\n",$response,2);
return $response;
}
至:
function _recaptcha_http_post($host,$port = 80) {
$req = _recaptcha_qsencode ($data);
$request = curl_init("http://".$host.$path);
curl_setopt($request,CURLOPT_USERAGENT,"reCAPTCHA/PHP");
curl_setopt($request,CURLOPT_POST,true);
curl_setopt($request,CURLOPT_POSTFIELDS,$req);
curl_setopt($request,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($request);
return $response;
}
基本上,我有兴趣找出为什么curl工作,而fsockopen失败“无法打开套接字”.谢谢.
此外:启用了套接字支持.
我可能错了,但你在fsockopen()中使用$port = 80,而在cURL情况下,根本不使用这个变量.当尝试通过端口80而不是端口443连接到SSL时,我遇到了同样的问题;据我所知,cURL默认检查SSL并相应地连接.
另外,尝试使用CURLOPT_VERBOSE运行cURL以查看它的作用.