Execute url through proxy

1. install tiny proxy on server.
    sudo apt-get install tinyproxy

2. configure tiny proxy
    (i)   vi /etc/tinyproxy.conf
     (ii)  allow ip through which you want to access proxy
        Allow 123.123.12.13
    (iii) set port of proxy
         Port 8080
    (iv)  You can set proxy user (optional)
        User vishal
    
3. Allow proxy port through ip tables
    / sbin/iptables -I INPUT -p tcp --dport 8080 -m state --state NEW,ESTABLISHED -j ACCEPT
    /sbin/ iptables -I OUTPUT -p tcp --sport 8080 -m state --state ESTABLISHED -j ACCEPT

4. restart proxy
    sudo /etc/init.d/tinyproxy restart

 

Setup PHP

To execute url through proxy function will be like this

function getUrl($url) {
    $ch = curl_init($url);
     $timeout = 5; // set to zero for no timeout
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);

// Set Proxy server url
    curl_setopt($ch, CURLOPT_PROXY, “http://122.122.12.12”); //your proxy url

// Set Proxy port
    curl_setopt($ch, CURLOPT_PROXYPORT, PROXY_PORT); // your proxy port number

// Set proxy user and password
    curl_setopt($ch, CURLOPT_PROXYUSERPWD, “vishal:vishal”); //uname:pass

     $file_contents = curl_exec($ch);
    curl_close($ch);
    return $file_contents;
  }

getUrl(“http://google.co.in”);

Comments