PHP code example of hamaadraza / php-impersonate

1. Go to this page and download the library: Download hamaadraza/php-impersonate 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/ */

    

hamaadraza / php-impersonate example snippets


use Raza\PHPImpersonate\PHPImpersonate;

$response = PHPImpersonate::get('https://tls.peet.ws/api/all');
echo $response->json()['tls']['ja4']; // a genuine browser fingerprint


Raza\PHPImpersonate\PHPImpersonate;

$response = PHPImpersonate::get('https://example.com');

echo $response->status();      // 200
echo $response->body();        // the HTML
$data = $response->json();     // decoded JSON (throws on invalid JSON)

use Raza\PHPImpersonate\PHPImpersonate;

$response = PHPImpersonate::get('https://example.com');   // auto engine

PHPImpersonate::get(string $url, array $headers = [], int $timeout = 30, string $browser = 'firefox147', array $curlOptions = [], string $engine = 'auto'): Response;
PHPImpersonate::post(string $url, ?array $data = null, array $headers = [], int $timeout = 30, string $browser = 'firefox147', array $curlOptions = [], string $engine = 'auto'): Response;
PHPImpersonate::put(/* … same shape as post … */): Response;
PHPImpersonate::patch(/* … same shape as post … */): Response;
PHPImpersonate::delete(string $url, array $headers = [], int $timeout = 30, string $browser = 'firefox147', array $curlOptions = [], string $engine = 'auto'): Response;
PHPImpersonate::head(/* … same shape as delete … */): Response;

$response = PHPImpersonate::get('https://example.com', [], 30, 'chrome146');

$client = new PHPImpersonate(
    browser: 'firefox147',   // default; any name from BrowserName::getAll()
    timeout: 30,             // seconds
    curlOptions: [],         // e.g. ['proxy' => 'http://127.0.0.1:8080']
    engine:  PHPImpersonate::ENGINE_AUTO,   // default
);

foreach ($urls as $url) {
    $response = $client->sendGet($url);
}

$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;
$client->send(Raza\PHPImpersonate\Request $request): Response;

new PHPImpersonate('chrome146', engine: PHPImpersonate::ENGINE_FFI);      // FFI, or throw if unusable
new PHPImpersonate('chrome146', engine: PHPImpersonate::ENGINE_PROCESS);  // always the executable

PHPImpersonate::ffiAvailable();                 // is the FFI engine usable here?
PHPImpersonate::ffiUnavailableReason();         // …and if not, why (null when it is)

(new PHPImpersonate('chrome146'))->engine();    // 'ffi' or 'process' — what was chosen

$response = PHPImpersonate::get('https://example.com', [
    'Accept-Language' => 'en-US,en;q=0.9',
]);

$response = PHPImpersonate::post('https://example.com/api', [
    'username' => 'johndoe',
    'email'    => '[email protected]',
]);

$deleted = PHPImpersonate::delete('https://example.com/api/1');
$head    = PHPImpersonate::head('https://example.com');   // headers only, empty body

$response = PHPImpersonate::get('https://api.example.com/user');

if ($response->isSuccess()) {
    $user = $response->json();
    echo $user['name'];
}

// Single-value headers — header() is all you need
$type = $response->header('Content-Type');        // 'application/json'
$etag = $response->header('ETag', 'none');        // default when absent

foreach ($response->headerAll('Set-Cookie') as $cookie) {
    echo $cookie . "\n";
}

// Or inspect everything — every value is an array:
foreach ($response->headers() as $name => $values) {
    foreach ($values as $value) {
        echo "$name: $value\n";
    }
}

$login = $client->sendPost('https://example.com/login', ['user' => 'me', 'pass' => '…']);

// Every Set-Cookie received on ANY response in the redirect chain, in order —
// headers() and headerAll() describe the final response only, and a session
// cookie is usually set on the 302, not on the page it redirects to.
$cookies = [];
foreach ($login->setCookieHeaders() as $setCookie) {
    [$pair] = explode(';', $setCookie, 2);
    [$name, $value] = explode('=', $pair, 2);
    $cookies[$name] = $value;
}

$page = $client->sendGet('https://example.com/account', [
    'Cookie' => http_build_query($cookies, '', '; ', PHP_QUERY_RFC3986),
]);

$response = PHPImpersonate::get('https://example.com', [
    'User-Agent' => 'MyApp/1.0',   // replaces the profile's User-Agent
    'X-Request-Id' => 'abc123',    // added to it
]);

use Raza\PHPImpersonate\Browser\BrowserName;

BrowserName::latest('chrome');       // 'chrome150' today
BrowserName::latest('firefox');      // 'firefox147'
BrowserName::latest('safari_ios');   // 'safari260_ios' — a desktop family never answers with a mobile profile

$client = new PHPImpersonate('safari184');
$response = $client->sendGet('https://example.com');

$client = new PHPImpersonate('chrome146', 30, [
    'proxy'      => 'http://127.0.0.1:8080',   // or socks5://127.0.0.1:1080
    'proxy-user' => 'user:password',
]);

$response = $client->sendGet('https://api.ipify.org?format=json');

// Form-encoded (application/x-www-form-urlencoded) — the POST default
PHPImpersonate::post('https://example.com/api', ['name' => 'John']);

// JSON — set a JSON Content-Type
PHPImpersonate::post('https://example.com/api',
    ['name' => 'John'],
    ['Content-Type' => 'application/json']
);

// multipart/form-data — the boundary is generated and added to the header
PHPImpersonate::post('https://example.com/api',
    ['name' => 'John', 'tags' => ['a', 'b']],
    ['Content-Type' => 'multipart/form-data']
);

PHPImpersonate::get('https://example.com', [], 5);   // 5-second timeout
$client = new PHPImpersonate('chrome146', 10);       // 10-second default for this client

use Raza\PHPImpersonate\Exception\PHPImpersonateException;

try {
    $response = PHPImpersonate::get('https://example.com/maybe', [], 5);

    if (! $response->isSuccess()) {
        echo "HTTP {$response->status()}\n";
    }
} catch (PHPImpersonateException $e) {
    echo "Request failed: {$e->getMessage()}";
}

use Raza\PHPImpersonate\Exception\RequestException;
use Raza\PHPImpersonate\Exception\InvalidArgumentException;

try {
    $response = PHPImpersonate::get($url);
} catch (InvalidArgumentException $e) {
    // the request was never sent — fix the arguments
} catch (RequestException $e) {
    // it went out and failed — worth retrying
}
bash
composer 
bash
php vendor/hamaadraza/php-impersonate/bin/php-impersonate-install
json
{
  "scripts": {
    "post-install-cmd": ["@php vendor/hamaadraza/php-impersonate/bin/php-impersonate-install"],
    "post-update-cmd":  ["@php vendor/hamaadraza/php-impersonate/bin/php-impersonate-install"]
  }
}