微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 什么阻止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,$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以查看它的作用.

原文地址:https://www.jb51.cc/php/134021.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