Http.class.php 8.9 KB

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