PHP code example of ecourty / tor-php

1. Go to this page and download the library: Download ecourty/tor-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ecourty / tor-php example snippets


   
   TorPHP\TorHttpClient;
   
   $client = new TorHttpClient();
   $response = $client->request('GET', 'https://api.ipify.org?format=json');
   echo $response->getContent(); // Your Tor exit IP
   

   use TorPHP\TorControlClient;
   
   $control = new TorControlClient(
       password: 'your_secure_password'
   );
   

   use TorPHP\TorControlClient;
   
   $control = new TorControlClient(
       authenticationCookie: '/var/run/tor/control.authcookie'
   );
   

use TorPHP\TorControlClient;

$control = new TorControlClient(); // No credentials

  $control = new TorControlClient(
      password: $_ENV['TOR_CONTROL_PASSWORD']
  );
  



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;



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');



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);



use TorPHP\Helper\KeyPairGenerator;
use TorPHP\TorControlClient;
use TorPHP\Model\PortMapping;

// Generate a new ED25519-V3 keypair
$keyPair = KeyPairGenerator::generate();

echo "Service ID: {$keyPair->serviceId}.onion\n";
echo "Private Key: {$keyPair->getPrivateKeyFormatted()}\n";

// Use the generated key to create an onion service
$control = new TorControlClient();
$onion = $control->addOnionService(
    portMappings: [new PortMapping('127.0.0.1', 8080, 80)],
    privateKey: $keyPair->getPrivateKeyBase64()
);

// The onion service ID will match the derived serviceId from the keypair
assert($onion->id === $keyPair->serviceId);
bash
   composer 
ini
ControlPort 9051
bash
composer 
ini
   ControlPort 9051
   CookieAuthentication 1
   
ini
ControlPort 9051
CookieAuthentication 0