PHP HTTP类库

<?php
/**
 * 封装基于curl的HTTP类库
 *
 * @file    class_httplib.php
 * @author  qaulau@hotmail.com
 * @date    2013-8-1
 */

class http{

	private $response;
	private $response_header;
	private $response_data;
	private $response_info;
	private $response_httpcode;
	private $timeout = 60;
	private $cookie = '';
	private $useragent = 'PHP-HttpLib-v1.0';
	private $request_header = array();
	private $starttime;
	private $request_proxy = array();
	private $request_method;

	public function __construct(){
		$this->starttime = self::_microtime();
		//能有效提高POST大于1M数据时的请求速度
		$this->set_header('Expect');
		//设置浏览器信息
		$this->set_useragent($_SERVER['HTTP_USER_AGENT']);
		//设置请求的方式为XMLHttpRequest,模拟ajax请求信息,可用于某些api的限制,此处仅为演示说明
		$this->set_header('X-Requested-With','XMLHttpRequest');
	}

	/**
	 * 设置cookie(可选)
	 * @param <array|string> $cookie
	 */
	public function set_cookie($cookie = ''){
		if (is_array($cookie)){
			$cookies = array();
			foreach ($cookie as $k => $v){
				$cookies[] = $k.'='.$v;
			}
			$this->cookie .= implode('; ', $cookies);
		}else{
			$this->cookie .= $cookie;
		}
	}

	/**
	 * 设置用户浏览器信息(可选)
	 * @param string $useragent : 浏览器代理信息
	 */
	public function set_useragent($useragent = ''){
		$this->useragent = $useragent;
	}

	/**
	 * 设置头部伪造IP(可选),对某些依靠头部信息判断ip的网站有效
	 * @param string $ip : 需要伪造的ip
	 */
	public function set_forgeip($ip){
		$this->set_header('CLIENT-IP',$ip);
		$this->set_header('X-FORWARDED-FOR',$ip);
	}

	/**
	 * 添加请求头部信息
	 * @param string $k
	 * @param string $v
	 */
	public function set_header($k,$v = ''){
		if (!empty($k)){
			$this->request_header[] = $k.':'.$v;
		}
	}

	/**
	 * 设置超时时间(可选)
	 * @param int $sec
	 */
	public function set_timeout($sec){
		if($sec > ini_get('max_execution_time')) @set_time_limit($sec);
		$this->timeout = $sec;
	}

	/**
	 * 设置代理(可选),如对方限制了ip访问或需要隐藏真实ip
	 * @param string $host	:代理服务器(ip或者域名)
	 * @param int	 $port	:代理服务端口
	 * @param string $user	:用户名(如果有)
	 * @param string $pass	:用户密码(如果有)
	 */
	public function set_proxy($host,$port = '',$user = '',$pass = ''){
		$this->request_proxy = array('host'=>$host,'port'=>$port,'user'=>$user,'pass'=>$pass);
	}

	/**
	 * 请求url
	 * @param string $url		: 请求的地址
	 * @param <array|string> $postdata :该项如果填写则为POST方式,否则为GET方式;如需上传文件,需在文件路径前加上@符号
	 * @param string $referer	: 来路地址,用于伪造来路
	 */
	public function request($url,$postdata = '',$referer=''){
		$ch = $this->_http_request($url,$postdata,$referer);
		if ($postdata == ''){
			$this->request_method = 'GET';
		}else{
			$this->request_method = 'POST';
		}
        $this->response = curl_exec($ch);
		if ($this->response === false) {
            //throw new Exception('cURL resource: ' . (string) $ch . '; cURL error: ' . curl_error($ch) . ' (' . curl_errno($ch) . ')');
            $this->response_httpcode = 204; //没有内容,请求失败
            $this->response_data = 'URL:'.$url.' cURL resource: ' . (string) $ch . '; cURL error: ' . curl_error($ch) . ' (' . curl_errno($ch) . ')';
			return false;
        }
        $this->_process_response($ch);
        $code = $this->get_statcode();
        if ($code == '301' || $code == '302') {
        	$info = $this->get_info();
        	if (empty($info['redirect_url'])) {
        		$this->response_httpcode = 204; //没有内容,请求失败
	            $this->response_data = 'TYPE: '.$code.' URL:'.$url.' cURL resource: ' . (string) $ch . '; cURL error: ' . curl_error($ch) . ' (' . curl_errno($ch) . ')';
				return false;
        	}
        	$url = $info['redirect_url'];
        	return $this->request($url,$postdata,$referer);
        }
        curl_close($ch);
	}

	/**
	 * 获取响应的所有信息
	 */
	public function get_response(){
		return $this->response;
	}

