Tor-PHP is a PHP library that provides two main things:
TorHttpClient
A Tor-proxied HTTP client built on top of Symfony'sHttpClientcomponent.
It allows you to send HTTP requests through the Tor network for anonymous browsingTorControlClient
A socket client implementing the TorControl protocol to manage your node, circuits and hidden services.
- HTTP requests through Tor (SOCKS5 proxy)
- Tor ControlPort integration for:
- Requesting a new identity
- Managing Tor circuits and configuration
- Creating and deleting Onion Services
- Retrieve the current Tor exit nodes
You must have the Tor service installed and running.
sudo apt install torbrew install torMake sure the
ControlPortis enabled in your Tor configuration (/etc/tor/torrcor equivalent):ControlPort 9051 HashedControlPassword <your_password_hash> CookieAuthentication 0
composer require ecourty/tor-php- PHP 8.4 or higher
- Tor must be running locally with ControlPort enabled for full features integration
<?php
use TorPHP\TorHttpClient;
use Symfony\Component\HttpClient\HttpClient;
$torClient = new TorHttpClient();
$response = $torClient->request('GET', 'https://api.ipify.org?format=json');
$torIp = $response->toArray()['ip'];
$normalClient = HttpClient::create();
$normalIp = $normalClient->request('GET', 'https://api.ipify.org?format=json')->toArray()['ip'];
echo "Tor IP: $torIp" . PHP_EOL;
echo "Non-Tor IP: $normalIp" . PHP_EOL;<?php
use TorPHP\TorHttpClient;
$torClient = new TorHttpClient();
$torClient->request('GET', 'https://example.com');
// Change circuit when you need
$torClient->newIdentity();
$response = $torClient->request('GET', 'https://example.com/another-page');<?php
use TorPHP\TorControlClient;
use TorPHP\Model\PortMapping;
$control = new TorControlClient();
// Get circuits
$circuits = $control->getCircuits();
// Change config
$control->setConfigValue('SocksPort', '9080');
// Add onion service
$onion = $control->addOnionService([
new PortMapping(host: 'localhost', localPort: 3000, remotePort: 80),
]);
// List onion services
$services = $control->listOnionServices();
// Delete onion service
$control->deleteOnionService($onion->id);Other code examples can be found here.
See CHANGELOG.md for details.
Edouard Courty
MIT Licensed (see)
© 2025
Feel free to open an issue or contribute via pull requests!
See contributing guidelines