Publish an HTTP download class written with PHPfsockopen _ PHP Tutorial

Source: Internet
Author: User
Tags ereg
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 be used to obtain the content of a web page. if you can enable the remote content option, php can use fopen or file_get_contents to obtain the content of a web page, 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"];

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 = ereg_replace ("/([^/] *) \. (. *) $", "/", $ this-> BaseUrlPath );
$ This-> BaseUrlPath = ereg_replace ("/$", "", $ this-> BaseUrlPath );
}
}
//
// Open the specified URL
//
Function OpenUrl ($ url)
{
// Reset parameters
$ 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-> JumpCount = 0;
$ This-> m_httphead = Array ();
// $ This-> m_puthead = "";
$ This-> m_html = "";
$ This-> Close ();
// Initialize the system
$ This-> PrivateInit ($ url );
$ This-> PrivateStartSession ();
}
//
// Open the 303 redirection URL
//
Function JumpOpenUrl ($ url)
{
// Reset parameters
$ 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-> JumpCount ++;
$ This-> m_httphead = Array ();
$ This-> m_html = "";
$ This-> Close ();
// Initialize the system
$ This-> PrivateInit ($ url );
$ This-> PrivateStartSession ();
}
//
// Obtain the cause of an operation error
//
Function printError ()
{
Echo "error message:". $ this-> m_error;
Echo "specific return headers:
";
Foreach ($ this-> m_httphead as $ k => $ v)
{Echo "$ k => $ v
\ R \ n ";}
}
//
// Determine whether the response results of the headers sent using the Get method are correct
//
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;
}
}
//
// Check whether the returned webpage is of the text type.
//
Function IsText ()
{
If (ereg ("^ 2", $ this-> GetHead ("http-state "))
& Eregi ("^ text", $ this-> GetHead ("content-type ")))
{Return true ;}
Else
{
$ This-> m_error. = "The content is non-text or URL redirection.
";
Return false;
}
}
//
// Determine whether the returned webpage is of a specific type
//
Function IsContentType ($ ctype)
{
If (ereg ("^ 2", $ this-> GetHead ("http-state "))
& $ This-> GetHead ("content-type") = strtolower ($ ctype ))
{Return true ;}
Else
{
$ This-> m_error. = "The type is incorrect". $ this-> GetHead ("content-type ")."
";
Return false;
}
}
//
// Download an object over Http
//
Function SaveToBin ($ savefilename)
{
If (! $ This-> IsGetOK () return false;
If (@ feof ($ this-> m_fp) {$ this-> m_error = "The connection has been 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 the webpage content as a Text file
//
Function SaveToText ($ savefilename)
{
If ($ this-> IsText () $ this-> SaveBinFile ($ savefilename );
Else return "";
}
//
// Obtain the content of a webpage through Http
//
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 an HTTP session
//
Function PrivateStartSession ()
{

If (! $ This-> PrivateOpenHost ()){
$ This-> m_error. = "an error occurred while opening the remote host! ";
Return false;
}

If ($ this-> GetHead ("http-edition") = "HTTP/1.1") $ httpv = "HTTP/1.1 ";
Else $ httpv = "HTTP/1.0 ";

// 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;
}
}
?>

...

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.