php远程下载类分享

本文实例为大家分享了php远程下载类,如下:

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