php – 什么阻止fsockopen?

前端之家收集整理的这篇文章主要介绍了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.f2er.com/php/134021.html

猜你在找的PHP相关文章