kelvinzer0/curl-impersonate-php

Curl-Impersonate-PHP - Library to execute HTTP requests using cURL in PHP with the ability to impersonate the behavior of major browsers (Chrome, Firefox, Safari, and Microsoft Edge).

v3.2 2023-12-20 12:04 UTC

This package is not auto-updated.

Last update: 2024-12-10 15:47:04 UTC


README

Curl-Impersonate-PHP is a library that allows the execution of HTTP requests using cURL within the PHP environment with the ability to emulate the behavior of four major browsers (Chrome, Firefox, Safari, and Microsoft Edge).

Description

Curl-Impersonate-PHP is an implementation of the original project Curl-Impersonate, available at https://github.com/lwthiker/curl-impersonate, which introduces a specialized cURL build that can mimic the behavior of four major browsers. By using Curl-Impersonate-PHP, you can make HTTP requests from PHP using cURL but with headers and behavior that resemble Chrome, Firefox, Safari, or Microsoft Edge.

Key Features

  • Support for making HTTP requests through cURL with headers and behavior emulating Chrome, Firefox, Safari, and Microsoft Edge.
  • Full control over cURL options such as the target URL, HTTP method, request data, headers, and more through the setopt function.
  • Ability to access the execution results of the request in the form of a cURL command, standard output, or streaming.

Installation

You can install Curl-Impersonate-PHP using Composer. Run the following command in your project directory:

composer require kelvinzer0/curl-impersonate-php

Usage

Below is an example of using Curl-Impersonate-PHP to make an HTTP request while emulating the behavior of a browser:

require 'vendor/autoload.php';
$curl = new CurlImpersonate\CurlImpersonate();
$curl->setopt(CURLCMDOPT_URL, 'https://example.com/');
$curl->setopt(CURLCMDOPT_METHOD, 'GET');
$curl->setopt(CURLCMDOPT_HEADER, false);
$curl->setopt(CURLCMDOPT_ENGINE, "/Users/qindexmedia/Downloads/curl-impersonate-v0.5.4.x86_64-macos/curl_safari15_3");
$response = $curl->execStandard();
echo $response;
$curl->closeStream();

Be sure to replace the value of the CURLCMDOPT_URL option with the appropriate target URL and set the browser impersonation according to your needs.

setopt Function

The setopt function is used to set options in the HTTP request that will be executed using cURL. Here is a list of options supported by the setopt function:

Streaming Usage

In addition to supporting getting the standard output of HTTP request execution, Curl-Impersonate-PHP also provides the ability to stream (retrieve data in chunks) the response from an ongoing HTTP request. You can use the streaming feature with the following steps:

  1. Enable Streaming: Call the execStream function to enable streaming before starting the HTTP request:
$curl = new CurlImpersonate\CurlImpersonate();
$curl->setopt(CURLCMDOPT_URL, 'https://example.com/');
$curl->setopt(CURLCMDOPT_METHOD, 'GET');
$curl->setopt(CURLCMDOPT_ENGINE, "/Users/qindexmedia/Downloads/curl-impersonate-v0.5.4.x86_64-macos/curl_safari15_3");
$curl->execStream();
  1. Retrieve Data in Chunks: You can use the readStream function to retrieve response data in chunks of the specified size:
$chunkSize = 4096; // Size of the data chunks to be retrieved (in bytes)
while ($data = $curl->readStream($chunkSize)) {
    echo $data;
    // Process the response data here
}
  1. Close Streaming: After you finish using streaming, be sure to close it by calling the closeStream function:
$curl->closeStream();

Example of Streaming Usage

Here's a complete example of using streaming in Curl-Impersonate-PHP:

$curl = new CurlImpersonate\CurlImpersonate();
$curl->setopt(CURLCMDOPT_URL, 'https://example.com/');
$curl->setopt(CURLCMDOPT_METHOD, 'GET');
$curl->setopt(CURLCMDOPT_ENGINE, "/Users/qindexmedia/Downloads/curl-impersonate-v0.5.4.x86_64-macos/curl_safari15_3");
$curl->execStream();

$chunkSize = 4096; // Size of the data chunks to be retrieved (in bytes)
while ($data = $curl->readStream($chunkSize)) {
    echo $data;
    // Process the response data here
}

$curl->closeStream();

Be sure to replace the value of CURLCMDOPT_URL with the appropriate target URL and set the browser impersonation according to your needs. Streaming is particularly useful for handling large responses or responses that need to be processed in specific chunks sequentially.

Contributions

If you would like to contribute to Curl-Impersonate-PHP, we greatly appreciate your contributions. Please open a new issue or submit a pull request on our GitHub repository.

License

Curl-Impersonate-PHP is licensed under the MIT License, which means you are free to use, modify, and distribute this library according to the terms of the license.