您好,登錄后才能下訂單哦!
這篇文章主要講解了“php自定義類fsocket怎么模擬post或get請(qǐng)求”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“php自定義類fsocket怎么模擬post或get請(qǐng)求”吧!
本文實(shí)例講述了php自定義類fsocket模擬post或get請(qǐng)求的方法。分享給大家供大家參考。具體如下:
zsocket.class.php文件如下:
<?php class ZSocket { /* * Init */ private function _fsockopen($host, $port, &$errno, &$errstr, $timeout){ $ip = @gethostbyname($host); $s = @socket_create(AF_INET, SOCK_STREAM, 0); if(socket_set_nonblock($s)){ $r = @socket_connect($s, $ip, $port); if ($r || socket_last_error() == EINPROGRESS) { $errno = EINPROGRESS; return $s; } } $errno = socket_last_error($s); $errstr = socket_strerror($errno); socket_close($s); return false; } /* * 設(shè)置Cookie */ private function _setCookie($cookie){ $_cookies = explode("; ",$cookie); $_tmp = explode("=",$_cookies[0]); setcookie($_tmp[0], $_tmp[1]); return $_cookies; } /* * 獲取返回?cái)?shù)據(jù)header內(nèi)容 */ private function _getDataHeader(&$fp,&$reHeader,&$cookies){ $maxlen = 0; while(!feof($fp)){ $line = fgets($fp,1024); if(substr($line, 0, 12) == 'Set-Cookie: '){ $cookies[] = $this->_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; } /* * 獲取返回?cái)?shù)據(jù)正文內(nèi)容 */ 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; } /* * 設(shè)置并返回要發(fā)送的header內(nèi)容 */ 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; } /* * 發(fā)送,并返回結(jié)果 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); } }
demo代碼如下:
$host = 'www.jb51.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"; print_r($header); print_r($data); echo "-->\r\n"; */ var_dump($header); var_dump($data);
感謝各位的閱讀,以上就是“php自定義類fsocket怎么模擬post或get請(qǐng)求”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)php自定義類fsocket怎么模擬post或get請(qǐng)求這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。