PHP code example of stil / curl-easy

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

    

stil / curl-easy example snippets



// We will check current Bitcoin price via API.
$request = new \cURL\Request('https://bitpay.com/rates/USD');
$request->getOptions()
    ->set(CURLOPT_TIMEOUT, 5)
    ->set(CURLOPT_RETURNTRANSFER, true);
$response = $request->send();
$feed = json_decode($response->getContent(), true);
echo "Current Bitcoin price: " . $feed['data']['rate'] . " " . $feed['data']['code'] . "\n";


// We will check current Bitcoin price via API.
$request = new \cURL\Request('https://bitpay.com/rates/USD');
$request->getOptions()
    ->set(CURLOPT_TIMEOUT, 5)
    ->set(CURLOPT_RETURNTRANSFER, true);
$request->addListener('complete', function (\cURL\Event $event) {
    $response = $event->response;
    $feed = json_decode($response->getContent(), true);
    echo "\nCurrent Bitcoin price: " . $feed['data']['rate'] . " " . $feed['data']['code'] . "\n";
});

while ($request->socketPerform()) {
    usleep(1000);
    echo '*';
}


// We will download Bitcoin rates for both USD and EUR in parallel.

// Init requests queue.
$queue = new \cURL\RequestsQueue;
// Set default options for all requests in queue.
$queue->getDefaultOptions()
    ->set(CURLOPT_TIMEOUT, 5)
    ->set(CURLOPT_RETURNTRANSFER, true);
// Set function to execute when request is complete.
$queue->addListener('complete', function (\cURL\Event $event) {
    $response = $event->response;
    $json = $response->getContent(); // Returns content of response
    $feed = json_decode($json, true);
    echo "Current Bitcoin price: " . $feed['data']['rate'] . " " . $feed['data']['code'] . "\n";
});

$request = new \cURL\Request('https://bitpay.com/rates/USD');
// Add request to queue
$queue->attach($request);

$request = new \cURL\Request('https://bitpay.com/rates/EUR');
$queue->attach($request);

// Execute queue
$timeStart = microtime(true);
$queue->send();
$elapsedMs = (microtime(true) - $timeStart) * 1000;
echo 'Elapsed time: ' . round($elapsedMs) . " ms\n";


// We will download Bitcoin rates for both USD and EUR in parallel.

// Init requests queue.
$queue = new \cURL\RequestsQueue;
// Set default options for all requests in queue.
$queue->getDefaultOptions()
    ->set(CURLOPT_TIMEOUT, 5)
    ->set(CURLOPT_RETURNTRANSFER, true);
// Set function to execute when request is complete.
$queue->addListener('complete', function (\cURL\Event $event) {
    $response = $event->response;
    $json = $response->getContent(); // Returns content of response
    $feed = json_decode($json, true);
    echo "\nCurrent Bitcoin price: " . $feed['data']['rate'] . " " . $feed['data']['code'] . "\n";
});

$request = new \cURL\Request('https://bitpay.com/rates/USD');
// Add request to queue
$queue->attach($request);

$request = new \cURL\Request('https://bitpay.com/rates/EUR');
$queue->attach($request);

// Execute queue
$timeStart = microtime(true);
while ($queue->socketPerform()) {
    usleep(1000);
    echo '*';
}
$elapsedMs = (microtime(true) - $timeStart) * 1000;
echo 'Elapsed time: ' . round($elapsedMs) . " ms\n";

$requests = [];
$currencies = ['USD', 'EUR', 'JPY', 'CNY'];
foreach ($currencies as $code) {
    $requests[] = new \cURL\Request('https://bitpay.com/rates/' . $code);
}

$queue = new \cURL\RequestsQueue;
$queue
    ->getDefaultOptions()
    ->set(CURLOPT_RETURNTRANSFER, true);

$queue->addListener('complete', function (\cURL\Event $event) use (&$requests) {
    $response = $event->response;
    $json = $response->getContent(); // Returns content of response
    $feed = json_decode($json, true);
    echo "Current Bitcoin price: " . $feed['data']['rate'] . " " . $feed['data']['code'] . "\n";

    if ($next = array_pop($requests)) {
        $event->queue->attach($next);
    }
});

$queue->attach(array_pop($requests));
$queue->attach(array_pop($requests));
$queue->send();

$opts = new \cURL\Options;

$opts->set(CURLOPT_URL, $url);
// it is equivalent to
// $opts->setUrl($url);

$opts->set(CURLOPT_RETURNTRANSFER, true);
// it is equivalent to
// $opts->setReturnTransfer(true);
// or
// $opts->setReTURNTranSFER(true);
// character case does not matter

$opts->set(CURLOPT_TIMEOUT, 5);
// it is equivalent to
// $opts->setTimeout(5);

// then you can assign options to Request

$request = new \cURL\Request;
$request->setOptions($opts);

// or make it default in RequestsQueue

$queue = new \cURL\RequestsQueue;
$queue->setDefaultOptions($opts);

$request = new \cURL\Request('http://non-existsing-page/');
$response = $request->send();

if ($response->hasError()) {
    $error = $response->getError();
    echo 'Error code: ' . $error->getCode() . "\n";
    echo 'Message: "' . $error->getMessage() . '"' . "\n";
}