PHP remote download class share PHP remote debugging PHP Remote Call PHP connection remote Data

Source: Internet
Author: User
Tags ereg
Examples of this article for everyone to share the PHP remote download class, as follows

<?php/** * 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 = ""; /** * Initialize */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 (); }/** * Get the cause of an operation error */public function Printerror () {echo "error message:". $this->m_error; echo "Specific return header:
"; foreach ($this->m_httphead as $k = + $v) {echo "$k + $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 T Rue } else {$this->m_error. = $this->gethead ("Http-state"). "-". $this->gethead ("Http-describe"). "
"; return false; }}/** * see if the page returned is text type */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; }}/** * Determines whether the returned page is of a specific type */public function Iscontenttype ($ctype) {if (Ereg ("^2", $this->gethead ("Http-state")) && Amp $this->gethead ("content-type") = = Strtolower ($ctype)) {return true;} else {$this->m_error. = "wrong type". $this->ge THead ("Content-type"). "
"; return false; }}/** * Download the file using the HTTP protocol */public function Savetobin ($savefilename) {if (! $this->isgetok ()) return false; if (@feof ($t HIS-&GT;M_FP) {$this->m_error = "Connection is OFF!) "; 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 page content as Text file */public function Savetotext ($savefilename) {if ($this->istext ()) {$this->savebinfile ($sa Vefilename); } else {return "";}} /** * Use the HTTP protocol to get the contents of a Web page */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. = "Open Remote host Error! "; REturn false; if ($this->gethead ("http-edition") = = "http/1.1") {$HTTPV = "http/1.1";} else {$HTTPV = "http/1.0";} fputs ($this-&G T;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, otherwise you cannot use feof to determine the end if ($HTTPV = = "http/1.1") {fputs ($this->m_fp, "connection:close\r\n\r") when reading the document. \ 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 m_httphead["Http-describe"]. = "". Trim ($httpstas [$i]);} while (!feof ($this->m_fp)) {$line = Str_replace ("\" "," ", Trim (fgets ($this->m_fp,256))), and if ($line = =" ") break; if (Ereg (":", $line)) {$lines = Split (":", $line); $this->m_httphead[strtolower (Trim ($lines [0])] = Trim ($lines [1]); }}}/** * Gets 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: #打开网页 $httpdown = new Httpdownload (); $httpdown->openurl ("http://www.google.com.hk"); echo $httpdown->gethtml (); $httpdown-&GT;CLOSE (); #下载文件 $file = new Httpdownload (); # Instantiate class $file->openurl ("Http://dldir1.qq.com/qqfile/qq/QQ8.2/17724/QQ8.2.exe"); # Remote File address $file->savetobin ("Qq.exe"); # Save path and file name $file->close (); # Freeing Resources

The above is the whole content of this article, I hope that everyone's study has helped.

The above describes the PHP remote download class sharing, including PHP, remote aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.