Http message Class Based on Torsocks5-PHP source code

Source: Internet
Author: User
Ec (2); try {& nbsp; $ tornewTorHttp (127.0.0.1, 9050); & nbsp; $ dataarray (username & gt; liujun); & nbsp; $ headersarray (Cookie & gt; php tutorial id12345 script ec (2); script

Try {
$ Tor = new TorHttp ('2017. 0.0.1 ', 127 );
$ Data = array ('username' => 'liujun ');
$ Headers = array ('cookie '=> 'php tutorial id = 123456; xx = V ');
Echo $ tor-> get ('HTTP: // host.com/testsocks.php', $ headers );
$ Tor-> newId ('20140901 ');
} Catch (Exception $ e ){
If ($ e-> getCode () = 9999 ){
$ Tor-> newId ('20140901 ');
}
Echo $ e-> getMessage (). "n ";
}
*/

Class Tool_TorHttp
{
/**
* Tor socks Server
*
* @ Var */
Private $ _ host;

/**
* Socks service connection
*
* @ Var
*/
Private $ _ sock;

/**
* Constructor
*
* @ Param $ Host socks server address
* @ Param $ Port
*/
Public function _ construct ($ host = '192. 0.0.1 ', $ port = 127)
{
$ This-> _ host = $ host;
@ $ This-> _ sock = fsockopen ($ host, $ port, $ errorCode, $ error, 5 );

If ($ errorCode ){
Throw new Exception ('cannot connect to the proxy Server ');
}

// Establish the connection at the application layer
Fwrite ($ this-> _ sock, pack ('c3', 5, 1, 0 ));
$ Resp = fread ($ this-> _ sock, 1024 );
$ Resp = unpack ('cversion/cmethod', $ resp );
If ($ resp ['version']! = 5 | $ resp ['method']! = 0 ){
$ This-> _ sock = null;
Throw new Exception ('proxy server unavailable or connection password ');
}
}

/**
* Connect to the target host
*
* @ Param $ Host
* @ Param $ Port
*/
Private function _ connect ($ host, $ port)
{
// The ip address and Domain Name Description server use different message formats
$ Lip = ip2long ($ host );
If (empty ($ lip )){
$ Pack = pack ('c5 ', 5, 1, 0, 3, strlen ($ host). $ host. pack ('n', intval ($ port ));
} Else {
$ Pack = pack ('c4nn ', 5, 1, 0, 1, $ lip, $ port );
}
Fwrite ($ this-> _ sock, $ pack );
$ Resp = '';
$ Counter = 0;
While (true ){
If (feof ($ this-> _ sock )){
Break;
}
$ Resp. = fread ($ this-> _ sock, 1024 );
If (! Empty ($ resp) | $ counter = 50 ){
Break;
}
$ Counter + = 1;
}

$ Resp = unpack ('cversion/crep', $ resp );
If ($ resp ['version']! = 5) | ($ resp ['rep ']! = 0 )){
Throw new Exception ("requested service [$ host: $ port] is temporarily unavailable, or Tor cannot be reached. If it occurs repeatedly, try restarting Tor", 9999 );
}
}

/**
* Initiate an http get request
*
* @ Param $ Url
* @ Return
*/
Public function get ($ url, $ headers = array ())
{
$ Ua = parse_url ($ url );
If (empty ($ ua ['Port']) {
$ Ua ['Port'] = 80;
}

$ This-> _ connect ($ ua ['host'], $ ua ['Port']);

$ RequestUri = $ ua ['path'];

If (! Empty ($ ua ['query']) {
$ RequestUri. = '? '. $ Ua ['query'];
}

$ Headers ['host'] = $ ua ['host'];
If (! Isset ($ headers ['user-agent']) {
$ Headers ['user-agent'] = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv: 1.9.0.10) Gecko/2009042316 Firefox/3.0.10 GTB5 ";
}
$ Headers ['connection'] = 'close ';

Fwrite ($ this-> _ sock, "GET {$ requestUri} HTTP/1.1n ");
Foreach ($ headers as $ key => $ val ){
Fwrite ($ this-> _ sock, "{$ key }:{ $ val} n ");
}
Fwrite ($ this-> _ sock, "n ");

$ Resp = '';
While (! Feof ($ this-> _ sock )){
$ Resp. = fread ($ this-> _ sock, 1024 );
}
Return $ resp;
}

/**
* Initiate an http post request
*
* @ Param $ Url
* @ Param $ Data
* @ Param $ Headers
* @ Return
*/
Public function post ($ url, $ data, $ headers = array ())
{
$ Ua = parse_url ($ url );

If (empty ($ ua ['Port']) {
$ Ua ['Port'] = 80;
}

$ This-> _ connect ($ ua ['host'], $ ua ['Port']);

If (isset ($ ua ['path']) &! Empty ($ ua ['path']) {
$ RequestUri = $ ua ['path'];
} Else {
$ RequestUri = '/';
}

If (! Empty ($ ua ['query']) {
$ RequestUri. = '? '. $ Ua ['query'];
}

If (! Isset ($ headers ['user-agent']) {
$ Headers ['user-agent'] = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv: 1.9.0.10) Gecko/2009042316 Firefox/3.0.10 GTB5 ";
}
$ Headers ['connection'] = 'close ';

$ Data = $ this-> _ parseData ($ data );

Fwrite ($ this-> _ sock, "POST {$ requestUri} HTTP/1.1n ");
Fwrite ($ this-> _ sock, "Host: {$ ua ['host']} n ");
Fwrite ($ this-> _ sock, "Content-Type: application/x-www-form-urlencodedn ");
Fwrite ($ this-> _ sock, "Content-Length:". strlen ($ data). "n ");
Foreach ($ headers as $ key => $ val ){
Fwrite ($ this-> _ sock, "{$ key }:{ $ val} n ");
}
Fwrite ($ this-> _ sock, "n ");
Fwrite ($ this-> _ sock, $ data. "n ");

$ Resp = '';
While (! Feof ($ this-> _ sock )){
$ Resp. = fread ($ this-> _ sock, 1024 );
}
Return $ resp;
}

/**
* Update the Tor identity
*
* @ Param $ Password
* @ Param $ Port
* @ Return
*/
Public function newId ($ password = '', $ port = 9051)
{
If (! Empty ($ password) & $ password [0]! = '"'){
$ Password = '"'. $ password .'"';
}

// Create a connection to the tor control terminal
@ $ Sock = fsockopen ($ this-> _ host, $ port, $ errorCode, $ error, 5 );
If ($ errorCode ){
Throw new Exception ('cannot connect to the control end of the proxy server. Check the port number ');
}

Fwrite ($ sock, "AUTHENTICATE {$ password} n ");
$ Resp = fread ($ sock, 1024 );
If (! Preg_match ('/^ 250/', $ resp )){
Throw new Exception ('Tor control authentication failed, please confirm the correct password ');
}

Fwrite ($ sock, "SIGNAL NEWNYMn ");
$ Resp = fread ($ sock, 1024 );
If (! Preg_match ('/^ 250/', $ resp )){
Throw new Exception ('failed to update the identity, Please retry ');
}
Return true;
}

Private function _ parseData ($ data)
{
If (empty ($ data) |! Is_array ($ data )){
Return $ data;
}
$ Encoded = '';
While (list ($ k, $ v) = each ($ data )){
$ Encoded. = $ k. "=". $ v .'&';
}
Return substr ($ encoded, 0,-1 );
}

Public function _ destruct ()
{
Fclose ($ this-> _ sock );
$ This-> _ sock = null;
}
}

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.