How to Use Tor Project Anonymous IP with Curl PHP on Linux
This article explains how to utilize Tor project in our own PHP application using Curl Library.
Tor is free software and an open network that helps you defend against a form of network surveillance that threatens personal freedom and privacy, confidential business activities and relationships, and state security known as traffic analysis.
We start by installing and configuring Tor and the necessary software then we create a simple application that fetch a website address anonymously.
Download and Install Tor
First thing, we need to download and install Tor application into our system. Click the download page to get the correct software for you, either Windows, Apple, or Linux bundle. In this example I use Linux system so I choose Linux.
My system use Debian etch, so I simply use apt-get to install it. But I need to configure my /etc/apt/source.list:
deb http://deb.torproject.org/torproject.org etch main
Then I add the gpg key used to sign the packages by running the following commands at the command prompt:
gpg --keyserver keys.gnupg.net --recv 886DDD89 gpg --export A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89 | sudo apt-key add -
I refresh my sources and install Tor by running the following commands at the command prompt:
apt-get update apt-get install tor tor-geoipdb
Install Polipo
For TOR to run well we need at least Polipo version 1.0.4. Debian etch package for polipo is still on version === so I need to download the source code from Polipo website. I put the source code at /usr/src/polipo-1.0.4.
I need to compile Polipo by typing make at the command prompt.
cd /usr/src/polipo-1.0.4/
make
When successful we will get polipo executable on that directory. We can place it on /opt/polipo directory or whichever you like.
Before running polipo we will need to configure Polipo to use Tor. Grab the Polipo configuration for Tor and put it in place of your current polipo config file (e.g. /etc/polipo/config or ~/.polipo). Then we run polipo to use that configuration file:
/opt/polipo/polipo -c /etc/polipo/config daemonise=true logFile="/var/log/polipo.log"
On that command, I run polipo with config file located at /etc/polipo/config in daemon mode and with a log file. Later you can put this line at /etc/rc.local to run polipo automatically on system start.
Configure Application to use TOR
After successfully installing Tor and Polipo, we need to configure our applications to use them.
For PHP CURL library we need to add an option to Curl to use a HTTP Proxy to access the internet. The option is CURLOPT_PROXY which contains the Proxy address and port. This proxy address is our Polipo proxy server, which is located at 127.0.0.1 on port 8118.
Here is an example code.
<? $url = 'http://www.foo.bar/curl_receive_vars.html'; $postfields = array ('username' => 'Myname', 'emailaddress' => 'myaddress@foo.bar');
if (!$curld = curl_init()) {
echo "Could not initialize cURL session.\n";
exit;
}
/* Prepare for the POST operation. */
curl_setopt($curld, CURLOPT_POST, true);
/* Give cURL the variable names & values to POST. */
curl_setopt($curld, CURLOPT_POSTFIELDS, $postfields);
/* The URL to which to POST the data. */
curl_setopt($curld, CURLOPT_URL, $url);
/* Indicate that we want the output returned into a variable. */
curl_setopt($curld, CURLOPT_RETURNTRANSFER, true); /* Set the proxy to our Polipo. */ curl_setopt($curld, CURLOPT_PROXY, "http://127.0.0.1:8118/");/* Do it. */
$output = curl_exec($curld);
echo "Received data: <hr>$output<hr>\n";
/* Clean up. */
curl_close($curld); ?>
The above code will fetch data from URL specified but the server will not know our real IP but the IP addresses of the Tor networks instead.
Additional Step
Set up Tor on web browser.
You should use Tor with Firefox and Torbutton, for best safety. Simply install the Torbutton plugin, restart your Firefox, and you’re all set:
After that you can browse the internet using Firefox anonymously.



hey,
this may be a bit old, but you need to fix your website’s css, since part of the sourcecode isn’t shown, and update the link to the polipo.conf https://gitweb.torproject.org/torbrowser.git/blob_plain/HEAD:/build-scripts/config/polipo.conf for example.. anyway, great post, thanks!