Publish an HTTP download class written with PHPfsockopen. if you can enable the remote content option, php can use fopen or file_get_contents to obtain the content of a webpage, however, the default function cannot obtain the HTTP header, which is inconvenient in some special applications. for example, there is a link:
Http://www.abc.com/showvd.asp? Id = 18
If it returns an image, it is difficult to identify it using the default function, but it is much easier to identify it using the HTTP response header. In addition, if the other party uses Refer for anti-Leech protection, it cannot be obtained, and the HTTP class can solve these problems perfectly, and the speed is almost the same.
Usage:
$ Hd = new DedeHttpDown ();
$ Hd-> OpenUrl ("http://www.dedecms.com ");
Echo $ hd-> GetHtml ();
// If saved as an object, use $ hd-> SaveBin ("dede.html ");
$ Hd-> Close ();
Get http request headers
$ Hd-> GetHead ("key ")
Set request header
$ Hd-> SetHead (key, value); (must be set before calling OpenUrl)
The code is as follows:
/*---------------------------------------------------------------------
// Zhimeng Http download class V1.0
// From: dream travel http://www.dedecms.com
// Author: IT Plato
// Time:
// Statement
---------------------------------------------------------------------*/
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 $ JumpCount = 0; // prevent multiple redirection from getting into an endless loop
//
// Initialize the system
//
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"];
// Send fixed start request header GET and Host information
Fputs ($ this-> m_fp, "GET". $ this-> m_urlpath. "$ httpv \ r \ n ");
$ This-> m_puthead ["Host"] = $ this-> m_host;
// Send custom request headers
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! = ""){
Fputs ($ this-> m_fp, "$ k: $ v \ r \ n ");
}
}
// Send a fixed end request header
// In HTTP1.1, the link must be closed after the document ends. otherwise, feof cannot be used to judge the end of the document.
If ($ httpv = "HTTP/1.1") fputs ($ this-> m_fp, "Connection: Close \ r \ n ");
Else fputs ($ this-> m_fp, "\ r \ n ");
// Obtain the response header status
$ 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 $ This-> m_httphead ["http-describe"]. = "". trim ($ httpstas [$ I]);
}
// Obtain the detailed response header
While (! Feof ($ this-> m_fp )){
$ Line = trim (fgets ($ this-> m_fp, 256 ));
If ($ line = "") break;
$ Hkey = "";
$ Hvalue = "";
$ V = 0;
For ($ I = 0; $ 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 );
}
// Determine whether the response starts with 3xx
If (ereg ("^ 3", $ this-> m_httphead ["http-state"])
{
If ($ this-> JumpCount> 3) return;
If (isset ($ this-> m_httphead ["location"]) {
$ Newurl = $ this-> m_httphead ["location"];
If (eregi ("^ http", $ newurl )){
$ This-> JumpOpenUrl ($ newurl );
}
Else {
$ Newurl = $ this-> FillUrl ($ newurl );
$ This-> JumpOpenUrl ($ newurl );
}
}
Else
{$ This-> m_error = "unrecognized transfer response! ";}
}//
}
//
// Obtain the value of an Http header
//
Function GetHead ($ headname)
{
$ Headname = strtolower ($ headname );
If (isset ($ this-> m_httphead [$ headname])
Return $ this-> m_httphead [$ headname];
Else
Return "";
}
//
// Set the Http header value
//
Function SetHead ($ skey, $ svalue)
{
$ This-> m_puthead [$ skey] = $ svalue;
}
//
// Open the connection
//
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 the connection
//
Function Close (){
@ Fclose ($ this-> m_fp );
}
//
// Complete the relative URL
//
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) <= 2) return "";
Else if ($ surl [0] = "/")
{
$ Okurl = "http: //". $ this-> BaseUrlPath. "/". substr ($ surl, 2, strlen ($ surl)-2 );
}
Else {
$ Urls = explode ("/", $ surl );
Foreach ($ urls as $ u ){
If ($ u = "...") $ pathStep ++;
Else if ($ I Else $ dstr. = $ urls [$ I];
$ I ++;
}
$ Urls = explode ("/", $ this-> BaseUrlPath );
If (count ($ urls) <= $ pathStep)
Return "";
Else {
$ Pstr = "http ://";
For ($ I = 0; $ 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 = eregi_replace ("^ (http: //)", "", $ okurl );
$ Okurl = eregi_replace ("/{1,}", "/", $ okurl );
Return "http: //". $ okurl;
}
}
?>
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.