Knowledgebase / Self-hosting Guide / How can I use CURL with TOR as a proxy in PHP?

How can I use CURL with TOR as a proxy in PHP?

CURL is a utility that facilitates data transfer to or from a server through the use of URLs.

Tor, an acronym for The Onion Router, is an open-source software that is free to use and promotes anonymous interactions online. It routes internet traffic through a global volunteer-driven overlay network comprising over seven thousand relays. The use of Tor increases the challenge of tracing a user's online activities.

PHP is a widely-used universal scripting language that is particularly effective for web development. It is quick, adaptable, and practical, powering everything from personal blogs to the world's most visited websites.

This guide outlines two common methods to utilize CURL with TOR as a proxy using PHP language.

The initial method involves installing the Tor Browser, which can be downloaded from the official Tor website. The website provides instructions to download a version compatible with your operating system. After the download is complete, the next step is to install the browser on your device. The installation process is straightforward and similar to that of any other software or application.

Download Tor Browser for Windows

Download Tor Browser for macOS

Download Tor Browser for Android

The first time you launch the Tor Browser, connection to the Tor network must be established. This can be achieved by clicking the "Connect" button found on the browser's interface. This step is required only during the initial use of the Tor Browser. It is meant to ensure a secure and private connection to the Tor network.

The first time you launch the Tor Browser, connection to the Tor network must be established. This can be achieved by clicking the Connect button found on the browser's interface.

The first time you launch the Tor Browser, connection to the Tor network must be established. This can be achieved by clicking the Connect button found on the browser's interface.

The first time you launch the Tor Browser, connection to the Tor network must be established. This can be achieved by clicking the Connect button found on the browser's interface.

After the connection is established, you can use the Tor Browser as a proxy to connect to a URL. The Tor Browser opens the SOCKS proxy on 127.0.0.1:9150. CURL can utilize this mechanism as a proxy to connect .com or .onion services.

Below script is written in PHP language to show you how to use CURL with tor as a proxy using PHP language.

<?php
$url = 'https://api.ipify.org';
$proxy = '127.0.0.1:9150';

$ch = curl_init();
if($ch === false) die('Failed to create curl object');

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
$content = curl_exec($ch);
curl_close($ch);

echo $content;

When you execute this script using PHP language, it will show the IP address of your Tor instance exit node, instead of your IP address. This is the result of using Tor as a proxy in PHP language.

The second method introduced is by utilizing Docker. Docker is a software platform designed to facilitate building, testing, and deploying applications with speed. It organizes software into uniform units known as containers, which incorporate all the necessary components for the software's operation, such as libraries, system tools, code, and runtime. Docker enables swift deployment and scaling of applications in any environment, ensuring the smooth running of your code.

To utilize Docker to run Tor instance, execute the below command:

$ docker run --rm -p 9050:9050 osminogin/tor-simple

Mar 27 01:36:09.530 [notice] Tor 0.4.8.10 running on Linux with Libevent 2.1.12-stable, OpenSSL 3.1.2, Zlib 1.3, Liblzma 5.4.5, Libzstd 1.5.5 and Unknown N/A as libc.
Mar 27 01:36:09.530 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://support.torproject.org/faq/staying-anonymous/
Mar 27 01:36:09.530 [notice] Read configuration file "/etc/tor/torrc".
Mar 27 01:36:09.533 [warn] You specified a public address '0.0.0.0:9050' for SocksPort. Other people on the Internet might find your computer and use it as an open proxy. Please don't allow this unless you have a good reason.
Mar 27 01:36:09.533 [notice] Opening Socks listener on 0.0.0.0:9050
Mar 27 01:36:09.534 [notice] Opened Socks listener connection (ready) on 0.0.0.0:9050
Mar 27 01:36:09.534 [warn] Fixing permissions on directory /var/lib/tor

The docker run command runs a command in a new container, pulling the image if needed and starting the container. In this example, it pulls a public Tor image from the Docker Hub registry. Use --rm flag to specify that the container should be removed when it exits. Use -p flag to publish a container port 9050 to the host.

Then, you can try to execute the below PHP script. This script shows how to use CURL with tor as a proxy using PHP language.

<?php
$url = 'https://api.ipify.org';
$proxy = '127.0.0.1:9050';

$ch = curl_init();
if($ch === false) die('Failed to create curl object');

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
$content = curl_exec($ch);
curl_close($ch);

echo $content;

Note that when you run Tor instance using Docker, the SOCKS port is 9050, which differs than the SOCKS port used in Tor Browser. This is to ensure that there is no conflict when your system runs Tor instance and Tor Browser at the same time.

This guide demonstrates two common ways of how to use CURL with TOR as a proxy using PHP language.

Link to Tor Project

Link to Docker

Link to Docker Compose

Tags:
  • CURL
  • Tor
  • Tor Browser
  • SOCKS proxy
  • Docker
  • PHP

24/7 Expert Support

Our experts are always on hand to help answer your questions, get you started, and grow your presence online. You can email us any time