http.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php
  2. class http {
  3. var $method;
  4. var $cookie;
  5. var $post;
  6. var $header;
  7. var $ContentType;
  8. var $errno;
  9. var $errstr;
  10. function __construct() {
  11. $this->method = 'GET';
  12. $this->cookie = '';
  13. $this->post = '';
  14. $this->header = '';
  15. $this->errno = 0;
  16. $this->errstr = '';
  17. }
  18. function post($url, $data = array(), $referer = '', $limit = 0, $timeout = 30, $block = TRUE) {
  19. $this->method = 'POST';
  20. $this->ContentType = "Content-Type: application/x-www-form-urlencoded\r\n";
  21. if($data) {
  22. $post = '';
  23. foreach($data as $k=>$v) {
  24. $post .= $k.'='.rawurlencode($v).'&';
  25. }
  26. $this->post .= substr($post, 0, -1);
  27. }
  28. return $this->request($url, $referer, $limit, $timeout, $block);
  29. }
  30. function get($url, $referer = '', $limit = 0, $timeout = 30, $block = TRUE) {
  31. $this->method = 'GET';
  32. return $this->request($url, $referer, $limit, $timeout, $block);
  33. }
  34. function upload($url, $data = array(), $files = array(), $referer = '', $limit = 0, $timeout = 30, $block = TRUE) {
  35. $this->method = 'POST';
  36. $boundary = "AaB03x";
  37. $this->ContentType = "Content-Type: multipart/form-data; boundary=$boundary\r\n";
  38. if($data) {
  39. foreach($data as $k => $v) {
  40. $this->post .= "--$boundary\r\n";
  41. $this->post .= "Content-Disposition: form-data; name=\"".$k."\"\r\n";
  42. $this->post .= "\r\n".$v."\r\n";
  43. $this->post .= "--$boundary\r\n";
  44. }
  45. }
  46. foreach($files as $k=>$v) {
  47. $this->post .= "--$boundary\r\n";
  48. $this->post .= "Content-Disposition: file; name=\"$k\"; filename=\"".basename($v)."\"\r\n";
  49. $this->post .= "Content-Type: ".$this->get_mime($v)."\r\n";
  50. $this->post .= "\r\n".file_get_contents($v)."\r\n";
  51. $this->post .= "--$boundary\r\n";
  52. }
  53. $this->post .= "--$boundary--\r\n";
  54. return $this->request($url, $referer, $limit, $timeout, $block);
  55. }
  56. function request($url, $referer = '', $limit = 0, $timeout = 30, $block = TRUE) {
  57. $matches = parse_url($url);
  58. $host = $matches['host'];
  59. $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
  60. $port = $matches['port'] ? $matches['port'] : 80;
  61. if($referer == '') $referer = URL;
  62. $out = "$this->method $path HTTP/1.1\r\n";
  63. $out .= "Accept: */*\r\n";
  64. $out .= "Referer: $referer\r\n";
  65. $out .= "Accept-Language: zh-cn\r\n";
  66. $out .= "User-Agent: ".$_SERVER['HTTP_USER_AGENT']."\r\n";
  67. $out .= "Host: $host\r\n";
  68. if($this->cookie) $out .= "Cookie: $this->cookie\r\n";
  69. if($this->method == 'POST') {
  70. $out .= $this->ContentType;
  71. $out .= "Content-Length: ".strlen($this->post)."\r\n";
  72. $out .= "Cache-Control: no-cache\r\n";
  73. $out .= "Connection: Close\r\n\r\n";
  74. $out .= $this->post;
  75. } else {
  76. $out .= "Connection: Close\r\n\r\n";
  77. }
  78. if($timeout > ini_get('max_execution_time')) @set_time_limit($timeout);
  79. $fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
  80. $this->post = '';
  81. if(!$fp) {
  82. $this->errno = $errno;
  83. $this->errstr = $errstr;
  84. return false;
  85. } else {
  86. stream_set_blocking($fp, $block);
  87. stream_set_timeout($fp, $timeout);
  88. fwrite($fp, $out);
  89. $this->data = '';
  90. $status = stream_get_meta_data($fp);
  91. if(!$status['timed_out']) {
  92. $maxsize = min($limit, 1024000);
  93. if($maxsize == 0) $maxsize = 1024000;
  94. $start = false;
  95. while(!feof($fp)) {
  96. if($start) {
  97. $line = fread($fp, $maxsize);
  98. if(strlen($this->data) > $maxsize) break;
  99. $this->data .= $line;
  100. } else {
  101. $line = fgets($fp);
  102. $this->header .= $line;
  103. if($line == "\r\n" || $line == "\n") $start = true;
  104. }
  105. }
  106. }
  107. fclose($fp);
  108. return $this->is_ok();
  109. }
  110. }
  111. function save($file) {
  112. dir_create(dirname($file));
  113. return file_put_contents($file, $this->data);
  114. }
  115. function set_cookie($name, $value) {
  116. $this->cookie .= "$name=$value;";
  117. }
  118. function get_cookie() {
  119. $cookies = array();
  120. if(preg_match_all("|Set-Cookie: ([^;]*);|", $this->header, $m)) {
  121. foreach($m[1] as $c) {
  122. list($k, $v) = explode('=', $c);
  123. $cookies[$k] = $v;
  124. }
  125. }
  126. return $cookies;
  127. }
  128. function get_data() {
  129. if (strpos($this->header,'chunk')) {
  130. $data = explode(chr(13), $this->data);
  131. return $data[1];
  132. } else {
  133. return $this->data;
  134. }
  135. }
  136. function get_header() {
  137. return $this->header;
  138. }
  139. function get_status() {
  140. preg_match("|^HTTP/1.1 ([0-9]{3}) (.*)|", $this->header, $m);
  141. return array($m[1], $m[2]);
  142. }
  143. function get_mime($file) {
  144. $ext = strtolower(trim(substr(strrchr($file, '.'), 1, 10)));
  145. if($ext == '') return '';
  146. $mime_types = array (
  147. 'acx' => 'application/internet-property-stream',
  148. 'ai' => 'application/postscript',
  149. 'aif' => 'audio/x-aiff',
  150. 'aifc' => 'audio/x-aiff',
  151. 'aiff' => 'audio/x-aiff',
  152. 'asp' => 'text/plain',
  153. 'aspx' => 'text/plain',
  154. 'asf' => 'video/x-ms-asf',
  155. 'asr' => 'video/x-ms-asf',
  156. 'asx' => 'video/x-ms-asf',
  157. 'au' => 'audio/basic',
  158. 'avi' => 'video/x-msvideo',
  159. 'axs' => 'application/olescript',
  160. 'bas' => 'text/plain',
  161. 'bcpio' => 'application/x-bcpio',
  162. 'bin' => 'application/octet-stream',
  163. 'bmp' => 'image/bmp',
  164. 'c' => 'text/plain',
  165. 'cat' => 'application/vnd.ms-pkiseccat',
  166. 'cdf' => 'application/x-cdf',
  167. 'cer' => 'application/x-x509-ca-cert',
  168. 'class' => 'application/octet-stream',
  169. 'clp' => 'application/x-msclip',
  170. 'cmx' => 'image/x-cmx',
  171. 'cod' => 'image/cis-cod',
  172. 'cpio' => 'application/x-cpio',
  173. 'crd' => 'application/x-mscardfile',
  174. 'crl' => 'application/pkix-crl',
  175. 'crt' => 'application/x-x509-ca-cert',
  176. 'csh' => 'application/x-csh',
  177. 'css' => 'text/css',
  178. 'dcr' => 'application/x-director',
  179. 'der' => 'application/x-x509-ca-cert',
  180. 'dir' => 'application/x-director',
  181. 'dll' => 'application/x-msdownload',
  182. 'dms' => 'application/octet-stream',
  183. 'doc' => 'application/msword',
  184. 'dot' => 'application/msword',
  185. 'dvi' => 'application/x-dvi',
  186. 'dxr' => 'application/x-director',
  187. 'eps' => 'application/postscript',
  188. 'etx' => 'text/x-setext',
  189. 'evy' => 'application/envoy',
  190. 'exe' => 'application/octet-stream',
  191. 'fif' => 'application/fractals',
  192. 'flr' => 'x-world/x-vrml',
  193. 'flv' => 'video/x-flv',
  194. 'gif' => 'image/gif',
  195. 'gtar' => 'application/x-gtar',
  196. 'gz' => 'application/x-gzip',
  197. 'h' => 'text/plain',
  198. 'hdf' => 'application/x-hdf',
  199. 'hlp' => 'application/winhlp',
  200. 'hqx' => 'application/mac-binhex40',
  201. 'hta' => 'application/hta',
  202. 'htc' => 'text/x-component',
  203. 'htm' => 'text/html',
  204. 'html' => 'text/html',
  205. 'htt' => 'text/webviewhtml',
  206. 'ico' => 'image/x-icon',
  207. 'ief' => 'image/ief',
  208. 'iii' => 'application/x-iphone',
  209. 'ins' => 'application/x-internet-signup',
  210. 'isp' => 'application/x-internet-signup',
  211. 'jfif' => 'image/pipeg',
  212. 'jpe' => 'image/jpeg',
  213. 'jpeg' => 'image/jpeg',
  214. 'jpg' => 'image/jpeg',
  215. 'js' => 'application/x-javascript',
  216. 'latex' => 'application/x-latex',
  217. 'lha' => 'application/octet-stream',
  218. 'lsf' => 'video/x-la-asf',
  219. 'lsx' => 'video/x-la-asf',
  220. 'lzh' => 'application/octet-stream',
  221. 'm13' => 'application/x-msmediaview',
  222. 'm14' => 'application/x-msmediaview',
  223. 'm3u' => 'audio/x-mpegurl',
  224. 'man' => 'application/x-troff-man',
  225. 'mdb' => 'application/x-msaccess',
  226. 'me' => 'application/x-troff-me',
  227. 'mht' => 'message/rfc822',
  228. 'mhtml' => 'message/rfc822',
  229. 'mid' => 'audio/mid',
  230. 'mny' => 'application/x-msmoney',
  231. 'mov' => 'video/quicktime',
  232. 'movie' => 'video/x-sgi-movie',
  233. 'mp2' => 'video/mpeg',
  234. 'mp3' => 'audio/mpeg',
  235. 'mpa' => 'video/mpeg',
  236. 'mpe' => 'video/mpeg',
  237. 'mpeg' => 'video/mpeg',
  238. 'mpg' => 'video/mpeg',
  239. 'mpp' => 'application/vnd.ms-project',
  240. 'mpv2' => 'video/mpeg',
  241. 'ms' => 'application/x-troff-ms',
  242. 'mvb' => 'application/x-msmediaview',
  243. 'nws' => 'message/rfc822',
  244. 'oda' => 'application/oda',
  245. 'p10' => 'application/pkcs10',
  246. 'p12' => 'application/x-pkcs12',
  247. 'p7b' => 'application/x-pkcs7-certificates',
  248. 'p7c' => 'application/x-pkcs7-mime',
  249. 'p7m' => 'application/x-pkcs7-mime',
  250. 'p7r' => 'application/x-pkcs7-certreqresp',
  251. 'p7s' => 'application/x-pkcs7-signature',
  252. 'pbm' => 'image/x-portable-bitmap',
  253. 'pdf' => 'application/pdf',
  254. 'pfx' => 'application/x-pkcs12',
  255. 'pgm' => 'image/x-portable-graymap',
  256. 'php' => 'text/plain',
  257. 'pko' => 'application/ynd.ms-pkipko',
  258. 'pma' => 'application/x-perfmon',
  259. 'pmc' => 'application/x-perfmon',
  260. 'pml' => 'application/x-perfmon',
  261. 'pmr' => 'application/x-perfmon',
  262. 'pmw' => 'application/x-perfmon',
  263. 'png' => 'image/png',
  264. 'pnm' => 'image/x-portable-anymap',
  265. 'pot,' => 'application/vnd.ms-powerpoint',
  266. 'ppm' => 'image/x-portable-pixmap',
  267. 'pps' => 'application/vnd.ms-powerpoint',
  268. 'ppt' => 'application/vnd.ms-powerpoint',
  269. 'prf' => 'application/pics-rules',
  270. 'ps' => 'application/postscript',
  271. 'pub' => 'application/x-mspublisher',
  272. 'qt' => 'video/quicktime',
  273. 'ra' => 'audio/x-pn-realaudio',
  274. 'ram' => 'audio/x-pn-realaudio',
  275. 'ras' => 'image/x-cmu-raster',
  276. 'rgb' => 'image/x-rgb',
  277. 'rmi' => 'audio/mid',
  278. 'roff' => 'application/x-troff',
  279. 'rtf' => 'application/rtf',
  280. 'rtx' => 'text/richtext',
  281. 'scd' => 'application/x-msschedule',
  282. 'sct' => 'text/scriptlet',
  283. 'setpay' => 'application/set-payment-initiation',
  284. 'setreg' => 'application/set-registration-initiation',
  285. 'sh' => 'application/x-sh',
  286. 'shar' => 'application/x-shar',
  287. 'sit' => 'application/x-stuffit',
  288. 'snd' => 'audio/basic',
  289. 'spc' => 'application/x-pkcs7-certificates',
  290. 'spl' => 'application/futuresplash',
  291. 'src' => 'application/x-wais-source',
  292. 'sst' => 'application/vnd.ms-pkicertstore',
  293. 'stl' => 'application/vnd.ms-pkistl',
  294. 'stm' => 'text/html',
  295. 'svg' => 'image/svg+xml',
  296. 'sv4cpio' => 'application/x-sv4cpio',
  297. 'sv4crc' => 'application/x-sv4crc',
  298. 'swf' => 'application/x-shockwave-flash',
  299. 't' => 'application/x-troff',
  300. 'tar' => 'application/x-tar',
  301. 'tcl' => 'application/x-tcl',
  302. 'tex' => 'application/x-tex',
  303. 'texi' => 'application/x-texinfo',
  304. 'texinfo' => 'application/x-texinfo',
  305. 'tgz' => 'application/x-compressed',
  306. 'tif' => 'image/tiff',
  307. 'tiff' => 'image/tiff',
  308. 'tr' => 'application/x-troff',
  309. 'trm' => 'application/x-msterminal',
  310. 'tsv' => 'text/tab-separated-values',
  311. 'txt' => 'text/plain',
  312. 'uls' => 'text/iuls',
  313. 'ustar' => 'application/x-ustar',
  314. 'vcf' => 'text/x-vcard',
  315. 'vrml' => 'x-world/x-vrml',
  316. 'wav' => 'audio/x-wav',
  317. 'wcm' => 'application/vnd.ms-works',
  318. 'wdb' => 'application/vnd.ms-works',
  319. 'wks' => 'application/vnd.ms-works',
  320. 'wmf' => 'application/x-msmetafile',
  321. 'wmv' => 'video/x-ms-wmv',
  322. 'wps' => 'application/vnd.ms-works',
  323. 'wri' => 'application/x-mswrite',
  324. 'wrl' => 'x-world/x-vrml',
  325. 'wrz' => 'x-world/x-vrml',
  326. 'xaf' => 'x-world/x-vrml',
  327. 'xbm' => 'image/x-xbitmap',
  328. 'xla' => 'application/vnd.ms-excel',
  329. 'xlc' => 'application/vnd.ms-excel',
  330. 'xlm' => 'application/vnd.ms-excel',
  331. 'xls' => 'application/vnd.ms-excel',
  332. 'xlt' => 'application/vnd.ms-excel',
  333. 'xlw' => 'application/vnd.ms-excel',
  334. 'xof' => 'x-world/x-vrml',
  335. 'xpm' => 'image/x-xpixmap',
  336. 'xwd' => 'image/x-xwindowdump',
  337. 'z' => 'application/x-compress',
  338. 'zip' => 'application/zip',
  339. );
  340. return isset($mime_types[$ext]) ? $mime_types[$ext] : '';
  341. }
  342. function is_ok() {
  343. $status = $this->get_status();
  344. if(intval($status[0]) != 200) {
  345. $this->errno = $status[0];
  346. $this->errstr = $status[1];
  347. return false;
  348. }
  349. return true;
  350. }
  351. function errno() {
  352. return $this->errno;
  353. }
  354. function errmsg() {
  355. return $this->errstr;
  356. }
  357. }
  358. ?>