http_class.php
26.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
<?php
/**
* Tiny - A PHP Framework For Web Artisans
* @author Tiny <tinylofty@gmail.com>
* @copyright Copyright(c) 2010-2014 http://www.tinyrise.com All rights reserved
* @version 1.0
*/
/**
* 数据采集,doGET,doPOST,文件下载,
*
* @author Tiny
* @class Http
*/
class Http
{
static public $way=0;
//手动设置访问方式
static public function setWay($way)
{
self::$way=intval($way);
}
static public function getSupport()
{
//如果指定访问方式,则按指定的方式去访问
if(isset(self::$way)&&in_array(self::$way,array(1,2,3)))
return self::$way;
//自动获取最佳访问方式
if(function_exists('curl_init'))//curl方式
{
return 1;
}
else if(function_exists('fsockopen'))//socket
{
return 2;
}
else if(function_exists('file_get_contents'))//php系统函数file_get_contents
{
return 3;
}
else
{
return 0;
}
}
//通过get方式获取数据
static public function doGet($url,$timeout=5,$header="")
{
if(empty($url)||empty($timeout))
return false;
if(!preg_match('/^(http|https)/is',$url))
$url="http://".$url;
$code=self::getSupport();
switch($code)
{
case 1:return self::curlGet($url,$timeout,$header);break;
case 2:return self::socketGet($url,$timeout,$header);break;
case 3:return self::phpGet($url,$timeout,$header);break;
default:return false;
}
}
//通过POST方式发送数据
static public function doPost($url, $post_data=array(), $timeout=5,$header="")
{
if(empty($url)||empty($post_data)||empty($timeout))
return false;
if(!preg_match('/^(http|https)/is',$url))
$url="http://".$url;
$code=self::getSupport();
switch($code)
{
case 1:return self::curlPost($url,$post_data,$timeout,$header);break;
case 2:return self::socketPost($url,$post_data,$timeout,$header);break;
case 3:return self::phpPost($url,$post_data,$timeout,$header);break;
default:return false;
}
}
//通过curl get数据
static public function curlGet($url,$timeout=5,$header="")
{
$header=empty($header)?self::defaultHeader():$header;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));//模拟的header头
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
//通过curl post数据
static public function curlPost($url, $post_data=array(), $timeout=5,$header="")
{
$header=empty($header)?'':$header;
$post_string = http_build_query($post_data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));//模拟的header头
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
//通过socket get数据
static public function socketGet($url,$timeout=5,$header="")
{
$header=empty($header)?self::defaultHeader():$header;
$url2 = parse_url($url);
$url2["path"] = isset($url2["path"])? $url2["path"]: "/" ;
$url2["port"] = isset($url2["port"])? $url2["port"] : 80;
$url2["query"] = isset($url2["query"])? "?".$url2["query"] : "";
$host_ip = @gethostbyname($url2["host"]);
if(($fsock = fsockopen($host_ip, $url2['port'], $errno, $errstr, $timeout)) < 0){
return false;
}
$request = $url2["path"] .$url2["query"];
$in = "GET " . $request . " HTTP/1.0\r\n";
if(false===strpos($header, "Host:"))
{
$in .= "Host: " . $url2["host"] . "\r\n";
}
$in .= $header;
$in .= "Connection: Close\r\n\r\n";
if(!@fwrite($fsock, $in, strlen($in))){
@fclose($fsock);
return false;
}
return self::GetHttpContent($fsock);
}
//通过socket post数据
static public function socketPost($url, $post_data=array(), $timeout=5,$header="")
{
$header=empty($header)?self::defaultHeader():$header;
$post_string = http_build_query($post_data);
$url2 = parse_url($url);
$url2["path"] = ($url2["path"] == "" ? "/" : $url2["path"]);
$url2["port"] = ($url2["port"] == "" ? 80 : $url2["port"]);
$host_ip = @gethostbyname($url2["host"]);
$fsock_timeout = $timeout; //超时时间
if(($fsock = fsockopen($host_ip, $url2['port'], $errno, $errstr, $fsock_timeout)) < 0){
return false;
}
$request = $url2["path"].($url2["query"] ? "?" . $url2["query"] : "");
$in = "POST " . $request . " HTTP/1.0\r\n";
$in .= "Host: " . $url2["host"] . "\r\n";
$in .= $header;
$in .= "Content-type: application/x-www-form-urlencoded\r\n";
$in .= "Content-Length: " . strlen($post_string) . "\r\n";
$in .= "Connection: Close\r\n\r\n";
$in .= $post_string . "\r\n\r\n";
unset($post_string);
if(!@fwrite($fsock, $in, strlen($in))){
@fclose($fsock);
return false;
}
return self::GetHttpContent($fsock);
}
//通过file_get_contents函数get数据
static public function phpGet($url,$timeout=5,$header="")
{
$header=empty($header)?self::defaultHeader():$header;
$opts = array(
'http'=>array(
'protocol_version'=>'1.0', //http协议版本(若不指定php5.2系默认为http1.0)
'method'=>"GET",//获取方式
'timeout' => $timeout ,//超时时间
'header'=> $header)
);
$context = stream_context_create($opts);
return @file_get_contents($url,false,$context);
}
//通过file_get_contents 函数post数据
static public function phpPost($url, $post_data=array(), $timeout=5,$header="")
{
$header=empty($header)?self::defaultHeader():$header;
$post_string = http_build_query($post_data);
$header.="Content-length: ".strlen($post_string);
$opts = array(
'http'=>array(
'protocol_version'=>'1.0',//http协议版本(若不指定php5.2系默认为http1.0)
'method'=>"POST",//获取方式
'timeout' => $timeout ,//超时时间
'header'=> $header,
'content'=> $post_string)
);
$context = stream_context_create($opts);
return @file_get_contents($url,false,$context);
}
//默认模拟的header头
static private function defaultHeader()
{
$header="User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12\r\n";
$header.="Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
$header.="Accept-language: zh-cn,zh;q=0.5\r\n";
$header.="Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7\r\n";
return $header;
}
//获取通过socket方式get和post页面的返回数据
static private function GetHttpContent($fsock=null)
{
$out = null;
while($buff = @fgets($fsock, 2048)){
$out .= $buff;
}
fclose($fsock);
$pos = strpos($out, "\r\n\r\n");
$head = substr($out, 0, $pos); //http head
$status = substr($head, 0, strpos($head, "\r\n")); //http status line
$body = substr($out, $pos + 4, strlen($out) - ($pos + 4));//page body
if(preg_match("/^HTTP\/\d\.\d\s([\d]+)\s.*$/", $status, $matches))
{
if(intval($matches[1]) / 100 == 2)
{
return $body;
}
else
{
return false;
}
}
else
{
return false;
}
}
static public function zip($files,$zipfile){
if(class_exists('ZipArchive'))
{
$zip = new ZipArchive();
$zip->open($zipfile,ZIPARCHIVE::CREATE);
if(is_array($files))
{
foreach($files as $file)
{
if(is_file($file))$zip->addFile($file,basename($file));
}
}
else{
if(is_file($files))$zip->addFile($files,basename($files));
}
$zip->close();
return true;
}
else
{
return false;
}
}
//下载导出CSV文件
static public function exportCSV($headings=false, $rows=false, $filename=false)
{
if (!empty($rows)){
$name = ($filename !== false) ? $filename . ".csv" : "export.csv";
header('Content-Type: text/csv; CHARSET=UTF-8');
header('Content-Disposition: attachment; filename=' . $name);
$output = fopen('php://output', 'w');
fwrite($output,chr(0xEF).chr(0xBB).chr(0xBF));
if(!empty($headings)){
fputcsv($output, $headings);
}
foreach($rows as $row){
fputcsv($output, $row);
}
exit();
}
return false;
}
/**
* 下载文件
*
* @access public
* @param mixed $filename 下载文件路径
* @param string $showname 下载显示的文件名
* @param int $expire 下载内容浏览器缓存时间
* @return mixed
*/
static public function download($filename, $showname='',$expire=1800)
{
if(file_exists($filename)&&is_file($filename))
{
$length = filesize($filename);
}
else{
die('下载文件不存在!');
}
$type = mime_content_type($filename);
//发送Http Header信息 开始下载
header("Pragma: public");
header("Cache-control: max-age=".$expire);
//header('Cache-Control: no-store, no-cache, must-revalidate');
header("Expires: " . gmdate("D, d M Y H:i:s",time()+$expire) . "GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s",time()) . "GMT");
header("Content-Disposition: attachment; filename=".$showname);
header("Content-Length: ".$length);
header("Content-type: ".$type);
header('Content-Encoding: none');
header("Content-Transfer-Encoding: binary" );
readfile($filename);
return true;
}
}
if( !function_exists ('mime_content_type')) {
/**
* 获取文件的mime_content类型
*
* @access global
* @param mixed $filename
* @return String
*/
function mime_content_type($filename)
{
static $contentType = array(
'ai' => 'application/postscript',
'aif' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'asc' => 'application/pgp', //changed by skwashd - was text/plain
'asf' => 'video/x-ms-asf',
'asx' => 'video/x-ms-asf',
'au' => 'audio/basic',
'avi' => 'video/x-msvideo',
'bcpio' => 'application/x-bcpio',
'bin' => 'application/octet-stream',
'bmp' => 'image/bmp',
'c' => 'text/plain', // or 'text/x-csrc', //added by skwashd
'cc' => 'text/plain', // or 'text/x-c++src', //added by skwashd
'cs' => 'text/plain', //added by skwashd - for C# src
'cpp' => 'text/x-c++src', //added by skwashd
'cxx' => 'text/x-c++src', //added by skwashd
'cdf' => 'application/x-netcdf',
'class' => 'application/octet-stream',//secure but application/java-class is correct
'com' => 'application/octet-stream',//added by skwashd
'cpio' => 'application/x-cpio',
'cpt' => 'application/mac-compactpro',
'csh' => 'application/x-csh',
'css' => 'text/css',
'csv' => 'text/comma-separated-values',//added by skwashd
'dcr' => 'application/x-director',
'diff' => 'text/diff',
'dir' => 'application/x-director',
'dll' => 'application/octet-stream',
'dms' => 'application/octet-stream',
'doc' => 'application/msword',
'dot' => 'application/msword',//added by skwashd
'dvi' => 'application/x-dvi',
'dxr' => 'application/x-director',
'eps' => 'application/postscript',
'etx' => 'text/x-setext',
'exe' => 'application/octet-stream',
'ez' => 'application/andrew-inset',
'gif' => 'image/gif',
'gtar' => 'application/x-gtar',
'gz' => 'application/x-gzip',
'h' => 'text/plain', // or 'text/x-chdr',//added by skwashd
'h++' => 'text/plain', // or 'text/x-c++hdr', //added by skwashd
'hh' => 'text/plain', // or 'text/x-c++hdr', //added by skwashd
'hpp' => 'text/plain', // or 'text/x-c++hdr', //added by skwashd
'hxx' => 'text/plain', // or 'text/x-c++hdr', //added by skwashd
'hdf' => 'application/x-hdf',
'hqx' => 'application/mac-binhex40',
'htm' => 'text/html',
'html' => 'text/html',
'ice' => 'x-conference/x-cooltalk',
'ics' => 'text/calendar',
'ief' => 'image/ief',
'ifb' => 'text/calendar',
'iges' => 'model/iges',
'igs' => 'model/iges',
'jar' => 'application/x-jar', //added by skwashd - alternative mime type
'java' => 'text/x-java-source', //added by skwashd
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'js' => 'application/x-javascript',
'kar' => 'audio/midi',
'latex' => 'application/x-latex',
'lha' => 'application/octet-stream',
'log' => 'text/plain',
'lzh' => 'application/octet-stream',
'm3u' => 'audio/x-mpegurl',
'man' => 'application/x-troff-man',
'me' => 'application/x-troff-me',
'mesh' => 'model/mesh',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'mif' => 'application/vnd.mif',
'mov' => 'video/quicktime',
'movie' => 'video/x-sgi-movie',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'mpe' => 'video/mpeg',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpga' => 'audio/mpeg',
'ms' => 'application/x-troff-ms',
'msh' => 'model/mesh',
'mxu' => 'video/vnd.mpegurl',
'nc' => 'application/x-netcdf',
'oda' => 'application/oda',
'patch' => 'text/diff',
'pbm' => 'image/x-portable-bitmap',
'pdb' => 'chemical/x-pdb',
'pdf' => 'application/pdf',
'pgm' => 'image/x-portable-graymap',
'pgn' => 'application/x-chess-pgn',
'pgp' => 'application/pgp',//added by skwashd
'php' => 'application/x-httpd-php',
'php3' => 'application/x-httpd-php3',
'pl' => 'application/x-perl',
'pm' => 'application/x-perl',
'png' => 'image/png',
'pnm' => 'image/x-portable-anymap',
'po' => 'text/plain',
'ppm' => 'image/x-portable-pixmap',
'ppt' => 'application/vnd.ms-powerpoint',
'ps' => 'application/postscript',
'qt' => 'video/quicktime',
'ra' => 'audio/x-realaudio',
'rar'=>'application/octet-stream',
'ram' => 'audio/x-pn-realaudio',
'ras' => 'image/x-cmu-raster',
'rgb' => 'image/x-rgb',
'rm' => 'audio/x-pn-realaudio',
'roff' => 'application/x-troff',
'rpm' => 'audio/x-pn-realaudio-plugin',
'rtf' => 'text/rtf',
'rtx' => 'text/richtext',
'sgm' => 'text/sgml',
'sgml' => 'text/sgml',
'sh' => 'application/x-sh',
'shar' => 'application/x-shar',
'shtml' => 'text/html',
'silo' => 'model/mesh',
'sit' => 'application/x-stuffit',
'skd' => 'application/x-koan',
'skm' => 'application/x-koan',
'skp' => 'application/x-koan',
'skt' => 'application/x-koan',
'smi' => 'application/smil',
'smil' => 'application/smil',
'snd' => 'audio/basic',
'so' => 'application/octet-stream',
'spl' => 'application/x-futuresplash',
'src' => 'application/x-wais-source',
'stc' => 'application/vnd.sun.xml.calc.template',
'std' => 'application/vnd.sun.xml.draw.template',
'sti' => 'application/vnd.sun.xml.impress.template',
'stw' => 'application/vnd.sun.xml.writer.template',
'sv4cpio' => 'application/x-sv4cpio',
'sv4crc' => 'application/x-sv4crc',
'swf' => 'application/x-shockwave-flash',
'sxc' => 'application/vnd.sun.xml.calc',
'sxd' => 'application/vnd.sun.xml.draw',
'sxg' => 'application/vnd.sun.xml.writer.global',
'sxi' => 'application/vnd.sun.xml.impress',
'sxm' => 'application/vnd.sun.xml.math',
'sxw' => 'application/vnd.sun.xml.writer',
't' => 'application/x-troff',
'tar' => 'application/x-tar',
'tcl' => 'application/x-tcl',
'tex' => 'application/x-tex',
'texi' => 'application/x-texinfo',
'texinfo' => 'application/x-texinfo',
'tgz' => 'application/x-gtar',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'tr' => 'application/x-troff',
'tsv' => 'text/tab-separated-values',
'txt' => 'text/plain',
'ustar' => 'application/x-ustar',
'vbs' => 'text/plain', //added by skwashd - for obvious reasons
'vcd' => 'application/x-cdlink',
'vcf' => 'text/x-vcard',
'vcs' => 'text/calendar',
'vfb' => 'text/calendar',
'vrml' => 'model/vrml',
'vsd' => 'application/vnd.visio',
'wav' => 'audio/x-wav',
'wax' => 'audio/x-ms-wax',
'wbmp' => 'image/vnd.wap.wbmp',
'wbxml' => 'application/vnd.wap.wbxml',
'wm' => 'video/x-ms-wm',
'wma' => 'audio/x-ms-wma',
'wmd' => 'application/x-ms-wmd',
'wml' => 'text/vnd.wap.wml',
'wmlc' => 'application/vnd.wap.wmlc',
'wmls' => 'text/vnd.wap.wmlscript',
'wmlsc' => 'application/vnd.wap.wmlscriptc',
'wmv' => 'video/x-ms-wmv',
'wmx' => 'video/x-ms-wmx',
'wmz' => 'application/x-ms-wmz',
'wrl' => 'model/vrml',
'wvx' => 'video/x-ms-wvx',
'xbm' => 'image/x-xbitmap',
'xht' => 'application/xhtml+xml',
'xhtml' => 'application/xhtml+xml',
'xls' => 'application/vnd.ms-excel',
'xlt' => 'application/vnd.ms-excel',
'xml' => 'application/xml',
'xpm' => 'image/x-xpixmap',
'xsl' => 'text/xml',
'xwd' => 'image/x-xwindowdump',
'xyz' => 'chemical/x-xyz',
'z' => 'application/x-compress',
'zip' => 'application/zip',
);
}
}