	/**
	 * 获取响应的header信息
	 * @param string $key	: (可选)header头部信息标示如获取Set-Cookie
	 * @return string
	 */
	public function get_header($key = ''){
		if ($key == ''){
			return $this->response_header;
		}
		if (!isset($this->response_header[$key])){
			return NULL;
		}
		return $this->response_header[$key];
	}

	/**
	 * 获取响应的cookie
	 * @param boolean $assoc : 选择true将返回数组否则返回字符串
	 */
	public function get_cookie($assoc = false){	
		$cookies = $this->get_header('Set-Cookie');
		if ($cookies){
			foreach ($cookies as $v){
				$cookieinfo = explode(';', $v);
				if(strpos($this->cookie,$cookieinfo[0]) === FALSE){
					$this->cookie .= $cookieinfo[0];
					$this->cookie .= '; ';
				}
			}
		}
		if ($assoc && $this->cookie){
			$cookie = substr($this->cookie, 0,-3);
			$cookie = explode('; ', $cookie);
			$cookies = array();
			foreach ($cookie as $v){
				$cookieinfo = explode('=', $v);
				$cookies[$cookieinfo[0]] = $cookieinfo[1];
			}
			return $cookies;

		}
		return $this->cookie;
	}

	/**
	 * 获取响应的页面数据即页面代码
	 */
	public function get_data(){
		return $this->response_data;
	}

	/**
	 * 获取响应的网页编码
	 */
	public function get_charset(){
		$content_type = $this->get_header('Content-Type');
		if ($content_type) {
			preg_match('/charset=(.+)/i', $content_type, $matches);
			if (isset($matches[1])){
				return strtoupper(trim($matches[1]));
			}
		}
		return NULL;
	}

	/**
	 * 获取连接资源的信息(返回数组)
	 */
	public function get_info(){
		return $this->response_info;
	}

	/**
	 * 获取响应的网页状态码 (注:200为正常响应)
	 */
	public function get_statcode(){
		return $this->response_httpcode;
	}

	/**
	 * 获取请求方法
	 */
	public function  get_method(){
		return $this->request_method;
	}

	/**
	 * 获取请求时间
	 */
	public function get_requesttime(){
		return self::_microtime() - $this->starttime;
	}

	/**
	 * 处理响应的数据
	 * @param resource $ch
	 */
	private function _process_response($ch = null){
		 if (is_resource($ch)) {
		 	$content_size = curl_getinfo($ch,CURLINFO_CONTENT_LENGTH_DOWNLOAD);
		 	if($content_size > 0){
		 		$this->response_header = substr($this->response, 0, -$content_size);
		 		$this->response_data = substr($this->response, -$content_size);
		 	}else{
		 	 	$header_size = curl_getinfo($ch,CURLINFO_HEADER_SIZE);
                $this->response_header = substr($this->response, 0, $header_size);
                $this->response_data = substr($this->response, $header_size);
		 	}	
		 	$this->response_httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		 	$this->response_info = curl_getinfo($ch);
		 	//分解响应头部信息
		 	$this->response_header = explode("\r\n\r\n", trim($this->response_header));
            $this->response_header = array_pop($this->response_header);
            $this->response_header = explode("\r\n", $this->response_header);
            array_shift($this->response_header); //开头为状态
            //分割数组
            $header_assoc = array();
            foreach ($this->response_header as $header) {
                $kv = explode(': ', $header);
                if($kv[0] == 'Set-Cookie'){
                	$header_assoc['Set-Cookie'][] = $kv[1]; 
                }else{
                	$header_assoc[$kv[0]] = $kv[1];
                }
            }
            $this->response_header = $header_assoc;
		 }
		 return false;
	}

