Home > php教程 > PHP源码 > fsocket

fsocket

PHP中文网
Release: 2016-05-25 17:01:09
Original
1302 people have browsed it

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=&#39;GET&#39;,$file=&#39;/&#39;,$params=array(),$head=array(),$cookies=array()){
		$_params = $_cookies = &#39;&#39;;
		if(is_array($params)){
			foreach($params as $key=>$value){ 
				$_params .= "&".$key."=".urlencode($value); 
			}
			$_params = (strlen($_params) > 1) ? substr($_params,1) : &#39;&#39;;
		}else if(is_string($params)){
			$_params = urlencode($params);
		}
		foreach($cookies as $key=>$value){
			$_cookies .= "; ".$key."=".urlencode($value); 
		}
		$_cookies = (strlen($_cookies) > 2) ? substr($_cookies,2) : &#39;&#39;;
		$file .= ($type == &#39;GET&#39;) ? ($_params == &#39;&#39; ? &#39;&#39; : &#39;?&#39;.$_params) : &#39;&#39;;
		$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 == &#39;GET&#39;) ? &#39;&#39; : "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 != &#39;&#39;) $header .= "Cookie: ".$_cookies."\r\n";
		/*
		foreach($_SERVER as $name => $value){
			if(substr($name, 0, 5) == &#39;HTTP_&#39; && $name != &#39;HTTP_HOST&#39;){
				$header .= str_replace(&#39; &#39;, &#39;-&#39;, ucwords(strtolower(str_replace(&#39;_&#39;, &#39; &#39;, 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=&#39;&#39;){
		if(function_exists(&#39;fsockopen&#39;)){
			$fp = fsockopen($host,$port,$errno,$errstr,10);
		}else if(function_exists(&#39;pfsockopen&#39;)){
			$fp = pfsockopen($host,$port,$errno,$errstr,10);
		}else if(function_exists(&#39;stream_socket_client&#39;)){
			$fp = stream_socket_client($host.&#39;:&#39;.$port,$errno,$errstr,10);
		}else{
			$fp = $this->_fsockopen($host,$port,$errno,$errstr,10);
		}
		$fp = fsockopen($host,$port,$errno,$errstr,10);
		if(!$fp) return array(&#39;header&#39;=>&#39;&#39;,&#39;data&#39;=>$errstr."--->".$errno,&#39;cookie&#39;=>&#39;&#39;);
		$reHeader = $reData = "";
		$cookies = array();
		fputs($fp,$header);
		$maxlen = $this->_getDataHeader($fp,$reHeader,$cookies);
		$reData = $this->_getDataBody($fp,$maxlen);
		fclose($fp);
		return array(&#39;header&#39;=>$reHeader,&#39;data&#39;=>$reData,&#39;cookie&#39;=>$cookies);
	}

}
Copy after login

2.

	/* demo */

	$host = &#39;www.oschina.net&#39;;
	$port = &#39;80&#39;;
	$type = &#39;POST&#39;;
	$file = &#39;/&#39;;
	
	$params = &#39;&#39;;

	//include_once(&#39;include/zsocket.class.php&#39;); //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);
Copy after login

           

       

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template