An HTTP download class written in PHP

Source: Internet
Author: User
Tags ereg

An HTTP download class written in PHP

 

In PHP, a common method for downloading HTTP resources is to use the file_get_contents function. However, this function cannot obtain the HTTP header information, which may cause inconvenience to some applications, therefore, I wrote an HTTP download class to solve this problem. The test speed is almost the same as that of file_get_contents.

<?
/* ===================================================== =
// Zhimeng HTTP download class
// Www.dedecms.com
======================================================= */
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 = "";
//
// Initialize the system
//
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)
{
// 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-> m_httphead = "";
$ 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: <br> ";
Foreach ($ this-> m_httphead as $ k => $ V)
{Echo "$ k = >$ v <br>/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 "). "<br> ";
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 not of the text type <br> ";
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. = "type incorrect". $ this-> gethead ("Content-Type"). "<br> ";
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") or die ("failed to write the file $ savefilename! ");
While (! Feof ($ this-> m_fp )){
@ Fwrite ($ FP, fgets ($ this-> m_fp, 256 ));
}
@ Fclose ($ this-> m_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 ";
Fputs ($ this-> 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 ");
// 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/R/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 <count ($ httpstas); $ I ++ ){
$ This-> m_httphead ["http-describe"]. = "". Trim ($ httpstas [$ I]);
}
While (! Feof ($ this-> m_fp )){
$ Line = str_replace ("/" "," ", trim (fgets ($ this-> m_fp, 256 )));
If ($ line = "") break;
If (ereg (":", $ line )){
$ Lines = Split (":", $ line );
$ This-> m_httphead [strtolower (TRIM ($ lines [0])] = trim ($ lines [1]);
}
}
}
//
// 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 "";
}
//
// 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 );
}
}

?>

Usage of this class:

Download webpage
<?
$ Httpdown = new dedehttpdown ();
$ Httpdown-> Openurl ("http://www.dedecms.com ");
Echo $ httpdown-> gethtml ();
$ Httpdown-> close ();
?>
If you download and save the image, you can use
<?
$ Httpdown = new dedehttpdown ();
$ Httpdown-> Openurl ("http://prato.bokele.com/0/0/399/bGluMi5qcGc=.jpg ");
Echo $ httpdown-> savebin ("test.jpg ");
$ Httpdown-> close ();
Echo " ";
?>

 

 

Reprint address: http://prato.bokele.com /? ArticleID = 19533

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.