hamaadraza/php-impersonate

GitHub: hamaadraza/php-impersonate

一个基于 curl-impersonate 的 PHP HTTP 客户端,通过模拟真实浏览器的 TLS 指纹和请求签名来绑过反爬虫检测。

Stars: 58 | Forks: 4

# PHP-Impersonate [![测试](https://img.shields.io/github/actions/workflow/status/hamaadraza/php-impersonate/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/hamaadraza/php-impersonate/actions/workflows/run-tests.yml) 一个用于通过浏览器模拟发起 HTTP 请求的 PHP 库。该库使用 curl-impersonate 来模拟各种浏览器的网络签名,使其适用于访问那些可能会检测并阻止自动化请求的网站。 ## 安装 通过 Composer 安装: ``` composer require hamaadraza/php-impersonate ``` ## 系统要求 - PHP 8.0 或更高版本 ## 基本用法 ``` body(); // POST request with data $response = PHPImpersonate::post('https://example.com/api', [ 'username' => 'johndoe', 'email' => 'john@example.com' ]); // Check the response if ($response->isSuccess()) { $data = $response->json(); echo "User created with ID: " . $data['id']; } else { echo "Error: " . $response->status(); } ``` ## API 参考 ### 静态方法 该库提供了便捷的静态方法用于发起请求: ``` // GET request with optional headers and timeout PHPImpersonate::get(string $url, array $headers = [], int $timeout = 30): Response // POST request with optional data, headers and timeout PHPImpersonate::post(string $url, ?array $data = null, array $headers = [], int $timeout = 30): Response // PUT request with optional data, headers and timeout PHPImpersonate::put(string $url, ?array $data = null, array $headers = [], int $timeout = 30): Response // PATCH request with optional data, headers and timeout PHPImpersonate::patch(string $url, ?array $data = null, array $headers = [], int $timeout = 30): Response // DELETE request with optional headers and timeout PHPImpersonate::delete(string $url, array $headers = [], int $timeout = 30): Response // HEAD request with optional headers and timeout PHPImpersonate::head(string $url, array $headers = [], int $timeout = 30): Response ``` ### 实例方法 您也可以创建客户端实例以获取更多配置选项: ``` // Create a client with specific browser and timeout $client = new PHPImpersonate('chrome107', 30); // Instance methods $client->sendGet(string $url, array $headers = []): Response $client->sendPost(string $url, ?array $data = null, array $headers = []): Response $client->sendPut(string $url, ?array $data = null, array $headers = []): Response $client->sendPatch(string $url, ?array $data = null, array $headers = []): Response $client->sendDelete(string $url, array $headers = []): Response $client->sendHead(string $url, array $headers = []): Response // Generic send method $client->send(Request $request): Response ``` ### 响应方法 `Response` 类提供了几种用于处理 HTTP 响应的方法: ``` // Get the HTTP status code $response->status(): int // Get the response body as string $response->body(): string // Check if the response was successful (status code 200-299) $response->isSuccess(): bool // Parse the JSON response $response->json(): array|null // Get a specific header value $response->header(string $name, ?string $default = null): ?string // Get all headers $response->headers(): array // Dump information about the response (returns string) $response->dump(): string // Output debug information about the response (echoes and returns self) $response->debug(): Response ``` ## 浏览器选项 PHP-Impersonate 支持模拟多种浏览器: - `chrome99_android` (默认) - `chrome99` - `chrome100` - `chrome101` - `chrome104` - `chrome107` - `chrome110` - `chrome116` - `chrome119` - `chrome120` - `chrome123` - `chrome124` - `chrome131` - `chrome131_android` - `chrome133a` - `chrome136` - `edge99` - `edge101` - `firefox133` - `firefox135` - `safari153` - `safari155` - `safari170` - `safari172_ios` - `safari180` - `safari180_ios` - `safari184` - `safari184_ios` - `safari260` - `safari260_ios` - `tor145` 示例: ``` // Create a client that mimics Firefox $client = new PHPImpersonate('firefox105'); $response = $client->sendGet('https://example.com'); ``` ## 超时设置 您可以配置请求超时时间: ``` // Set a 5-second timeout for this request $response = PHPImpersonate::get('https://example.com', [], 5); // Or when creating a client instance $client = new PHPImpersonate('chrome107', 10); // 10-second timeout ``` ## 代理配置 您可以使用 `curlOptions` 参数通过代理服务器路由请求: ### 基本代理用法 ``` use Raza\PHPImpersonate\PHPImpersonate; $client = new PHPImpersonate( browser: 'chrome136', timeout: 30, curlOptions: [ 'proxy' => 'http://127.0.0.1:8080', // HTTP proxy 'proxy-user' => 'user:password', // optional authentication ] ); $response = $client->sendGet('https://api.ipify.org?format=json'); echo $response->body(); ``` ### 代理选项 支持以下与代理相关的 curl 选项: | 选项 | 描述 | 示例 | |--------|-------------|---------| | `proxy` | 代理服务器地址 | `'http://127.0.0.1:8080'` 或 `'http://proxy.example.com:3128'` | | `proxy-user` | 代理认证凭据 | `'username:password'` | ### SOCKS 代理 您也可以通过指定协议来使用 SOCKS 代理: ``` $client = new PHPImpersonate( browser: 'chrome136', timeout: 30, curlOptions: [ 'proxy' => 'socks5://127.0.0.1:1080', // SOCKS5 proxy ] ); ``` ### 在静态方法中使用代理 对于需要使用代理的一次性请求,请创建一个实例并使用实例方法: ``` $client = new PHPImpersonate( browser: 'chrome136', timeout: 30, curlOptions: [ 'proxy' => 'http://proxy.example.com:8080', 'proxy-user' => 'user:pass', ] ); // GET request through proxy $response = $client->sendGet('https://example.com'); // POST request through proxy $response = $client->sendPost('https://example.com/api', [ 'key' => 'value' ]); ``` ## 高级示例 ### JSON API 请求 ``` // Data will be automatically converted to JSON with correct Content-Type $data = [ 'title' => 'New Post', 'body' => 'This is the content', 'userId' => 1 ]; $response = PHPImpersonate::post( 'https://jsonplaceholder.typicode.com/posts', $data, ['Content-Type' => 'application/json'] ); $post = $response->json(); echo "Created post with ID: {$post['id']}\n"; ``` ### 错误处理 ``` try { $response = PHPImpersonate::get('https://example.com/nonexistent', [], 5); if (!$response->isSuccess()) { echo "Error: HTTP {$response->status()}\n"; echo $response->body(); } } catch (\Raza\PHPImpersonate\Exception\RequestException $e) { echo "Request failed: " . $e->getMessage(); } ``` ## POST、PUT 和 PATCH 请求的数据格式 PHP-Impersonate 支持以不同格式发送数据: ### 表单数据 默认情况下,数据以表单数据 (`application/x-www-form-urlencoded`) 形式发送: ``` // This will be sent as form data $response = PHPImpersonate::post('https://example.com/api', [ 'username' => 'johndoe', 'email' => 'john@example.com' ]); // Explicitly specify form data $response = PHPImpersonate::post('https://example.com/api', [ 'username' => 'johndoe', 'email' => 'john@example.com' ], ['Content-Type' => 'application/x-www-form-urlencoded'] ); ``` ### JSON 数据 您可以通过指定 `Content-Type` 标头将数据作为 JSON 发送: ``` // Send data as JSON $response = PHPImpersonate::post('https://example.com/api', [ 'username' => 'johndoe', 'email' => 'john@example.com' ], ['Content-Type' => 'application/json'] ); ``` 对于 PUT 和 PATCH 请求,JSON 为默认格式。 ## 测试 运行测试套件: ``` composer test ``` ## 许可证 本项目基于 MIT 许可证授权 - 详情请参阅 LICENSE 文件。
标签:Composer包, cURL, ffuf, OpenVAS, PHP, PHP库, Radare2, TLS指纹, UA伪造, User-Agent, Web抓取, 反爬虫, 命令控制, 数据采集, 浏览器指纹, 绕过检测, 网络安全, 网络安全, 自动化请求, 请求头篡改, 身份模拟, 隐私保护, 隐私保护