php工具类之【浏览器模拟类】

/**
 * 浏览器模拟工具类
 * @version 1.0 20140724
 */
class cURL { 
	var $headers; 
	var $user_agent; 
	var $compression; 
	var $cookie_file; 
	var $proxy; 
	var $httpStatus;
	function __construct($cookies=TRUE,$cookie='cookies.txt',$compression='gzip',$proxy='') { 
		$this->headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'; 
		$this->headers[] = 'Accept-Encoding: gzip,deflate';
		$this->headers[] = 'Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3';
		$this->headers[] = 'Connection: Keep-Alive'; 
		$this->user_agent = 'Mozilla/5.0 (Windows NT 5.1; rv:30.0) Gecko/20100101 Firefox/30.0'; 
		$this->compression=$compression; 
		$this->proxy=$proxy; 
		$this->cookies=$cookies; 
		if ($this->cookies == TRUE) 
			$this->cookie($cookie); 
	} 
	function cookie($cookie_file) { 
		if (file_exists($cookie_file)) { 
			$this->cookie_file=$cookie_file; 
		} else { 
			fopen($cookie_file,'w') or $this->error('The cookie file could not be opened. Make sure this directory has the correct permissions'); 
			$this->cookie_file=$cookie_file; 
			fclose($this->cookie_file); 
		} 
	} 
	function get($url) { 
		$process = curl_init($url); 
		curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers); 
		curl_setopt($process, CURLOPT_HEADER, 0); 
		curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent); 
		if ($this->cookies == TRUE) 
			curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file); 
		if ($this->cookies == TRUE) 
			curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file); 
		curl_setopt($process,CURLOPT_ENCODING , $this->compression); 
		curl_setopt($process, CURLOPT_TIMEOUT, 30); 
		if ($this->proxy) 
			curl_setopt($process, CURLOPT_PROXY, $this->proxy); 
		curl_setopt($process, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1); 
		
		curl_setopt($process, CURLOPT_SSL_VERIFYPEER, FALSE);	// https请求 不验证证书和hosts
		curl_setopt($process, CURLOPT_SSL_VERIFYHOST, FALSE);
		
		$return = curl_exec($process); 
		$this->httpStatus = curl_getinfo($process,CURLINFO_HTTP_CODE); //获取本次请求返回的http状态
		curl_close($process); 
		return $return; 
	} 
	function post($url,$data) { 
		$process = curl_init($url); 
		curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers); 
		curl_setopt($process, CURLOPT_HEADER, 1); 
		curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent); 
		if ($this->cookies == TRUE) 
			curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file); 
		if ($this->cookies == TRUE) 
			curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file); 
		curl_setopt($process, CURLOPT_ENCODING , $this->compression); 
		curl_setopt($process, CURLOPT_TIMEOUT, 30); 
		if ($this->proxy) 
			curl_setopt($process, CURLOPT_PROXY, $this->proxy); 
		curl_setopt($process, CURLOPT_POSTFIELDS, $data); 
		curl_setopt($process, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1); 
		curl_setopt($process, CURLOPT_POST, 1); 
		
		curl_setopt($process, CURLOPT_SSL_VERIFYPEER, FALSE);	// https请求 不验证证书和hosts
		curl_setopt($process, CURLOPT_SSL_VERIFYHOST, FALSE);
		
		$return = curl_exec($process); 
		$this->httpStatus = curl_getinfo($process,CURLINFO_HTTP_CODE); //获取本次请求返回的http状态
		curl_close($process); 
		return $return; 
	} 
	/**
	 * 返回请求后,服务端返回的状态码
	 * @return mixed
	 */
	function getHttpCode(){
		return $this->httpStatus;
	}
	/**
	 * 输出错误信息
	 * @param unknown_type $error
	 */
	function error($error) { 
		echo "<center><div style='width:500px;border: 3px solid #FFEEFF; padding: 3px; background-color: #FFDDFF;font-family: verdana; font-size: 10px'><b>cURL Error</b><br>$error</div></center>"; 
		die; 
	} 
} 
//echo $cc->get('http://www.baidu.com'); 
//$cc->post('http://www.example.com','foo=bar'); 

 

你可能感兴趣的:(php工具类,php模拟浏览器,php登录站点,php对https站点的处理,php爬取网站内容)