Dedehttpdown PHP Remote download Web page class, Enhanced edition 2013-1-17 _php tutorial

Source: Internet
Author: User
1) New remote host judgment, save server resources. Avoid the remote host does not exist when still fsockopen, resulting in the crash occupied the server CPU (2) added response 401 of the judgment and support. (3) Added support for JSON return text. (4) Log generation, if Debug_level is set and true, logs are made for each remote download. (5) Get the qualified datalimit of the byte, save the server resources. (6) Date Modified: 2013-1-17 If there is a better way to suggest, you can contact me at any time. admin@zbphp.com [PHP] M_url = $url; if (Is_array ($urls)) {$this->m_host = $urls ["Host"], if (!emptyempty ($urls ["scheme"]) {$this->m_scheme = $urls [" Scheme "]; } if (!emptyempty ($urls ["user"]) {$this->m_user = $urls ["User"];} if (!emptyempty ($urls ["Pass"]) {$this->m_ pass = $urls ["Pass"]; } if (!emptyempty ($urls ["Port"]) {$this->m_port = $urls ["Port"];} if (!emptyempty ($urls ["path"]) {$this->m_ Path = $urls ["Path"]; } $this->m_urlpath = $this->m_path; if (!emptyempty ($urls ["Query"])) {$this->m_query = $urls ["Query"]; $this->m_urlpath. = "?". $this->m_query; } $this->homeurl = $urls ["Host"]; $this->baseurlpath = $this->homeurl. $urls ["Path"]; $this->baseurlpath = Preg_replace ("/\/([^\/]*) \. (. *) $/","/", $this->baseurlpath); $this->baseurlpath = preg_replace ("/\/$/", "", $this->baseurlpath); }}/** * Reset Parameters * * @access public * @return void */function Resetany () {$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 = "";} /** * Open the specified URL * * @access public * @param string $url address * @param string $requestType Request type * @return String */function Ope Nurl ($url, $requestType = "GET") {$this->resetany (); $this->jumpcount = 0; $this->m_httphead = Array (); $this-& gt;m_html = "; $this->datalimit = 0; $this->retry = 0; $this->close (); Initialize the system $this->privateinit ($url); $this->privatestartsession ($requestType); }/** * Go to 303 redirect URL * * @access public * @param string $url address * @return String */function Jumpopenurl ($url) {$this->r Esetany (); $this->jumpcount++; $this->m_httphead = Array (); $this->m_html = ""; $this->close (); Initialize the system $this->privateinit ($url); $this->privatestartsession (' GET '); }/** * The reason for getting an operation error * * @access public * @return void */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 * * @access public * @return BOOL */function Isgetok () {if (Preg_match ("/^2/", $this- GetHead ("Http-state")) {return TRUE;} else {$this->m_error. = $this->gethead ("Http-state"). "-" $this->geth EAD ("Http-describe"). "
"; return FALSE; }}/** * see if the page returned is text type * * @access public * @return BOOL */function Istext () {if (Preg_match ("/^ (2|401)/", $this- GetHead ("Http-state")) && Preg_match ("/text|xml|json/i", $this->gethead ("Content-type")) {return TRUE;} else {$this->m_error. = "Content is non-text type or URL redirection
"; return FALSE; }}/** * Determines whether the returned Web page is of a specific type * * @access public * @param string $ctype Content Type * @return String */function Iscontenttype ($ctype {if (Preg_match ("/^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 file with HTTP protocol * * @access public * @param string $savefilename Save file name * @return String */function Savetobin ($save FileName) {if (! $this->isgetok ()) {return FALSE;} if (@feof ($this->m_fp)) {$this->m_error = "Connection closed! "; return FALSE; } $fp = fopen ($savefilename, "w"); while (!feof ($this->m_fp)) {fwrite ($fp, Fread ($this->m_fp, 1024x768));} fclose ($this->m_fp); Fclose ($FP); return TRUE; /** * Save the page content as text file * * @access public * @param string $savefilename Save file name * @return String */function Savetotext ($sav Efilename) {if ($this->istext ()) {$this->savebinfile ($savefilename);} else {return "";}} /** * Use the HTTP protocol to get the contents of a Web page * * @access public * @return String */function gethtml () {$tm 1 = microtime (true); if (! $this->i Stext ()) {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); if ($ This->datAlimit > 0 && strlen ($this->m_html) > $this->datalimit) break; } @fclose ($this->m_fp); $tm 2 = Microtime (true); $log = "\NTM2-TM1 =". ($tm 2-$TM 1); $log. = "\ n". $this->m_html; $this->log_write (' gethtml ', $log); return $this->m_html; }/** * Start http Session * * @access public * @param string $requestType Request type * @return String */function Privatestartsession ($re Questtype= "GET") {if (! $this->privateopenhost ()) {$this->m_error. = "Error opening remote host!"; return FALSE;} $this->retry+ +; if ($this->gethead ("http-edition") = = "http/1.1") {$HTTPV = "http/1.1";} else {$HTTPV = "http/1.0";} $ps = Explode ('? ') , $this->m_urlpath); $headString = "; Send a fixed start request header GET, host information if ($requestType = = "Get") {$headString. = "Get". $this->m_urlpath. "$HTTPV \ r \ n";} else {$headS Tring. = "POST". $ps [0]. "$HTTPV \ r \ n"; } if ($this->m_user | | $this->m_pass) {$headString. = "Authorization:basic". Base64_encode ($this->m_user. ":". $this->m_pass). " \ r \ n "; } $this->m_puthead["HosT "] = $this->m_host; Send user-defined request header if (!isset ($this->m_puthead["User-agent")) {$this->m_puthead["user-agent"] = "mozilla/4.0 ( Compatible MSIE 6.0; Windows NT 5.2) "; } if (!isset ($this->m_puthead["Refer"])) {$this->m_puthead["Refer"] = "http://". $this->m_puthead["Host"];} /* Add on 2012-12-19 */$headString. = "connection:keep-alive\r\n"; $headString. = "accept-language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3\r\n"; $headString. = "accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"; foreach ($this->m_puthead as $k + $v) {$k = Trim ($k), $v = Trim ($v), if ($k! = "" && $v! = "") {$headString. = "$k: $v \ r \ n "; }} fputs ($this->m_fp, $headString); if ($requestType = = "POST") {$postdata = ""; if (count ($ps) >1) {for ($i =1; $i M_FP, "content-type:application/x-www-form-urlencoded\r\n"); Fputs ($this->m_fp, "content-length: $plen \ r \ n"); }//Send a fixed end request header The//HTTP1.1 protocol must specify closing the link after the document is finished, otherwise you cannot use feof to determine the end if ($HTTPV = = "http/1.1") {fputs ($this->m_fp, ") when reading the document. Connection:close\r\n\r\n "); } else {fputs ($this->m_fp, "\ r \ n"),} if ($requestType = = "POST") {fputs ($this->m_fp, $postdata);}//Get Reply header status information $ Httpstas = Explode ("", Fgets ($this->m_fp,256)); $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]);} Gets the detail answer header while (!feof ($this->m_fp)) {$line = Trim (fgets ($this->m_fp,256)), if ($line = = "") {break;} $hkey = ""; $hvalue = ""; $v = 0; for ($i =0; $i M_httphead[strtolower ($hkey)] = Trim ($hvalue); }}//If the connection is not properly closed, retry if (feof ($this->m_fp)) {if ($this->retry >) {return FALSE;} $this->privatestartsession ($requestType); }//Determine if the answer is 3xx (Preg_match ("/^3/", $this->m_httphead["Http-state")) {if ($this->jumpcount > 3) {return;} if (Isset ($this->m_httphead[)) {$newurl = $this->m_httphead["Location"]; if (Preg_match ("/^http/i"), $newurl) {$this->jumpopenurl ($newurl),} else {$newurl = $this->fillurl ($newurl); $this->jumpopenurl ($ Newurl); }} else {$this->m_error = "Unrecognized reply! "; } } } /** * Gets the value of an HTTP header * * @access public * @param string $headname header file name * @return String */function GetHead ($headname) {$he Adname = Strtolower ($headname); return Isset ($this->m_httphead[$headname])? $this->m_httphead[$headname]: "; }/** * Sets the value of the HTTP header * * @access public * @param string $skey key * @param string $svalue value * @return String */function Sethea D ($skey, $svalue) {$this->m_puthead[$skey] = $svalue; /** * Open Connection * * @access public * @return BOOL */function Privateopenhost () {if ($this->m_host== "") {return FALSE;} if (function_exists (' CHECKDNSRR ') &&!checkdnsrr ($this->m_host, ' A ') &&!CHECKDNSRR ($this->m_ Host, ' CNAME ') {$this->m_error = ' remote host '. $this->m_host. ' does not exist!CHECKDNSRR! '; return FALSE;} $errno = ""; $errstr = ""; $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 * * @access public * @return void */function Close () {@fclose ($this->m_fp);}/** * Complete relative URL * * @access pub LIC * @param string $surl requires an incomplete address * @return String */function Fillurl ($surl) {$i = 0; $dstr = ""; $pstr = ""; $okurl = ""; $pathStep = 0; $surl = Trim ($surl); if ($surl = = "") {return "";} $pos = Strpos ($surl, "#"); if ($pos >0) {$surl = substr ($surl, 0, $pos),} if ($surl [0]== "/") {$okurl = "http://". $this->homeurl. $surl;}else if ($surl [0]== ".") {if (strlen ($surl) <=1) {return "";} else if ($surl [1]== "/") {$okurl = "http://". $this->baseurlpath. " /". substr ($surl, 2,strlen ($surl)-2); } else {$urls = explode ("/", $surl), foreach ($urls as $u) {if ($u = = "...") {$pathStep + +;} else if ($i Baseurlpath); if (count ($urls) <= $pathStep) {return "";} else {$pstr = "http://"; for ($i =0; $i Baseurlpath. " /". $surl; The Else if (Strtolower ($surl, 0,7)) = = "http://") {$okurl = $surl;} else {$okurl = "http://". $this->baseurlpath. " /". $surl; }} $okurl = Preg_replace ("/^ (http:\/\/)/I", "", $okurl); $okurl = Preg_replace ("/\/{1,}/", "/", $okurl); Return "http://" $OKURL; } function Log_write ($funcname, $message) {if (! ( Defined (' Debug_level ') && debug_level = = TRUE)) return; $log = "\ n". Date ("Y-m-d h:i:s"). Get_current_user (). " [". Getmypid ()."] "; $log. = "\ n". $this->m_url. " \ n ". Str_repeat ('------', 10)." \ n ". $message; $path = $funcname. ' '. Date (' Y m d H i s '). Preg_replace ('/([\w]+|\s+)/I ', ' ', $this->m_url); if (strlen ($path) >) $path = substr ($path, 0,250); $dir = Dededata. ' /httpdownlog '; if (!is_dir ($dir) &&!mkdir ($dir)) exit (' Can not make dir '. $dir); $path = $dir. '/'. $path; if (!file_exists ($path)) Touch ($path); $fp = fopen ($path, ' A + '); Flock ($FP, LOCK_EX); Fputs ($FP, "PATH:". $path. " \nreal: ". Realpath ($path)." \NMSSG: ". $log); Fclose ($FP); Return TRUE }}//end Class use method: [PHP] $DHD = new Dedehttpdown (); $DHD->openurl ($Rs [' Wurl ']); $DHD->datalimit = 5120; $DHD->m_puthead["Refer") = $Rs [' Wurl ']; $filecnt = Trim ($DHD->gethtml ()); Unresolved issues: (1) If the domain name is bound to a record and CNAME, there is IP pointing, but the IP address is nonexistent or false, the program will continue to obtain. (2) The time Out of PHP's fsockopen seems to have no effect at all. The 10s timeout is set, but it is executed until the program time is out. If a good way to suggest, you can contact me at any time. Admin@zbphp.com

http://www.bkjia.com/PHPjc/477811.html www.bkjia.com true http://www.bkjia.com/PHPjc/477811.html techarticle 1) New remote host judgment, save server resources. Avoid the remote host does not exist when still fsockopen, resulting in the death of the server CPU (2) added response 401 of the judgment and support ...

  • 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.