PHP download remote file class, support for the continuation of a breakpoint download, the code contains specific invocation instructions. The main program is to use the HTTP protocol to download files, HTTP1.1 protocol must be specified after the end of the document closed link, or read the document can not be used feof judgment end, there are two ways to use, specific download view source.
- /**
- * Download remote file class support breakpoint continuation
- */
- Class Httpdownload {
- Private $m _url = "";
- Private $m _urlpath = "";
- Private $m _scheme = "http";
- Private $m _host = "";
- Private $m _port = "80";
- Private $m _user = "";
- Private $m _pass = "";
- Private $m _path = "/";
- Private $m _query = "";
- Private $m _FP = "";
- Private $m _error = "";
- Private $m _httphead = "";
- Private $m _html = "";
- /**
- * Initialization
- */
- Public Function Privateinit ($url) {
- $urls = "";
- $urls = @parse_url ($url);
- $this->m_url = $url;
- if (Is_array ($urls)) {
- $this->m_host = $urls ["Host"];
- if (!empty ($urls ["scheme"]) $this->m_scheme = $urls ["scheme"];
- if (!empty ($urls ["User"]) $this->m_user = $urls ["User"];
- if (!empty ($urls ["Pass"]) $this->m_pass = $urls ["Pass"];
- if (!empty ($urls ["Port"]) $this->m_port = $urls ["Port"];
- if (!empty ($urls ["path"])) $this->m_path = $urls ["Path"];
- $this->m_urlpath = $this->m_path;
- if (!empty ($urls ["Query"])) {
- $this->m_query = $urls ["Query"];
- $this->m_urlpath. = "?". $this->m_query;
- }
- }
- }
- /**
- * Open the specified URL
- */
- function OpenUrl ($url) {
- #重设各参数
- $this->m_url = "";
- $this->m_urlpath = "";
- $this->m_scheme = "http";
- $this->m_host = "";
- $this->m_port = "80";
- $this->m_user = "";
- $this->m_pass = "";
- $this->m_path = "/";
- $this->m_query = "";
- $this->m_error = "";
- $this->m_httphead = "";
- $this->m_html = "";
- $this->close ();
- #初始化系统
- $this->privateinit ($url);
- $this->privatestartsession ();
- }
- /**
- * The reason for getting a wrong operation
- */
- Public Function Printerror () {
- echo "error message:". $this->m_error;
- echo "Specific return header:
";
- foreach ($this->m_httphead as $k = + $v) {
- echo "$k and $v
\ r \ n ";
- }
- }
- /**
- * Determine if the answer to the header sent with the Get method is correct
- */
- Public Function Isgetok () {
- if (Ereg ("^2", $this->gethead ("Http-state"))) {
- return true;
- } else {
- $this->m_error. = $this->gethead ("Http-state"). "-". $this->gethead ("Http-describe"). "
";
- return false;
- }
- }
- /**
- * See if the returned page is of type text
- */
- Public Function Istext () {
- if (Ereg ("^2", $this->gethead ("Http-state")) && eregi ("^text", $this->gethead ("Content-type"))) {
- return true;
- } else {
- $this->m_error. = "Content is non-text type
";
- return false;
- }
- }
- /**
- * Determine if the returned page is of a specific type
- */
- Public Function Iscontenttype ($ctype) {
- if (Ereg ("^2", $this->gethead ("Http-state")) && $this->gethead ("content-type") = = Strtolower ($ctype)) {
- return true;
- } else {
- $this->m_error. = "wrong type". $this->gethead ("Content-type"). "
";
- return false;
- }
- }
- /**
- * Download files with HTTP protocol
- */
- Public Function Savetobin ($savefilename) {
- if (! $this->isgetok ()) return false;
- if (@feof ($this->m_fp)) {
- $this->m_error = "The connection is closed! ";
- return false;
- }
- $fp = fopen ($savefilename, "w") or Die ("Write File $savefilename failed! ");
- while (!feof ($this->m_fp)) {
- @fwrite ($fp, fgets ($this->m_fp,256));
- }
- @fclose ($this->m_fp);
- return true;
- }
- /**
- * Save Web content as Text file
- */
- Public Function Savetotext ($savefilename) {
- if ($this->istext ()) {
- $this->savebinfile ($savefilename);
- } else {
- Return "";
- }
- }
- /**
- * Get the content of a webpage using the HTTP protocol
- */
- Public Function gethtml () {
- if (! $this->istext ()) return "";
- if ($this->m_html!= "") return $this->m_html;
- if (! $this->m_fp| | @feof ($this->m_fp)) return "";
- while (!feof ($this->m_fp)) {
- $this->m_html. = fgets ($this->m_fp,256);
- }
- @fclose ($this->m_fp);
- return $this->m_html;
- }
- /**
- * Start HTTP session
- */
- Public Function privatestartsession () {
- if (! $this->privateopenhost ()) {
- $this->m_error. = "Error opening remote host!";
- return false;
- }
- if ($this->gethead ("http-edition") = = "http/1.1") {
- $HTTPV = "http/1.1";
- } else {
- $HTTPV = "http/1.0";
- }
- Fputs ($this->m_fp, "GET". $this->m_urlpath. "$HTTPV \ r \ n");
- Fputs ($this->m_fp, "Host:". $this->m_host. " \ r \ n ");
- Fputs ($this->m_fp, "Accept: */*\r\n");
- Fputs ($this->m_fp, "user-agent:mozilla/4.0+ (compatible;+msie+6.0;+windows+nt+5.2) \ r \ n");
- #HTTP1.1 The protocol must close the link after the document is closed, or the document cannot be read with feof judgment end
- if ($HTTPV = = "http/1.1") {
- Fputs ($this->m_fp, "connection:close\r\n\r\n");
- } else {
- Fputs ($this->m_fp, "\ r \ n");
- }
- $httpstas = fgets ($this->m_fp,256);
- $httpstas = Split ("", $httpstas);
- $this->m_httphead["http-edition") = Trim ($httpstas [0]);
- $this->m_httphead["http-state") = Trim ($httpstas [1]);
- $this->m_httphead["http-describe"] = "";
- for ($i =2; $i
- $this->m_httphead["Http-describe". "". Trim ($httpstas [$i]);
- }
- while (!feof ($this->m_fp)) {
- $line = Str_replace ("\" "," ", Trim (fgets ($this->m_fp,256)));
- if ($line = = "") break;
- if (Ereg (":", $line)) {
- $lines = Split (":", $line);
- $this->m_httphead[strtolower (Trim ($lines [0])] = Trim ($lines [1]);
- }
- }
- }
- /**
- * Get the value of an HTTP header
- */
- Public Function GetHead ($headname) {
- $headname = Strtolower ($headname);
- if (Isset ($this->m_httphead[$headname])) {
- return $this->m_httphead[$headname];
- } else {
- Return "";
- }
- }
- /**
- * Open Connection
- */
- Public Function Privateopenhost () {
- if ($this->m_host== "") return false;
- $this->m_fp = @fsockopen ($this->m_host, $this->m_port, & $errno, & $errstr, 10);
- if (! $this->m_fp) {
- $this->m_error = $errstr;
- return false;
- } else {
- return true;
- }
- }
- /**
- * Close Connection
- */
- Public Function Close () {
- @fclose ($this->m_fp);
- }
- }
- #两种使用方法, respectively, as follows:
- #打开网页
- $httpdown = new Httpdownload ();
- $httpdown->openurl ("http://www.google.com.hk");
- echo $httpdown->gethtml ();
- $httpdown->close ();
- #下载文件
- $file = new Httpdownload (); # Instantiating classes
- $file->openurl ("http://www.ti.com.cn/cn/lit/an/rust020/rust020.pdf"); # Remote File Address
- $file->savetobin ("rust020.pdf"); # Save path and file name
- $file->close (); # Freeing Resources
- ?>
Copy Code |