PHP code example of xicrow / php-curl

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

    

xicrow / php-curl example snippets


$request = new Request();

$request = new Request([
    CURLOPT_URL       => 'example.com',
    CURLOPT_USERAGENT => 'Mozilla/4.0',
    CURLOPT_TIMEOUT   => 5,
]);

$request->curlOptions()->set(CURLOPT_URL, 'example.com')->set(CURLOPT_USERAGENT, 'Mozilla/4.0');
$request->curlOptions()->set([
    CURLOPT_TIMEOUT        => 5,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_MAXREDIRS      => 3,
]);

$response = $request->execute();

$response->body();

$response->headers()->get();

$response->headers()->getHttpStatusCode();
$response->headers()->get('Http-Status-Code');
$response->headers()->get([
    'Http-Status-Code',
    'Http-Status-Message',
]);

$batch = new Batch([
    'max_concurrent_requests' => 5,
]);

$batch->addRequest(new Request());

$batch->addRequests([
    new Request(),
    new Request(),
    new Request(),
]);

$batch->curlOptions()->set([
    CURLOPT_CUSTOMREQUEST  => 'GET',
    CURLOPT_PORT           => 80,
]);

$batch->execute();
foreach ($batch->getRequests() as $requestIndex => $request) {
    foreach ($batch->getResponses() as $responseIndex => $response) {
        // Skip if request and response index does not match
        if ($requestIndex != $responseIndex) {
            continue;
        }

        // ...
    }
}
bash
composer