php - 什么阻止了 fsockopen?

标签 php curl fsockopen

折腾了半天,终于把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/

相关文章:

php - 上面打印变量

php - 如何将变量附加到已经有变量的 url?

macos - 由于 "unknown message digest algorithm",curl 无法访问 github.com

php - 在 php mysql 查询中不会合并包含表中的所有字段

php - CodeIgniter 相对路径或基本 URL 函数?

c++ - Libcurl 不会发送 SMTP 退出命令

redirect - 阻止curl在302重定向上发送授权 header

php - 在 PHP 中使用 CURL 或 fsockopen 的 Wordpress 自动登录

php - fsockopen 在端口 25 和 587 上被阻止

configuration - 在 PHP fsockopen() 中启用 SSL