	/**
	 * 请求数据
	 * @param string $url
	 * @param string|array $post_data
	 * @param string $referer
	 */
	private function _http_request($url,$post_data,$referer=''){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, 1);
		curl_setopt($ch, CURLOPT_USERAGENT, $this->useragent);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
		//强制使用IPV4协议解析域名,否则在支持IPV6的环境下请求会异常慢
		if(defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')){
			@curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
		}
		if ($post_data){
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		}
		if ($this->cookie){
			curl_setopt($ch, CURLOPT_COOKIE, $this->cookie);
		}
		if ($referer){
			curl_setopt($ch, CURLOPT_REFERER, $referer);
		}
		if ($this->request_header){
			curl_setopt($ch, CURLOPT_HTTPHEADER, $this->request_header);
		}
		if ($this->request_proxy) {
            curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
            $host = $this->request_proxy['host'];
            $host .= ($this->request_proxy['port']) ? ':' . $this->request_proxy['port'] : '';
            curl_setopt($ch, CURLOPT_PROXY, $host);
            if (isset($this->request_proxy['user']) && isset($this->request_proxy['pass'])) {
                curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->request_proxy['user'] . ':' . $this->request_proxy['pass']);
            }
        }
		return $ch;
	} 

	private static function _microtime(){
		return array_sum(explode(' ', microtime()));
	}
	public static function getMillisecond() {
		list($s1, $s2) = explode(' ', microtime());
		return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
	}
	public static function downAllImg($body  = '',$host = '',$postion = 2,$dir = 'attachs'){
		if (!empty($body)) {
			global $cfg;
		    $body = stripslashes($body);
		    $body = str_replace(PHP_EOL, '', $body);
		    $img_array = array();
		    preg_match_all("/(src|SRC)=[\"|'| ]{0,}((.*)\.(gif|jpg|jpeg|bmp|png))/isU", $body, $img_array);
		    // preg_match_all("/(src|SRC)=[\"|'| ]{0,}(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png))/isU", $body, $img_array);
		    if (empty($img_array) || empty($img_array[0])) {
		    	return $body;
		    }
		    $fileExt = $img_array[4];
		    $img_array = array_unique($img_array[2]);
		    set_time_limit(0);
		    foreach ($img_array as $key => $value) {
		        $value = trim($value);
		        $value = str_replace('"', '', $value);
		        $value = str_replace("'", '', $value);
		        $fileurl = '/upload/'.$dir.'/imgs/'.date('Ymd').'/'.md5(self::getMillisecond().'_'.$key.rand(100,999)).'.'.$fileExt[$key];
		        $fileName = ROOT.$fileurl;
		        if (sub($value,0,4,0) != 'http') {
		        	$_value = $host.$value;
		        }else{
		        	$_value = $value;
		        }
		        if (sub($_value,0,20,'') == 'http://mmbiz.qpic.cn') {
		        	continue;
		        }
		        
		        $get_file = @file_get_contents($_value);
		        if ($get_file) {
		        	dir_create(dirname($fileName));
		            $fp = @fopen($fileName, 'w');
		            @fwrite($fp, $get_file);
		            @fclose($fp);
		        }
		        $body = str_replace($value, img($fileurl), $body);
		        /*$body = preg_replace('/<img.+?src=\"(.+?)\".+?>/','<img src="\1" />',$body);*/
		    }
		    $body = addslashes($body);
		}
		return $body;
	}
	public static function downAllfile($body  = '',$host = '',$postion = 2,$dir = 'attachs'){
		if (!empty($body)) {
		    $body = stripslashes($body);
		    $body = str_replace(PHP_EOL, '', $body);
		    $img_array = array();
		    preg_match_all("/(href|HREF)=[\"|'| ]{0,}((.*)\.(xls|xlsx|doc|docx|rar|zip))/isU", $body, $img_array);
		    // preg_match_all("/(src|SRC)=[\"|'| ]{0,}(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png))/isU", $body, $img_array);
		    if (empty($img_array) || empty($img_array[0])) {
		    	return $body;
		    }
		    $fileExt = $img_array[4];
		    $img_array = array_unique($img_array[2]);
		    set_time_limit(0);
		    foreach ($img_array as $key => $value) {
		        $value = trim($value);
		        $value = str_replace('"', '', $value);
		        $value = str_replace("'", '', $value);
		        $fileurl = '/upload/'.$dir.'/file/'.date('Ymd').'/'.md5(self::getMillisecond().'_'.$key.rand(100,999)).'.'.$fileExt[$key];
		        $fileName = ROOT.$fileurl;
		        if (sub($value,0,4,0) != 'http') {
		        	$_value = $host.$value;
		        }else{
		        	$_value = $value;
		        }
		        if (sub($_value,0,20,'') == 'http://mmbiz.qpic.cn') {
		        	continue;
		        }
		        
		        $get_file = @file_get_contents($_value);
		        if ($get_file) {
		        	dir_create(dirname($fileName));
		            $fp = @fopen($fileName, 'w');
		            @fwrite($fp, $get_file);
		            @fclose($fp);
		        }
		        $body = str_replace($value, img($fileurl), $body);
		    }
		    $body = addslashes($body);
		}
		return $body;
	}
	public static function downImg($url = '',$dir = 'attachs'){
		if (empty($url)) {
			return '';
		}
		$fileExt = self::get_extension($url);
		$fileurl = '/upload/'.$dir.'/imgs/'.date('Ymd').'/'.md5(self::getMillisecond().'_'.rand(100,999)).'.'.$fileExt;
		$fileName = ROOT.$fileurl;
		$get_file = @file_get_contents($url);
		if ($get_file) {
			dir_create(dirname($fileName));
			$fp = @fopen($fileName, 'w');
			@fwrite($fp, $get_file);
			@fclose($fp);
		}
		return img($fileurl);
	}
	static function get_extension($file)
	{
		return pathinfo($file, PATHINFO_EXTENSION);
	}
}

张飞贡献,谢谢兄弟

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值