Home
PHP
Tech Tube
MySQL
Linux
CSS&HTML
JavaScript

TOR requests via PHP

This post describes the way of sending TOR requests using PHP. The source of the code and the concept is https://gist.github.com/sxss/acfdce73976f219a6695 The script requires to install TOR:
sudo apt-get install tor
Usage example:
$proxy = new Proxy();
$post = array(
    'key1' => 'value1',
    'key2' => 'value2'
);
$x = $proxy->curl('https://www.iplocation.net/find-ip-address');
var_dump($x);
$x = $proxy->curl('http://ipecho.net/plain');
var_dump($x);
<?php

class Proxy {

    private $ch;
    private $proxy;

    function __construct() {
        $torSocks5Proxy = "socks5://127.0.0.1:9050";

        $this->ch = curl_init();
        curl_setopt( $this->ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5 );
        curl_setopt( $this->ch, CURLOPT_PROXY, $torSocks5Proxy );
        curl_setopt( $this->ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $this->ch, CURLOPT_FOLLOWLOCATION, true );
        curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $this->ch, CURLOPT_HEADER, false );
	    curl_setopt( $this->ch, CURLOPT_HTTPHEADER, array(
		    'Accept:text/html, */*; q=0.01',
		    'Accept-Encoding:gzip, deflate, sdch',
		    'Accept-Language:en-US,en;q=0.8',
		    'Connection:keep-alive',
		    'Cookie:_hjIncludedInSample=1; PHPSESSID=1234319c2438ef04086e64776f68bff0; hfsl=1; _ga=GA1.2.726218037.1495492344; _gid=GA1.2.2108210767.1495502231; _gat=1',
		    'Host:www.hillsshelter.bg',
		    'Referer:http://www.hillsshelter.bg/pet-sisters-sofia/',
		    'User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
		    'X-Requested-With:XMLHttpRequest',
	    ));
    }

    public function curl( $url, $postParameter = null ) {
        if( sizeof( $postParameter ) > 0 ) {
            curl_setopt( $this->ch, CURLOPT_POSTFIELDS, $postParameter );
        }
        curl_setopt( $this->ch, CURLOPT_URL, $url );
        $result = curl_exec( $this->ch );
        return $result;
    }

    function __destruct() {
        curl_close( $this->ch );
    }
}

Download...