Http.class.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * 洛阳赤炎鹰网络科技有限公司
  4. * https://www.cyyvip.com
  5. * Copyright (c) 2022 赤店商城 All rights reserved.
  6. */
  7. // +----------------------------------------------------------------------
  8. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  9. // +----------------------------------------------------------------------
  10. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  11. // +----------------------------------------------------------------------
  12. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  13. // +----------------------------------------------------------------------
  14. // | Author: liu21st <liu21st@gmail.com>
  15. // +----------------------------------------------------------------------
  16. namespace Org\Net;
  17. /**
  18. * Http 工具类
  19. * 提供一系列的Http方法
  20. * @author liu21st <liu21st@gmail.com>
  21. */
  22. class Http {
  23. /**
  24. * 采集远程文件
  25. * @access public
  26. * @param string $remote 远程文件名
  27. * @param string $local 本地保存文件名
  28. * @return mixed
  29. */
  30. static public function curlDownload($remote,$local) {
  31. $cp = curl_init($remote);
  32. $fp = fopen($local,"w");
  33. curl_setopt($cp, CURLOPT_FILE, $fp);
  34. curl_setopt($cp, CURLOPT_HEADER, 0);
  35. curl_exec($cp);
  36. curl_close($cp);
  37. fclose($fp);
  38. }
  39. /**
  40. * 使用 fsockopen 通过 HTTP 协议直接访问(采集)远程文件
  41. * 如果主机或服务器没有开启 CURL 扩展可考虑使用
  42. * fsockopen 比 CURL 稍慢,但性能稳定
  43. * @static
  44. * @access public
  45. * @param string $url 远程URL
  46. * @param array $conf 其他配置信息
  47. * int limit 分段读取字符个数
  48. * string post post的内容,字符串或数组,key=value&形式
  49. * string cookie 携带cookie访问,该参数是cookie内容
  50. * string ip 如果该参数传入,$url将不被使用,ip访问优先
  51. * int timeout 采集超时时间
  52. * bool block 是否阻塞访问,默认为true
  53. * @return mixed
  54. */
  55. static public function fsockopenDownload($url, $conf = array()) {
  56. $return = '';
  57. if(!is_array($conf)) return $return;
  58. $matches = parse_url($url);
  59. !isset($matches['host']) && $matches['host'] = '';
  60. !isset($matches['path']) && $matches['path'] = '';
  61. !isset($matches['query']) && $matches['query'] = '';
  62. !isset($matches['port']) && $matches['port'] = '';
  63. $host = $matches['host'];
  64. $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
  65. $port = !empty($matches['port']) ? $matches['port'] : 80;
  66. $conf_arr = array(
  67. 'limit' => 0,
  68. 'post' => '',
  69. 'cookie' => '',
  70. 'ip' => '',
  71. 'timeout' => 15,
  72. 'block' => TRUE,
  73. );
  74. foreach (array_merge($conf_arr, $conf) as $k=>$v) ${$k} = $v;
  75. if($post) {
  76. if(is_array($post))
  77. {
  78. $post = http_build_query($post);
  79. }
  80. $out = "POST $path HTTP/1.0\r\n";
  81. $out .= "Accept: */*\r\n";
  82. //$out .= "Referer: $boardurl\r\n";
  83. $out .= "Accept-Language: zh-cn\r\n";
  84. $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  85. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  86. $out .= "Host: $host\r\n";
  87. $out .= 'Content-Length: '.strlen($post)."\r\n";
  88. $out .= "Connection: Close\r\n";
  89. $out .= "Cache-Control: no-cache\r\n";
  90. $out .= "Cookie: $cookie\r\n\r\n";
  91. $out .= $post;
  92. } else {
  93. $out = "GET $path HTTP/1.0\r\n";
  94. $out .= "Accept: */*\r\n";
  95. //$out .= "Referer: $boardurl\r\n";
  96. $out .= "Accept-Language: zh-cn\r\n";
  97. $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  98. $out .= "Host: $host\r\n";
  99. $out .= "Connection: Close\r\n";
  100. $out .= "Cookie: $cookie\r\n\r\n";
  101. }
  102. $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
  103. if(!$fp) {
  104. return '';
  105. } else {
  106. stream_set_blocking($fp, $block);
  107. stream_set_timeout($fp, $timeout);
  108. @fwrite($fp, $out);
  109. $status = stream_get_meta_data($fp);
  110. if(!$status['timed_out']) {
  111. while (!feof($fp)) {
  112. if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) {
  113. break;
  114. }
  115. }
  116. $stop = false;
  117. while(!feof($fp) && !$stop) {
  118. $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
  119. $return .= $data;
  120. if($limit) {
  121. $limit -= strlen($data);
  122. $stop = $limit <= 0;
  123. }
  124. }
  125. }
  126. @fclose($fp);
  127. return $return;
  128. }
  129. }
  130. /**
  131. * 下载文件
  132. * 可以指定下载显示的文件名,并自动发送相应的Header信息
  133. * 如果指定了content参数,则下载该参数的内容
  134. * @static
  135. * @access public
  136. * @param string $filename 下载文件名
  137. * @param string $showname 下载显示的文件名
  138. * @param string $content 下载的内容
  139. * @param integer $expire 下载内容浏览器缓存时间
  140. * @return void
  141. */
  142. static public function download ($filename, $showname='',$content='',$expire=180) {
  143. if(is_file($filename)) {
  144. $length = filesize($filename);
  145. }elseif(is_file(UPLOAD_PATH.$filename)) {
  146. $filename = UPLOAD_PATH.$filename;
  147. $length = filesize($filename);
  148. }elseif($content != '') {
  149. $length = strlen($content);
  150. }else {
  151. E($filename.L('下载文件不存在!'));
  152. }
  153. if(empty($showname)) {
  154. $showname = $filename;
  155. }
  156. $showname = basename($showname);
  157. if(!empty($filename)) {
  158. $finfo = new \finfo(FILEINFO_MIME);
  159. $type = $finfo->file($filename);
  160. }else{
  161. $type = "application/octet-stream";
  162. }
  163. //发送Http Header信息 开始下载
  164. header("Pragma: public");
  165. header("Cache-control: max-age=".$expire);
  166. //header('Cache-Control: no-store, no-cache, must-revalidate');
  167. header("Expires: " . gmdate("D, d M Y H:i:s",time()+$expire) . "GMT");
  168. header("Last-Modified: " . gmdate("D, d M Y H:i:s",time()) . "GMT");
  169. header("Content-Disposition: attachment; filename=".$showname);
  170. header("Content-Length: ".$length);
  171. header("Content-type: ".$type);
  172. header('Content-Encoding: none');
  173. header("Content-Transfer-Encoding: binary" );
  174. if($content == '' ) {
  175. readfile($filename);
  176. }else {
  177. echo($content);
  178. }
  179. exit();
  180. }
  181. /**
  182. * 显示HTTP Header 信息
  183. * @return string
  184. */
  185. static function getHeaderInfo($header='',$echo=true) {
  186. ob_start();
  187. $headers = getallheaders();
  188. if(!empty($header)) {
  189. $info = $headers[$header];
  190. echo($header.':'.$info."\n"); ;
  191. }else {
  192. foreach($headers as $key=>$val) {
  193. echo("$key:$val\n");
  194. }
  195. }
  196. $output = ob_get_clean();
  197. if ($echo) {
  198. echo (nl2br($output));
  199. }else {
  200. return $output;
  201. }
  202. }
  203. /**
  204. * HTTP Protocol defined status codes
  205. * @param int $num
  206. */
  207. static function sendHttpStatus($code) {
  208. static $_status = array(
  209. // Informational 1xx
  210. 100 => 'Continue',
  211. 101 => 'Switching Protocols',
  212. // Success 2xx
  213. 200 => 'OK',
  214. 201 => 'Created',
  215. 202 => 'Accepted',
  216. 203 => 'Non-Authoritative Information',
  217. 204 => 'No Content',
  218. 205 => 'Reset Content',
  219. 206 => 'Partial Content',
  220. // Redirection 3xx
  221. 300 => 'Multiple Choices',
  222. 301 => 'Moved Permanently',
  223. 302 => 'Found', // 1.1
  224. 303 => 'See Other',
  225. 304 => 'Not Modified',
  226. 305 => 'Use Proxy',
  227. // 306 is deprecated but reserved
  228. 307 => 'Temporary Redirect',
  229. // Client Error 4xx
  230. 400 => 'Bad Request',
  231. 401 => 'Unauthorized',
  232. 402 => 'Payment Required',
  233. 403 => 'Forbidden',
  234. 404 => 'Not Found',
  235. 405 => 'Method Not Allowed',
  236. 406 => 'Not Acceptable',
  237. 407 => 'Proxy Authentication Required',
  238. 408 => 'Request Timeout',
  239. 409 => 'Conflict',
  240. 410 => 'Gone',
  241. 411 => 'Length Required',
  242. 412 => 'Precondition Failed',
  243. 413 => 'Request Entity Too Large',
  244. 414 => 'Request-URI Too Long',
  245. 415 => 'Unsupported Media Type',
  246. 416 => 'Requested Range Not Satisfiable',
  247. 417 => 'Expectation Failed',
  248. // Server Error 5xx
  249. 500 => 'Internal Server Error',
  250. 501 => 'Not Implemented',
  251. 502 => 'Bad Gateway',
  252. 503 => 'Service Unavailable',
  253. 504 => 'Gateway Timeout',
  254. 505 => 'HTTP Version Not Supported',
  255. 509 => 'Bandwidth Limit Exceeded'
  256. );
  257. if(isset($_status[$code])) {
  258. header('HTTP/1.1 '.$code.' '.$_status[$code]);
  259. }
  260. }
  261. }//类定义结束