Example of using PHP to implement proxy function [upgraded to v0.0.2]

Source: Internet
Author: User

Introduction: This is a demonstration of using PHP to implement the proxy function [upgraded to v0.0.2]. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 326940 'rolling = 'no'>

The work of the trainer just written. I feel that there is still some practical value .. Let's try it out.

Function: Implement proxy through PHP

Applicable users: Download at home is slow. You are too lazy to find the proxy. There are high-speed hosts and PHP hosts, and the host supports socket.

Usage:

1. CopyProgramUpload to host

2. Browse this file on the host in a browser

3. Enter the address of the file to be downloaded in the URL (if necessary, enter Referer)

4. Click get proxy URL

5. The link on the new page is the link downloaded through proxy.

Note:

1. Supports multi-threaded download and resumable upload

2. Only HTTP protocol is supported

3. Server-side header redirection not supported

4. HTTP auth not supported

5. When downloading a large file, you can modify the value of set_time_limit in the program to reduce the disconnection phenomenon (default value: 180, that is, 3 minutes)

PHPCode:--------------------------------------------------------------------------------

<? PHP

/***************************************/

/**/

/* Php http Proxy */

/* Version: 0.0.2 */

/* Last modify: 2005.1.12 */

/* Author: q3boy <q3boy@sina.com> */

/**/

/***************************************/

Set_time_limit (180 );

Define ('pp _ error_code ', 1 );

Define ('pp _ error_message ', 2 );

Define ('pp _ error_all ', 3 );

Define ('pp _ return_string ', 1 );

Define ('pp _ return_array ', 2 );

Define ('pp _ return_all ', 3 );

