1 <?php
2 // +----------------------------------------------------------------------
3 // | <--$framwork--> http.class.php http请求类
4 // +----------------------------------------------------------------------
5 // | Copyright (c) <--$company--> All rights reserved.
6 // +----------------------------------------------------------------------
7 // | Licensed ( <--$license--> )
8 // +----------------------------------------------------------------------
9 // | Author: <--$author--> <<--$mail-->>
10 // +----------------------------------------------------------------------
11
12 defined('IN_CORE') or die('No permission resources.');
13 class http {
14 private $_url = '';
15 private $_method = 'GET';
16 private $_referer = '';
17 private $_limit = 0;
18 private $_timeout = 30;
19 private $_block = true;
20 private $_param = '';
21
22 private $_data = '';
23 private $_header = '';
24 private $_errno = 0;
25 private $_errmsg = '';
26 private $_cookie = '';
27
28 public function get() {
29 $this->_method = 'GET';
30 $this->url .= (strpos($this->url, '?') !== false ? '&' : '?').$this->_param;
31 return $this->request();
32 }
33
34 private function request() {
35 $matches = parse_url($this->_url);
36 $host = $matches['host'];
37 $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
38 $port = $matches['port'] ? $matches['port'] : 80;
39 $out = "{$this->_method} {$path} HTTP/1.1\r\n";
40 $out .= "Accept: */*\r\n";
41 $out .= "Referer: {$this->_referer}\r\n";
42 $out .= "Accept-Language: zh-cn\r\n";
43 $out .= "User-Agent: ".$_SERVER['HTTP_USER_AGENT']."\r\n";
44 $out .= "Host: {$host}\r\n";
45 if($this->_cookie) $out .= "Cookie: {$this->_cookie}\r\n";
46 if($this->method == 'POST') {
47 $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
48 $out .= "Content-Length: ".strlen($this->param)."\r\n";
49 $out .= "Cache-Control: no-cache\r\n";
50 $out .= "Connection: Close\r\n\r\n";
51 $out .= $this->param;
52 } else {
53 $out .= "Connection: Close\r\n\r\n";
54 }
55 $fp = fsockopen($host, $port, $errno, $errmsg, $this->_timeout);
56 if ( ! $fp) {
57 $this->_errno = $errno;
58 $this->_errmsg = $errmsg;
59 return false;
60 } else {
61 stream_set_blocking($fp, $this->_block);
62 stream_set_timeout($fp, $this->_timeout);
63 fwrite($fp, $out);
64 $this->_data = '';
65 $status = stream_get_meta_data($fp);
66 if ( ! $status['timed_out']) {
67 $maxsize = min($this->_limit, 1024000);
68 if ($maxsize == 0) $maxsize = 1024000;
69 $start = false;
70 while ( ! feof($fp)) {
71 if ($start) {
72 $line = fread($fp, $maxsize);
73 if (strlen($this->_data) > $maxsize) break;
74 $this->_data .= $line;
75 } else {
76 $line = fgets($fp);
77 $this->_header .= $line;
78 if ($line == "\r\n" || $line == "\n") $start = true;
79 }
80 }
81 }
82 fclose($fp);
83 return $this->is_ok();
84 }
85 }
86
87 public function get_status() {
88 preg_match("|^HTTP/1.1 ([0-9]{3}) (.*)|", $this->_header, $m);
89 return array($m[1], $m[2]);
90 }
91
92 public function is_ok() {
93 $status = $this->get_status();
94 if (intval($status[0]) != 200) {
95 $this->_errno = $status[0];
96 $this->_errmsg = $status[1];
97 return false;
98 }
99 return true;
100 }
101
102 public function get_data() {
103 if (strpos($this->_header, 'chunk')) {
104 $data = explode(chr(13), $this->_data);
105 return $data[1];
106 } else {
107 return $this->_data;
108 }
109 }
110
111 public function get_err() {
112 return array(
113 'message' => $this->_errmsg,
114 'status' => $this->_errno
115 );
116 }
117
118 public function __call($_method, $ags) {
119 switch ($_method) {
120 case 'url':
121 case 'method':
122 case 'referer':
123 case 'limit':
124 case 'timeout':
125 case 'block':
126 $var = '_'.$_method;
127 $this->$var = $ags[0];
128 break;
129 case 'param' :
130 $_c = array();
131 if ($ags[0] && is_array($ags[0])) {
132 $_c = $ags[0];
133 } else if ($ags[0]) {
134 $_c[$ags[0]] = (string)$ags[1];
135 }
136 $post = array();
137 foreach($_c as $k=>$v) {
138 $post[] = $k.'='.rawurlencode($v);
139 }
140 $this->param = implode('&', $post);
141 break;
142 case 'cookie':
143 $_c = array();
144 if ($ags[0] && is_array($ags[0])) {
145 $_c = $ags[0];
146 } else if ($ags[0] && $ags[1]) {
147 $_c[$ags[0]] = $ags[1];
148 }
149 foreach ($_c as $name=>$val) {
150 $this->_cookie .= "$name=$value;";
151 }
152 break;
153 default :
154 return false;
155 break;
156 }
157 return $this;
158 }
159
160 }
161 ?>