php – 什么阻止fsockopen?
发布时间:2020-12-13 17:55:59 所属栏目:PHP教程 来源:网络整理
导读:经过半天的努力,我终于通过转换这个功能设法让reCAPTCHA工作了: function _recaptcha_http_post($host,$path,$data,$port = 80) { $req = _recaptcha_qsencode ($data); $http_request = "POST $path HTTP/1.0rn"; $http_request .= "Host: $hostrn"; $
经过半天的努力,我终于通过转换这个功能设法让reCAPTCHA工作了:
function _recaptcha_http_post($host,$path,$data,$port = 80) { $req = _recaptcha_qsencode ($data); $http_request = "POST $path HTTP/1.0rn"; $http_request .= "Host: $hostrn"; $http_request .= "Content-Type: application/x-www-form-urlencoded;rn"; $http_request .= "Content-Length: " . strlen($req) . "rn"; $http_request .= "User-Agent: reCAPTCHA/PHPrn"; $http_request .= "rn"; $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("rnrn",$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以查看它的作用. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |