All Projects → stil → Curl Easy

stil / Curl Easy

Licence: mit
cURL wrapper for PHP. Supports parallel and non-blocking requests. For high speed crawling, see stil/curl-robot

Projects that are alternatives of or similar to Curl Easy

Parallel Ssh
Asynchronous parallel SSH client library.
Stars: ✭ 864 (+190.91%)
Mutual labels:  asynchronous, parallel
Future.apply
🚀 R package: future.apply - Apply Function to Elements in Parallel using Futures
Stars: ✭ 159 (-46.46%)
Mutual labels:  asynchronous, parallel
Read Multiple Files
Read multiple files Observable way
Stars: ✭ 13 (-95.62%)
Mutual labels:  asynchronous, parallel
Parallel
Parallel processing for PHP based on Amp.
Stars: ✭ 478 (+60.94%)
Mutual labels:  parallel, multiprocessing
Galaxy
Galaxy is an asynchronous parallel visualization ray tracer for performant rendering in distributed computing environments. Galaxy builds upon Intel OSPRay and Intel Embree, including ray queueing and sending logic inspired by TACC GraviT.
Stars: ✭ 18 (-93.94%)
Mutual labels:  asynchronous, parallel
Asyncro
⛵️ Beautiful Array utilities for ESnext async/await ~
Stars: ✭ 487 (+63.97%)
Mutual labels:  asynchronous, parallel
Rubico
[a]synchronous functional programming
Stars: ✭ 133 (-55.22%)
Mutual labels:  asynchronous, parallel
Cloe
Cloe programming language
Stars: ✭ 398 (+34.01%)
Mutual labels:  asynchronous, parallel
Parallel-NDJSON-Reader
Parallel NDJSON Reader for Python
Stars: ✭ 13 (-95.62%)
Mutual labels:  multiprocessing, parallel
spellbook
Functional library for Javascript
Stars: ✭ 14 (-95.29%)
Mutual labels:  asynchronous, parallel
Index
Metarhia educational program index 📖
Stars: ✭ 2,045 (+588.55%)
Mutual labels:  asynchronous, parallel
cephgeorep
An efficient unidirectional remote backup daemon for CephFS.
Stars: ✭ 27 (-90.91%)
Mutual labels:  asynchronous, parallel
Metasync
Asynchronous Programming Library for JavaScript & Node.js
Stars: ✭ 164 (-44.78%)
Mutual labels:  asynchronous, parallel
do
Simplest way to manage asynchronicity
Stars: ✭ 33 (-88.89%)
Mutual labels:  asynchronous, parallel
MultiHttp
This is a high performance , very useful multi-curl tool written in php. 一个超级好用的并发CURL工具!!!(httpful,restful, concurrency)
Stars: ✭ 79 (-73.4%)
Mutual labels:  curl, parallel
Korio
Korio: Kotlin cORoutines I/O : Virtual File System + Async/Sync Streams + Async TCP Client/Server + WebSockets for Multiplatform Kotlin 1.3
Stars: ✭ 282 (-5.05%)
Mutual labels:  asynchronous
Bb8
Full-featured async (tokio-based) postgres connection pool (like r2d2)
Stars: ✭ 287 (-3.37%)
Mutual labels:  asynchronous
Multiprocessing Logging
Handler for logging from multiple processes
Stars: ✭ 277 (-6.73%)
Mutual labels:  multiprocessing
Awesome Distributed Deep Learning
A curated list of awesome Distributed Deep Learning resources.
Stars: ✭ 277 (-6.73%)
Mutual labels:  asynchronous
S3 Parallel Put
Parallel uploads to Amazon AWS S3
Stars: ✭ 294 (-1.01%)
Mutual labels:  parallel

Travis Latest Stable Version Total Downloads License

Table of contents

Introduction

Description

This is small but powerful and robust library which speeds the things up. If you are tired of using PHP cURL extension with its procedural interface, but you want also keep control about script execution - it's great choice for you! If you need high speed crawling in your project, you might be interested in stil/curl-easy extension - stil/curl-robot.

Main features

  • widely unit tested.
  • lightweight library with moderate level interface. It's not all-in-one library.
  • parallel/asynchronous connections with very simple interface.
  • attaching/detaching requests in parallel on run time!
  • support for callbacks, so you can control execution process.
  • intelligent setters as alternative to CURLOPT_* constants.
  • if you know the cURL php extension, you don't have to learn things from beginning

Installation

In order to use cURL-PHP library you need to install the » libcurl package.

Install this library as Composer package with following command:

composer require stil/curl-easy

Examples

Single blocking request

<?php
// 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";

The above example will output:

Current Bitcoin price: 1999.97 USD

Single non-blocking request

<?php
// 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 '*';
}

The above example will output:

********************
Current Bitcoin price: 1997.48 USD

Requests in parallel

<?php
// 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";

The above example will output:

Current Bitcoin price: 1772.850062 EUR
Current Bitcoin price: 1987.01 USD
Elapsed time: 284 ms

Non-blocking requests in parallel

<?php
// 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";

The above example will output something like that:

*****************************************************************************************************************************************************
Current Bitcoin price: 1772.145208 EUR
************************************************************************
Current Bitcoin price: 1986.22 USD
Elapsed time: 374 ms

Processing queue of multiple requests while having maximum 2 at once executed at the moment

$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();

The above example will output something like that:

Current Bitcoin price: 220861.025 JPY
Current Bitcoin price: 13667.81675 CNY
Current Bitcoin price: 1771.0567 EUR
Current Bitcoin price: 1985 USD

Intelligent Options setting

Replace CURLOPT_* with set*() and you will receive method name. Examples:

$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);

Error handling

You can access cURL error codes in Response class. Examples:

$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";
}

Probably above example will output

Error code: 6
Message: "Could not resolve host: non-existsing-page; Host not found"

You can find all of CURLE_* error codes here.

cURL\Request

Request::__construct

Request::getOptions

Request::setOptions

RequestsQueue::socketPerform

RequestsQueue::socketSelect

Request::send

cURL\RequestQueue

RequestsQueue::__construct

RequestsQueue::getDefaultOptions

RequestsQueue::setDefaultOptions

RequestsQueue::socketPerform

RequestsQueue::socketSelect

RequestsQueue::send

cURL\Response

Response::getContent

Response::getInfo

Response::hasError

Response::getError

cURL\Options

Options::set

Options::toArray

cURL\Error

Error::getCode

Error::getMessage

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].