You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
7.1 KiB

  1. <?php
  2. namespace App\Common\Utils;
  3. /**
  4. * 下载远程文件类支持断点续传
  5. */
  6. class HttpDownload
  7. {
  8. private $m_url = "";
  9. private $m_urlpath = "";
  10. private $m_scheme = "http";
  11. private $m_host = "";
  12. private $m_port = "80";
  13. private $m_user = "";
  14. private $m_pass = "";
  15. private $m_path = "/";
  16. private $m_query = "";
  17. private $m_fp = "";
  18. private $m_error = "";
  19. private $m_httphead = "";
  20. private $m_html = "";
  21. /**
  22. * 初始化
  23. */
  24. public function PrivateInit($url)
  25. {
  26. $urls = "";
  27. $urls = @parse_url($url);
  28. $this->m_url = $url;
  29. if (is_array($urls)) {
  30. $this->m_host = $urls["host"];
  31. if (!empty($urls["scheme"])) $this->m_scheme = $urls["scheme"];
  32. if (!empty($urls["user"])) $this->m_user = $urls["user"];
  33. if (!empty($urls["pass"])) $this->m_pass = $urls["pass"];
  34. if (!empty($urls["port"])) $this->m_port = $urls["port"];
  35. if (!empty($urls["path"])) $this->m_path = $urls["path"];
  36. $this->m_urlpath = $this->m_path;
  37. if (!empty($urls["query"])) {
  38. $this->m_query = $urls["query"];
  39. $this->m_urlpath .= "?" . $this->m_query;
  40. }
  41. }
  42. }
  43. /**
  44. * 打开指定网址
  45. */
  46. function OpenUrl($url)
  47. {
  48. #重设各参数
  49. $this->m_url = "";
  50. $this->m_urlpath = "";
  51. $this->m_scheme = "http";
  52. $this->m_host = "";
  53. $this->m_port = "80";
  54. $this->m_user = "";
  55. $this->m_pass = "";
  56. $this->m_path = "/";
  57. $this->m_query = "";
  58. $this->m_error = "";
  59. $this->m_httphead = "";
  60. $this->m_html = "";
  61. $this->Close();
  62. #初始化系统
  63. $this->PrivateInit($url);
  64. $this->PrivateStartSession();
  65. }
  66. /**
  67. * 获得某操作错误的原因
  68. */
  69. public function printError()
  70. {
  71. echo "错误信息:" . $this->m_error;
  72. echo "具体返回头:<br>";
  73. foreach ($this->m_httphead as $k => $v) {
  74. echo "$k => $v <br>\r\n";
  75. }
  76. }
  77. /**
  78. * 判别用Get方法发送的头的应答结果是否正确
  79. */
  80. public function IsGetOK()
  81. {
  82. if (ereg("^2", $this->GetHead("http-state"))) {
  83. return true;
  84. } else {
  85. $this->m_error .= $this->GetHead("http-state") . " - " . $this->GetHead("http-describe") . "<br>";
  86. return false;
  87. }
  88. }
  89. /**
  90. * 看看返回的网页是否是text类型
  91. */
  92. public function IsText()
  93. {
  94. if (ereg("^2", $this->GetHead("http-state")) && eregi("^text", $this->GetHead("content-type"))) {
  95. return true;
  96. } else {
  97. $this->m_error .= "内容为非文本类型<br>";
  98. return false;
  99. }
  100. }
  101. /**
  102. * 判断返回的网页是否是特定的类型
  103. */
  104. public function IsContentType($ctype)
  105. {
  106. if (ereg("^2", $this->GetHead("http-state")) && $this->GetHead("content-type") == strtolower($ctype)) {
  107. return true;
  108. } else {
  109. $this->m_error .= "类型不对 " . $this->GetHead("content-type") . "<br>";
  110. return false;
  111. }
  112. }
  113. /**
  114. * HTTP 协议下载文件
  115. */
  116. public function SaveToBin($savefilename)
  117. {
  118. if (!$this->IsGetOK()) return false;
  119. if (@feof($this->m_fp)) {
  120. $this->m_error = "连接已经关闭!";
  121. return false;
  122. }
  123. $fp = fopen($savefilename, "w") or die("写入文件 $savefilename 失败!");
  124. while (!feof($this->m_fp)) {
  125. @fwrite($fp, fgets($this->m_fp, 256));
  126. }
  127. @fclose($this->m_fp);
  128. return true;
  129. }
  130. /**
  131. * 保存网页内容为 Text 文件
  132. */
  133. public function SaveToText($savefilename)
  134. {
  135. if ($this->IsText()) {
  136. $this->SaveBinFile($savefilename);
  137. } else {
  138. return "";
  139. }
  140. }
  141. /**
  142. * HTTP 协议获得一个网页的内容
  143. */
  144. public function GetHtml()
  145. {
  146. if (!$this->IsText()) return "";
  147. if ($this->m_html != "") return $this->m_html;
  148. if (!$this->m_fp || @feof($this->m_fp)) return "";
  149. while (!feof($this->m_fp)) {
  150. $this->m_html .= fgets($this->m_fp, 256);
  151. }
  152. @fclose($this->m_fp);
  153. return $this->m_html;
  154. }
  155. /**
  156. * 开始 HTTP 会话
  157. */
  158. public function PrivateStartSession()
  159. {
  160. if (!$this->PrivateOpenHost()) {
  161. $this->m_error .= "打开远程主机出错!";
  162. return false;
  163. }
  164. if ($this->GetHead("http-edition") == "HTTP/1.1") {
  165. $httpv = "HTTP/1.1";
  166. } else {
  167. $httpv = "HTTP/1.0";
  168. }
  169. fputs($this->m_fp, "GET " . $this->m_urlpath . " $httpv\r\n");
  170. fputs($this->m_fp, "Host: " . $this->m_host . "\r\n");
  171. fputs($this->m_fp, "Accept: */*\r\n");
  172. fputs($this->m_fp, "User-Agent: Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.2)\r\n");
  173. #HTTP1.1协议必须指定文档结束后关闭链接,否则读取文档时无法使用feof判断结束
  174. if ($httpv == "HTTP/1.1") {
  175. fputs($this->m_fp, "Connection: Close\r\n\r\n");
  176. } else {
  177. fputs($this->m_fp, "\r\n");
  178. }
  179. $httpstas = fgets($this->m_fp, 256);
  180. $httpstas = split(" ", $httpstas);
  181. $this->m_httphead["http-edition"] = trim($httpstas[0]);
  182. $this->m_httphead["http-state"] = trim($httpstas[1]);
  183. $this->m_httphead["http-describe"] = "";
  184. for ($i = 2; $i < count($httpstas); $i++) {
  185. $this->m_httphead["http-describe"] .= " " . trim($httpstas[$i]);
  186. }
  187. while (!feof($this->m_fp)) {
  188. $line = str_replace("\"", "", trim(fgets($this->m_fp, 256)));
  189. if ($line == "") break;
  190. if (ereg(":", $line)) {
  191. $lines = split(":", $line);
  192. $this->m_httphead[strtolower(trim($lines[0]))] = trim($lines[1]);
  193. }
  194. }
  195. }
  196. /**
  197. * 获得一个Http头的值
  198. */
  199. public function GetHead($headname)
  200. {
  201. $headname = strtolower($headname);
  202. if (isset($this->m_httphead[$headname])) {
  203. return $this->m_httphead[$headname];
  204. } else {
  205. return "";
  206. }
  207. }
  208. /**
  209. * 打开连接
  210. */
  211. public function PrivateOpenHost()
  212. {
  213. if ($this->m_host == "") return false;
  214. $this->m_fp = @fsockopen($this->m_host, $this->m_port, $errno, $errstr, 10);
  215. if (!$this->m_fp) {
  216. $this->m_error = $errstr;
  217. return false;
  218. } else {
  219. return true;
  220. }
  221. }
  222. /**
  223. * 关闭连接
  224. */
  225. public function Close()
  226. {
  227. @fclose($this->m_fp);
  228. }
  229. }
  230. /*
  231. #两种使用方法,分别如下:
  232. #打开网页
  233. $httpdown = new HttpDownload();
  234. $httpdown->OpenUrl("http://www.google.com.hk");
  235. echo $httpdown->GetHtml();
  236. $httpdown->Close();
  237. #下载文件
  238. $file = new HttpDownload(); # 实例化类
  239. $file->OpenUrl("http://www.ti.com.cn/cn/lit/an/rust020/rust020.pdf"); # 远程文件地址
  240. $file->SaveToBin("rust020.pdf"); # 保存路径及文件名
  241. $file->Close();
  242. */