折腾了半天,终于把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, $path, $data, $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 的情况下这个变量没有被使用全部。当尝试通过 port 80
而不是端口 443
连接到 SSL 时,我遇到了同样的问题;据我所知,cURL
默认检查 SSL 并进行相应连接。
此外,尝试使用 CURLOPT_VERBOSE 运行 cURL
以查看它的作用。
关于php - 什么阻止了 fsockopen?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10471942/