megahertz / guzzle-tor
这个Guzzle中间件允许使用Tor客户端作为代理
v1.1.0
2020-12-24 14:09 UTC
Requires
- guzzlehttp/guzzle: >=6.2
Requires (Dev)
- phpunit/phpunit: 9.*
This package is auto-updated.
Last update: 2024-08-24 21:52:24 UTC
README
此 Guzzle 中间件允许使用Tor客户端作为代理
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; } }
开始使用
- 安装Composer
按照 https://getcomposer.org.cn/download/ 上的说明进行操作
- 安装guzzle-tor
php composer.phar require megahertz/guzzle-tor
- (可选) 复制示例并运行
cp vendor/megahertz/guzzle-tor/example.php example.php php example.php
Symfony 3/4 配置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'
选项
常规
Middleware::tor($proxy, $torControl)
- $proxy 是tor socks5端口,默认为'127.0.0.1:9050'
- $torControl 是Tor控制端口,默认为'127.0.0.1:9051'。如果您想更改IP(清除回路),则设置该选项
请求选项
$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' ]);