This article describes an HTTP file download class that is excerpted from the Dream cms. Share to everyone for your reference. as follows:
<?php if (!defined (' Dedeinc ')) exit ("Request error!"); /** * Dream HTTP Download class * * @version $Id: dedehttpdown.class.php 1 11:42 July 6, 2010 z Tianya $ * @package dedecms.libraries *
@copyright Copyright (c) 2007-2010, Desdev, Inc.
* @license http://help.dedecms.com/usersguide/license.html * @link http://www.dedecms.com/@set_time_limit (0);
Class Dedehttpdown {var $m _url = ';
var $m _urlpath = ';
var $m _scheme = ' http ';
var $m _host = ';
var $m _port = ' 80 ';
var $m _user = ';
var $m _pass = ';
var $m _path = '/';
var $m _query = ';
var $m _fp = ';
var $m _error = ';
var $m _httphead = ';
var $m _html = ';
var $m _puthead = ';
var $BaseUrlPath = ';
var $HomeUrl = ';
var $reTry = 0;
var $JumpCount = 0;
/** * Initialization System * @access public * @param string $url address to be downloaded * @return string/function Privateinit ($url)
{if ($url = = ") {return;
} $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;
$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 Strin
G */function OpenURL ($url, $requestType = "Get") {$this->resetany ();
$this->jumpcount = 0;
$this->m_httphead = Array ();
$this->m_html = ';
$this->retry = 0;
$this->close ();
Initialization system $this->privateinit ($url);
$this->privatestartsession ($requestType); /** * Go to 303 redirect URL * * @access public * @param string $url address * @return string/function Jumpopenurl ($url
) {$this->resetany ();
$this->jumpcount++;
$this->m_httphead = Array ();
$this->m_html = "";
$this->close ();
Initialization system $this->privateinit ($url);
$this->privatestartsession (' get '); /** * Get the reason for an operation error * * @access public * @return void */function Printerror () {echo error message: ". $this->m_error;
echo "<br/> concrete return head:<br/>";
foreach ($this->m_httphead as $k => $v) {echo "$k => $v <br/>\r\n";} /** * To determine if the answer result of a header sent with a 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->gethead ("Http-describe"). "
<br/> ";
return FALSE; }/** * See if the returned page is a text type * * @access public * @return bool/function Istext () {if (Preg_match) ("/^2/", $this->gethead ("Http-state")) && Preg_match ("/text|xml/i", $this->gethead ("Content-type"))) {return T
RUE;
else {$this->m_error. = "Content is not text type or URL redirect <br/>";
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"). "
<br/> ";
return FALSE; /** * Download file with HTTP protocol * * @access public * @param string $savefilename Save file name * @return string/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");
while (!feof ($this->m_fp)) {fwrite ($fp, Fread ($this->m_fp, 1024));
} fclose ($this->m_fp);
Fclose ($FP);
return TRUE; /** * Save Web page content as text file * * @access public * @param string $savefilename Save file name * @return string/function
Savetotext ($savefilename) {if ($this->istext ()) {$this->savebinfile ($savefilename);
else {return ""; }
}
/**
* Use HTTP protocol to obtain the content of a Web page * * @access public * @return string/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 * * @access public * @param string $requestType Request type * @return string/function Privates
Tartsession ($requestType = "Get") {if (! $this->privateopenhost ()) {$this->m_error. = "Open remote host error!";
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 = ';
Sends a fixed start request header get, host information if ($requestType = = "Get") {$headString. = ' Get '. $this->m_urlpath. "$HTTPV \ r \ n";
} else {$headString. = "POST". $ps [0]. "$HTTPV \ r \ n";
} $this->m_puthead["Host" = $this->m_host;
Sends a user-defined request header if (!isset ($this->m_puthead["Accept"])) {$this->m_puthead["Accept"] = "*/*"; } 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"];
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 <count ($ps); $i + +) {$postdata. = $ps [$i];
} else {$postdata = "OK";
} $plen = strlen ($postdata);
Fputs ($this->m_fp, "content-type:application/x-www-form-urlencoded\r\n"); FPuts ($this->m_fp, "content-length: $plen \ r \ n"); The fixed end request Header//http1.1 protocol must specify that the link be closed after the end of the document, otherwise the feof can not be used when reading the document ($HTTPV = = "http/1.1") {fputs ($this->m_fp, "Conn
Ection:close\r\n\r\n ");
else {fputs ($this->m_fp, "\ r \ n");
} if ($requestType = = "POST") {fputs ($this->m_fp, $postdata);
//Get the response 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 <count ($httpstas), $i + +) {$this->m_httphead["http-describe"]. = "". Trim ($httpstas [$i]);
//Get the detailed answer head while (!feof ($this->m_fp)) {$line = Trim (fgets ($this->m_fp,256));
if ($line = = "") {break;
} $hkey = "";
$hvalue = "";
$v = 0;
For ($i =0 $i <strlen ($line); $i + +) {if ($v ==1) {$hvalue. = $line [$i];
} if ($line [$i]== ":") {$v = 1; } if ($v==0) {$hkey. = $line [$i];
}} $hkey = Trim ($hkey);
if ($hkey!= "") {$this->m_httphead[strtolower ($hkey)] = Trim ($hvalue);
}//If the connection is not shut down properly, 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["Location"])) {$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) {$headname = Strtolower ($headname); return ISSET ($this->m_httphead[$headname])?
$this->m_httphead[$headname]: '; /** * Set HTTP Header value * * @access public * @param string $skey key * @param string $svalue value * @return String *
/function Sethead ($skey, $svalue) {$this->m_puthead[$skey] = $svalue;
/** * Open Connection * * @access public * @return bool/function Privateopenhost () {if ($this->m_host== "") {
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;
}/** * Shut down connection * * @access public * @return void */function close () {@fclose ($this->m_fp);
/** * Complement relative URL * * @access public * @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 <count ($urls)-1) {$dstr. = $urls [$i].
/";
else {$dstr. = $urls [$i];
} $i + +;
$urls = Explode ("/", $this->baseurlpath);
if (count ($urls) <= $pathStep) {return "";
else {$pstr = "http://"; For ($i =0 $i <count ($urls)-$pathStep; $i + +) {$pstr. = $urls [$i]. "
/";
$okurl = $pstr. $dstr; }} else {if (strlen ($surl) <7) {$okurl = "http://". $this->baseurlpath."
/". $surl;
else if (Strtolower (substr ($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;
}}//end Class
I hope this article will help you with your PHP programming.