更新提示:
1. 增加 301、302 http 重定向
2. 增加 自定义 发送头部
3. 修复 超时时间
4. 增加 http_status_code 属性
5. 支持 https
来源地址: http://www.haowei.me/archives/1154.html args = $args; $charset = isset($this->args['charset']) ? $this->args['charset'] : 'utf-8'; if(!empty($this->args['debugging'])) { set_time_limit(0); header('content-type: text/plain;charset='. $charset); }else{ header('content-type: text/html;charset='. $charset); } if(!isset($this->args['timeout'])) $this->args['timeout'] = 5; $this->args['timeout'] = intval($this->args['timeout']); if(!empty($this->args['redirect'])) $this->attachredirect = true; $this->headerlist = array(); } public static function init(& $instanceof, $args = null) { static $instance; if(!$instance) $instanceof = new self($args); return $instance = $instanceof; } public function setheader($name, $value) { $this->headerlist[$name] = $value; } private function build($args) { list($method, $url, $data, $cookie) = $args; $this->buffer = ''; $this->request = ''; $this->header = array(); $this->response = array(); $useragent = isset($this->args['useragent']) ? $this->args['useragent'] : ( isset($_server['http_user_agent']) ? $_server['http_user_agent'] : __class__ ); extract($parse = parse_url($url)); $path = isset($query) ? $path .'?'. $query : ( isset($path) ? $path : '/' ); $port = isset($port) ? $port : ( $scheme == 'https' ? 443 : 80 ); $protocol = $scheme == 'https' ? 'ssl://' : 'tcp://'; self::$context = fsockopen($protocol . $host, $port, $errno, $errstr, $this->args['timeout']); if($errno) trigger_error(iconv('gbk//ignore', 'utf-8', $errstr), e_user_error); stream_set_blocking(self::$context, 1); stream_set_timeout(self::$context, $this->args['timeout']); $query = $data; if($data && is_array($data)) { $query = array(); foreach($data as $k => $value) array_push($query, $k .'='. $value); $query = implode('&', $query); } array_push($this->header, $method .' '. $path .' http/1.1'); array_push($this->header, 'host: '. $host); array_push($this->header, 'accept: */*'); array_push($this->header, 'content-type: application/x-www-form-urlencoded'); array_push($this->header, 'connection: close'); array_push($this->header, 'user-agent: '. $useragent); if($this->headerlist) foreach($this->headerlist as $name => $value) array_push($this->header, $name .': '. $value); if($cookie) array_push($this->header, 'cookie: '. $cookie ); if($data) array_push($this->header, 'content-length: '. strlen($query)); if($data) array_push($this->header, ''); if($data) array_push($this->header, $query); array_push($this->header, \r\n); $this->request = implode(\r\n, $this->header); fputs(self::$context, $this->request); $skipped = false; $this->http_status_code = 0; $this->http_transfer_chunked = false; while(!feof(self::$context)) { if(($line = fgets(self::$context))) { if(preg_match('/http\/\d\.\d\s*(\d+)/i', $line, $match)) $this->http_status_code = (int) array_pop($match); if(preg_match('/location:\s*(.+)\s*?/i', $line, $match)) ( ($this->http_redirect_url = trim(array_pop($match))) && $skipped = !$skipped ); if(preg_match('/transfer\-encoding:\s*chunked/i', $line, $match)) $this->http_transfer_chunked = true; if(array_push($this->response, $line) && in_array($line, array(\n, \r\n))) break; } } if($this->attachredirect && $skipped) { fclose(self::$context); $data ? call_user_func_array(array($this, $method), array($this->http_redirect_url, $data, $cookie)): call_user_func_array(array($this, $method), array($this->http_redirect_url, $cookie)); } if(!$skipped) { if($this->http_status_code === 200) { $this->buffer = ''; $chunksize = 0; $chunked = ''; while(!feof(self::$context)) { $line = fgets(self::$context); if($this->http_transfer_chunked) { if(!$chunksize) { $chunksize = (int) hexdec(trim(ltrim($line, '0'))) + 2; }else{ if(strlen($chunked) buffer .= substr($chunked, 0, $chunksize - 2); $chunksize = (int) hexdec(trim(ltrim($line, '0'))) + 2; $chunked = ''; } } }else{ $this->buffer .= $line; } } } } return (string) $this->buffer; } public function get($url, $cookie = null) { return $this->build(array('get', $url, null, $cookie)); } public function post($url, $data = null, $cookie = null) { return $this->build(array('post', $url, $data, $cookie)); } public function __set($attr, $value) { $this->$attr = $value; } public function __destruct() { if(is_resource(self::$context)) fclose(self::$context); unset($this->headerlist, $this->header, $this->response, $this->request, $this->args, $this->buffer); } }httpclient::init($httpclient, array('useragent' => $_server['http_user_agent'], 'redirect' => true));$httpclient->get('http://www.haowei.me');echo $httpclient->buffer;
