Search by

somecoding / guzzle-tor

Dwarfex

This Guzzle middleware allows to use Tor client as a proxy

Package info

github.com/somecoding/guzzle-tor

pkg:composer/somecoding/guzzle-tor

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 0

2.0.2 2026-12-03 21:57 UTC

This package is auto-updated.

Last update: 2026-12-03 21:57:34 UTC


README

This Guzzle middleware allows to use Tor client as a proxy

Version 2.0.0

Requires PHP 7.4 or newer, the cURL extension, and Guzzle ^8.6.2. Proxy defaults, request option overrides, and Tor identity switching are unchanged. As required by Guzzle 8, TorNewIdentityException now receives the failed request as its second constructor argument, available through getRequest().

function get_tor_ip()
{
    $stack = new HandlerStack();
    $stack->setHandler(new CurlHandler());
    $stack->push(Middleware::tor());
    $client = new Client(['handler' => $stack]);

    $response = $client->get('https://check.torproject.org/');

    if (preg_match('/<strong>([\d.]+)<\/strong>/', $response->getBody(), $matches)) {
        return $matches[1];
    } else {
        return null;
    }
}

Get started

  1. Add this repository to your project's composer.json so Composer installs this fork rather than the original megahertz/guzzle-tor package:
{
  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/somecoding/guzzle-tor"
    }
  ]
}

The repository must be pushed and accessible to Composer. Commit and push the local changes before installing them from master; install a tagged release once one is published.

  1. In your application's directory, run composer require somecoding/guzzle-tor:dev-master.

  2. (optional) Copy an example and run

cp vendor/somecoding/guzzle-tor/example.php example.php
php example.php

Symfony 3/4 config DI

services:

    GuzzleHttp\HandlerStack: ~
    
    app.client.tor:
      class: GuzzleTor\Middleware
      factory: ['GuzzleTor\Middleware', tor]

    app.client.handler_stack:
      factory: GuzzleHttp\HandlerStack:create
      class: GuzzleHttp\HandlerStack
      calls:
      - [ push, ['@app.client.tor'] ]

    app.client:
      class: GuzzleHttp\Client
      arguments:
      - {handler: '@app.client.handler_stack'}

    #Aliases
    GuzzleHttp\Client: '@app.client'

Laravel

Laravel automatically discovers and registers GuzzleTor\\TorServiceProvider after the Composer installation above. No manual provider registration is needed.

Publish the configuration file if you want to customize the Tor endpoints or Guzzle client options:

php artisan vendor:publish --tag=guzzle-tor-config

The provider registers a dedicated client as guzzle-tor.client, leaving Laravel's default Guzzle client bindings untouched:

$client = app('guzzle-tor.client');
$response = $client->get('https://check.torproject.org/');

The published config/guzzle-tor.php accepts proxy, tor_control, and a client array of Guzzle options. Request-level Guzzle options continue to override the middleware's proxy defaults.

Tests and pull requests

The GitHub Actions job Unit tests runs on every pull request:

vendor/bin/phpunit --exclude-group integration

The integration group contains the two live tests that require a running Tor proxy and internet access; run those separately with vendor/bin/phpunit --group integration. To block merging until tests pass, enable a branch protection rule or ruleset for the target branch, require pull requests and status checks, and select Unit tests as a required check after its first run. The workflow alone does not enforce branch protection.

Options

General

Middleware::tor($proxy, $torControl)
  • $proxy is tor socks5 port, by default '127.0.0.1:9050'
  • $torControl is Tor control port, by default '127.0.0.1:9051'. Set if you want to change ip (clean circuits)

Request options

$client->get('https://check.torproject.org/', [
    'tor_new_identity'           => true,
    'tor_new_identity_sleep'     => 15,
    'tor_new_identity_timeout'   => 3,
    'tor_new_identity_exception' => true, 
    'tor_control_password'       => 'password' 
]);
Name Description
tor_new_identity Change an identity/IP (clean circuits) before request. If it's set, we send NEWNYM signal to Tor client. Please be aware, that this method does not guarantee that an identity will be changed soon
tor_new_identity_sleep Pause (seconds) between an ip change identity and a request sending
tor_new_identity_timeout Timeout for a Tor control connection
tor_control_password Tor control password
tor_new_identity_exception If true throw TorNewIdentityException if an error was occurred while trying to change an identity