Class cphpproxy {

VaR $ _ strurl;

VaR $ _ arrargv;

VaR $ _ strref;

VaR $ _ arrurl;

VaR $ _ ressocket;

VaR $ _ interror;

VaR $ _ strerror;

VaR $ _ arrdefaultarrayurl;

VaR $ _ arrdefaultport;

Function cphpproxy ($ strurl = NULL, $ arrargv = array ()){

$ This-> Init ();

Is_null ($ strurl )? ': $ This-> seturl ($ strurl );

Is_null ($ arrargv ['Referer'])? ': $ This-> SETREF ($ arrargv ['Referer']);

$ This-> proxy ($ strurl, $ arrargv );

}

Function seterror ($ interror, $ mixargv = NULL ){

$ This-> _ interror = $ interror;

If ($ interror = 1001 ){

$ This-> _ strerror = "An error occurred while calling the $ mixargv method. The enumerated value does not exist ";

} Elseif ($ interror = 1 ){

$ This-> _ strerror = "url error, URL format incorrect. \ n". $ mixargvs;

} Elseif ($ interror = 2 ){

$ This-> _ strerror = "url error. This protocol is not supported currently. \ n". $ mixargvs;

} Elseif ($ interror = 3 ){

$ This-> _ strerror = "cannot open ". $ this-> _ arrurl ['host']. ':'. $ this-> _ arrurl ['Port']. "\ n ". $ mixargv [0]. ':'. $ mixargv [1];

}

Return false;

}

Function geterror ($ enumreturntype = pp_error_code ){

If ($ enumreturntype = pp_error_all ){

Return array (str_pad ($ this-> _ interror, 5, '0', str_pad_left), $ this-> _ strerror );

} Elseif ($ enumreturntype = pp_error_message ){

Return $ this-> _ strerror;

} Elseif ($ enumreturntype = pp_error_code ){

Return $ this-> _ interror;

} Else {

Return false;

}

}

Function Init (){

$ This-> _ strurl = NULL;

$ This-> _ arrargv = array ();

$ This-> _ strref = NULL;

$ This-> _ arrurl = array ();

$ This-> _ ressocket = NULL;

$ This-> _ interror = 0;

$ This-> _ strerror = 'there is no error ';

// Arrurl Default Value

$ This-> _ arrdefaultarrayurl = array ('protocol' => 'http', 'file' => 'index.htm ');

// Default port

$ This-> _ arrdefaultport = array ('HTTP '=> '80 ');

Return true;

}

Function seturl ($ strurl ){

$ Strurl = trim ($ strurl );

// Regular

$ Reg = "/^ ([\ W] + ):\/\/)? ([^: | \/| @] * :)? ([^: | \/| @] * @)? ([\ W | \-| \.] +) (: \ D + )? (\/[^ \? | #] *)? (\? [^ #] *)? (#.*)? $/Is ";

If (! Preg_match ($ Reg, $ strurl, $ arrurl )){

Return $ this-> seterror (1, $ strurl );

} Else {

// Disassemble matching Array

List ($ TMP, $ TMP, $ arr ['protocol'], $ arr ['user'], $ arr ['pass'], $ arr ['host'], $ arr ['Port'], $ arr ['path'], $ arr ['query'], $ arr ['anchor ']) = $ arrurl;

// Default Protocol

If (! $ Arr ['protocol']) {

$ Arrurl [1] = $ this-> _ arrdefaultarrayurl ['protocol']. ': //';

}

// Set filename

$ Arr ['file'] = basename ($ arr ['path']);

// Default Value

Foreach ($ this-> _ arrdefaultarrayurl as $ key => $ Val ){

If ($ arr [$ key] = '){

$ Arr [$ key] = $ val;

}

}

// Default port

If (is_null ($ this-> _ arrdefaultport [$ arr ['protocol']) {

Return $ this-> seterror (2, $ arr ['protocol']);

} Elseif (! $ Arr ['Port']) {

$ Arr ['Port'] = $ this-> _ arrdefaultport [$ arr ['protocol'];

}

// Set URI

$ Arr ['uri '] = ($ arr ['path']? $ Arr ['path']: '/'). ($ arr ['query']? '? '. $ Arr ['query']:'). ($ arr ['anchor ']? '#'. $ Arr ['anchor ']:');

// Set the URL

Unset ($ arrurl [0]);

Unset ($ arrurl [2]);

$ This-> _ strurl = implode (', $ arrurl );

// Set arrurl

$ This-> _ arrurl = $ arr;

Return true;

}

}

Function geturl ($ enumreturntype = pp_return_array ){

If ($ enumreturntype = pp_return_string ){

Return $ this-> _ strurl;

} Elseif ($ enumreturntype = pp_return_array ){

Return $ this-> _ arrurl;

} If ($ enumreturntype = pp_return_all ){

Return array ($ this-> _ strurl, $ this-> _ arrurl );

} Else {

Return $ this-> seterror (1001 );

}

}

Function setrefer ($ strref ){

$ This-> _ strref = trim ($ strref );

Return true;

}

Function getrefer (){

Return $ this-> _ strref;

}

Function getproxyurl (){

Global $ _ server;

List ($ strprocotol) = explode ('/', strtolower (TRIM ($ _ server ['server _ Protocol']);

$ STR = $ strprocotol. ': //'. $ _ server ['HTTP _ host']. $ _ server ['php _ Self '].

'? U = '. urlencode ($ this-> geturl (pp_return_string )).

($ This-> getrefer ()? ('& R ='. urlencode ($ this-> getrefer ())):');

Return $ STR;

}

Function opensocket (){

$ Arr = $ this-> geturl ();

$ This-> _ ressocket = @ fsockopen ($ arr ['host'], $ arr ['Port'], $ interror, $ strerror, 30 );

If (! $ This-> _ ressocket ){

$ This-> _ ressocket = NULL;

Return $ this-> seterror (3, array ($ interror, $ strerror ));

} Else {

Return true;

}

}

Function getrequest (){

Return $ this-> {'get'. ucfirst ($ this-> _ arrurl ['protocol']). 'request '}();

}

Function sendrequest (){

Return fwrite ($ this-> _ ressocket, $ this-> getrequest ());

}

Function flushresponse (){

Return $ this-> {'fluse'. ucfirst ($ this-> _ arrurl ['protocol']). 'response '}();

}

Function gethttprequest (){

$ Arr = $ this-> geturl ();

$ Arrrequest = getallheaders ();

$ Arrrequest ['host'] = $ arr ['host'];

$ Strrequest = "get". $ arr ['uri ']. "HTTP/1.1 \ r \ n ";

Foreach ($ arrrequest as $ key => $ Val ){

$ Strrequest. = "$ key: $ Val \ r \ n ";

}

$ Strrequest. = "\ r \ n ";

Return $ strrequest;

}

Function flushhttpresponse (){

$ Bolheader = true;

While (! Feof ($ this-> _ ressocket )){

$ STR = fgets ($ this-> _ ressocket, 4096 );

If ($ bolheader ){

@ Header ($ Str );

} Else {

Echo ($ Str );

}

If ($ bolheader & $ STR = "\ r \ n "){

$ Bolheader = false;

@ Header ("Content-Type: Application/gzip ");

@ Header ('content-Disposition: attachment; filename = "'. $ this-> _ arrurl ['file'].'" ');

}

}

Return true;

}

Function close (){

Fclose ($ this-> _ ressocket );

$ This-> _ ressocket = NULL;

Return true;

}

Function proxy ($ strurl = NULL, $ arrargv = array ()){

If (! Is_null ($ this-> geturl (pp_return_string) & $ this-> opensocket () & $ this-> sendrequest () & $ this-> flushresponse ()){

Echo (1, 123 );

$ This-> close ();

} Else {

Return false;

}

}

}

If (sizeof ($ _ Get )){

$ Strgetarrayname = '_ get ';

} Elseif (sizeof ($ http_get_vars )){

$ Strgetarrayname = 'HTTP _ get_vars ';

} Else {

Die ('<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">

<HTML>

<Head>

<Meta http-equiv = "Content-Type" content = "text/html">

<Title> phpproxy </title>

<Style type = "text/CSS">

<! -- Body, TD {font-family: verdana; font-size: 12px;} th, input {font-family: verdana;} -->

</Style>

</Head>

<Body>

<Form name = "form1" method = "get" Action = "'. $ _ server ['php _ Self']. '">

<Table width = "500" border = "1" align = "center" cellpadding = "5" cellspacing = "1">

<Tr>

<TH colspan = "2"> phpproxy </Th>

</Tr>

<Tr>

<TD width = "39" nowrap> URL: </TD>

<TD width = "432"> <input name = "U" type = "text" id = "U" size = "60"> </TD>

</Tr>

<Tr>

<TD nowrap> Referer: </TD>

<TD> <input name = "R" type = "text" id = "R" size = "60"> </TD>

</Tr>

<Tr>

<TD colspan = "2" align = "center"> <input name = "act" value = "make" type = "hidden">

<Input type = "Submit" value = "Get proxy URL">

<Input type = "reset" value = "reset"> </TD>

</Tr>

</Table>

</Form>

</Body>

</Html> ');

}

$ Strurl = trim ($ {$ strgetarrayname} ['U']);

If ($ strurl = '){

Die ('Enter the URL address .');

}

// Get Referer

$ Strreftmp = trim ($ {$ strgetarrayname} ['R']);

// Initialize the proxy class

$ Objproxy = new cphpproxy ();

// Set the URL and refer

$ Objproxy-> seturl ($ strurl );

$ Objproxy-> setrefer ($ strref );

// Error output

If ($ objproxy-> geterror ()){

Die ($ objproxy-> geterror (pp_error_message ));

}

// Echo URL for download

If ($ {$ strgetarrayname} ['ac'] = 'make '){

Die ("<center> <H4> <a href = '". $ objproxy-> getproxyurl (). "'> click here to download the file through phpproxy </a> </H2> </center> ");

}

// Obtain the file

$ Objproxy-> proxy ();

// Handle errors

If ($ objproxy-> geterror ()){

Die ($ objproxy-> geterror (pp_error_message ));

}

?>

"Examples of using PHP to implement proxy functions [upgraded to v0.0.2]"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/326940.html pageno: 13.

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.