farhaanaliii/curl-cffi
Composer 安装命令:
composer require farhaanaliii/curl-cffi
包简介
PHP bindings for curl-impersonate via FFI, with browser fingerprint impersonation support
README 文档
README
PHP curl_cffi
PHP bindings for curl-impersonate via FFI, with browser fingerprint impersonation support.
Overview
PHP curl_cffi is a comprehensive port of the popular Python curl_cffi library, providing low-level and high-level interfaces to libcurl-impersonate using PHP's FFI (Foreign Function Interface) extension. This library allows PHP applications to impersonate TLS/JA3 and HTTP/2 client fingerprints of major browsers (Chrome, Firefox, Safari), making it highly effective for web scraping and bypassing anti-bot solutions like Cloudflare, Akamai, and others.
Features
- Browser Impersonation: Spoof JA3 TLS and HTTP/2 browser fingerprints out of the box (e.g. Chrome 110, Firefox 100, etc.).
- High-Level Requests API: Simple static shortcuts like
Requests::get(),Requests::post(), matching Python's requests interface. - Stateful Sessions: Maintain cookies, headers, proxies, and connection reuse across multiple HTTP requests.
- Concurrent Requests: Perform concurrent non-blocking HTTP requests using the FFI-powered
MultiSessionwrapper. - WebSockets Support: Connect, send, and receive text/binary frames dynamically using native libcurl WebSocket APIs.
- Autoloading & Auto-Configured Exception Map: Automatically map curl error codes to specialized exceptions like
DNSErrororProxyError.
Prerequisites
- PHP 8.1 or higher.
- PHP FFI extension enabled (
extension=ffiinphp.ini). - For web servers (FPM/Apache), ensure
ffi.enable=onis configured inphp.ini.
Installation
1. Install the PHP Library
Install the package via Composer:
composer require farhaanaliii/curl-cffi
2. Install the Native Library (libcurl-impersonate)
This library loads libcurl-impersonate dynamically using PHP FFI. You must install the compiled binary (.dll, .so, or .dylib) on your system:
Option A: Set Environment Variable (Recommended)
You can download the precompiled library and point to it directly in your application code or environment configuration:
// Define this before making any CurlCffi calls (e.g., in your bootstrap file) putenv("CURL_CFFI_LIB_PATH=/path/to/libcurl-impersonate.so");
Option B: Install to System PATH
Download the precompiled binary matching your architecture from the curl-impersonate Releases:
-
Linux (Debian/Ubuntu):
- Download the
libcurl-impersonate-v*.aarch64-linux-gnu.tar.gz(ARM64) orlibcurl-impersonate-v*.x86_64-linux-gnu.tar.gz(x86_64). - Extract the file and copy
libcurl-impersonate.so(and any related symlinks) to/usr/local/lib/. - Run
sudo ldconfigto update the system dynamic linker cache.
- Download the
-
macOS:
- Download
libcurl-impersonate-v*.macos.tar.gzmatching your CPU (ARM64 / Apple Silicon or Intel). - Copy
libcurl-impersonate.dylibto/usr/local/lib/.
- Download
-
Windows:
- Download
libcurl-impersonate-v*.x86_64-win32.tar.gz. - Extract and copy
libcurl-impersonate.dll. - Place it in your system
PATH(e.g.C:\Windows\System32), or directly in your PHP installation folder (the same directory containingphp.exe).
- Download
Usage
Simple GET Request
<?php require_once 'vendor/autoload.php'; use CurlCffi\Requests; $response = Requests::get("https://httpbin.org/headers", [ 'impersonate' => 'chrome110' ]); echo "Status: " . $response->statusCode . "\n"; echo "Body:\n" . $response->getText() . "\n";
Session Management
<?php require_once 'vendor/autoload.php'; use CurlCffi\Session; $session = new Session(); $response1 = $session->post("https://httpbin.org/post", [ 'json' => ['username' => 'testuser'], 'impersonate' => 'chrome110' ]); $response2 = $session->get("https://httpbin.org/cookies", [ 'impersonate' => 'chrome110' ]); echo $response2->getText();
Concurrent Requests via MultiSession
<?php require_once 'vendor/autoload.php'; use CurlCffi\MultiSession; $multi = new MultiSession(); $id1 = $multi->add('GET', 'https://httpbin.org/headers', [ 'impersonate' => 'chrome110' ]); $id2 = $multi->add('GET', 'https://httpbin.org/html', [ 'impersonate' => 'chrome110' ]); $results = $multi->perform(); foreach ($results as $id => $res) { if ($res instanceof \Exception) { echo "Request {$id} failed: " . $res->getMessage() . "\n"; } else { echo "Request {$id} status: " . $res->statusCode . "\n"; } } $multi->close();
WebSockets
<?php require_once 'vendor/autoload.php'; use CurlCffi\WebSocket; $ws = new WebSocket(); $ws->connect("wss://ws.postman-echo.com/raw"); $ws->sendStr("Hello WebSocket!"); $reply = $ws->recvStr(5.0); echo "Server said: " . $reply . "\n"; $ws->close();
Authors & Credits
- Author: Farhaan Ali (GitHub: @farhaanaliii)
- Original Projects:
- Python curl_cffi wrapper: lexiforest/curl_cffi
- Precompiled curl-impersonate libraries: lexiforest/curl-impersonate
License
This project is open-source and licensed under the MIT License. See LICENSE for details.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-24