什么是阻塞fsockopen? [英] What is blocking fsockopen?
问题描述
在挣扎半天后,我终于设法通过转换这个函数让reCAPTCHA工作:
After struggling for half a day, I finally manage to get reCAPTCHA to work by converting this function:
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
失败,并显示无法打开套接字。非常感谢。
Basically, I am interested to find out why curl
works while fsockopen
fails with "Could not open socket". Thanks.
此外:已启用套接字支持。
In addition: Sockets Support is enabled.
推荐答案
我可能错了,但在 fsockopen()
中使用 $ port = 80
在 cURL 中,此变量未使用。我尝试通过端口80
而不是端口 443
连接到 SSL ;据我所知, cURL
默认情况下会检查 SSL ,并相应连接。
I might be wrong, but you use $port = 80
in fsockopen()
while in cURL case this variable is not used at all. I had same problem when tried to connect to SSL via port 80
instead of port 443
; as far as I know, cURL
checks SSL by default and connects accordingly.
此外,尝试使用 CURLOPT_VERBOSE 运行 cURL
,以查看它的功能。
Also, try running cURL
with CURLOPT_VERBOSE to see what it does.
这篇关于什么是阻塞fsockopen?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!