您好,欢迎访问一九零五行业门户网

fsocket

1.
_setcookie(substr($line, 12)); } $reheader .= $line; if(substr($line, 0, 16) == 'content-length: '){ $maxlen = intval(substr($line, 16, -2)); } if($line == "\r\n" || $line == "") break; } return $maxlen; } /* * 获取返回数据正文内容 */ private function _getdatabody(&$fp,$maxlen){ $redata = ""; while(!feof($fp)){ $line = fgets($fp,$maxlen+1); $redata .= $line; if(strlen($line) < $maxlen) $maxlen = $maxlen - strlen($line); else break; } return $redata; } /* * 设置并返回要发送的header内容 */ public function get_headerinfo($host,$type='get',$file='/',$params=array(),$head=array(),$cookies=array()){ $_params = $_cookies = ''; if(is_array($params)){ foreach($params as $key=>$value){ $_params .= "&".$key."=".urlencode($value); } $_params = (strlen($_params) > 1) ? substr($_params,1) : ''; }else if(is_string($params)){ $_params = urlencode($params); } foreach($cookies as $key=>$value){ $_cookies .= "; ".$key."=".urlencode($value); } $_cookies = (strlen($_cookies) > 2) ? substr($_cookies,2) : ''; $file .= ($type == 'get') ? ($_params == '' ? '' : '?'.$_params) : ''; $header = $type." ".$file." http/1.1\r\n"; $header .= "host: ".$host."\r\n"; //$header .= "referer: ".get_ip()."\r\n"; //$header .= "x-forwarded-for: ".get_ip()."\r\n"; $header .= ($type == 'get') ? '' : "content-type: application/x-www-form-urlencoded\r\n"; if(is_array($head) && $head != array()){ foreach($head as $k=>$v){ $header .= $k.": ".$v."\r\n"; } } $header .= "content-length: ".strlen($_params)."\r\n"; if($_cookies != '') $header .= "cookie: ".$_cookies."\r\n"; /* foreach($_server as $name => $value){ if(substr($name, 0, 5) == 'http_' && $name != 'http_host'){ $header .= str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))).":".$value."\r\n"; } } */ $header .= "connection: close\r\n\r\n"; $header .= $_params."\r\n"; return $header; } /* * 发送,并返回结果 array */ public function get_senddata($host,$port=80,$header=''){ if(function_exists('fsockopen')){ $fp = fsockopen($host,$port,$errno,$errstr,10); }else if(function_exists('pfsockopen')){ $fp = pfsockopen($host,$port,$errno,$errstr,10); }else if(function_exists('stream_socket_client')){ $fp = stream_socket_client($host.':'.$port,$errno,$errstr,10); }else{ $fp = $this->_fsockopen($host,$port,$errno,$errstr,10); } $fp = fsockopen($host,$port,$errno,$errstr,10); if(!$fp) return array('header'=>'','data'=>$errstr."--->".$errno,'cookie'=>''); $reheader = $redata = ""; $cookies = array(); fputs($fp,$header); $maxlen = $this->_getdataheader($fp,$reheader,$cookies); $redata = $this->_getdatabody($fp,$maxlen); fclose($fp); return array('header'=>$reheader,'data'=>$redata,'cookie'=>$cookies); } }
2. /* demo */ $host = 'www.oschina.net'; $port = '80'; $type = 'post'; $file = '/'; $params = ''; //include_once('include/zsocket.class.php'); //include $zsk = new zsocket(); $header = $zsk->get_headerinfo($host,$type,$file,$params); $data = $zsk->get_senddata($host,$port,$header); /* echo "\r\n"; */ var_dump($header); var_dump($data);
其它类似信息

推荐信息