2017 © Pedro Peláez
 

library multicurl

Simple library helping with requesting multiple resources at once using multi cUrl.

image

bluepsyduck/multicurl

Simple library helping with requesting multiple resources at once using multi cUrl.

  • Wednesday, November 29, 2017
  • by BluePsyduck
  • Repository
  • 2 Watchers
  • 3 Stars
  • 3,778 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 9 Versions
  • 14 % Grown

The README.md

BluePsyduck's MultiCurl Library

Latest Stable Version Total Downloads License Build Status codecov, (*1)

This library helps with creating and executing multiple requests simultaneously using the Multi-cURL functions of PHP., (*2)

Requirements

  • PHP 7.0 or newer
  • PHP cURL extension

Usage

The main class of the library is the MultiCurlManager, to which you can add as many requests as you like and which will execute them. For each request to be started, create an instance of the Entity\Request class and set its properties as required, and add it to the manager using $manager->addRequest($request). Added requests will immediately be executed, without blocking the script execution., (*3)

To wait for a certain request to finish, call $manager->waitForSingleRequest($request). To wait for all requests to finish, call $manager->waitForAllRequests(). These methods will block script execution until the desired requests are finished., (*4)

Once a request is finished, use $request->getResponse() to get the information of the response. You may want to check $response->getErrorCode() and $response->getErrorMessage() to get any information in case the request has failed., (*5)

The requests offer two callbacks: - onInitialize: This callback is triggered once the underlying cURL request has been initialized. Use this callback to further manipulate the cURL. After this callback, the cURL request gets executed. - onComplete: This callback is triggered once the cURL request finished and the response has been parsed into the entity., (*6)

Examples

Here is a basic example demonstrating the use of the MultiCurlManager:, (*7)

<?php

use BluePsyduck\MultiCurl\MultiCurlManager;
use BluePsyduck\MultiCurl\Entity\Request;

$manager = new MultiCurlManager();

$requestFoo = new Request();
$requestFoo->setUrl('http://localhost/data.php?action=foo');
$manager->addRequest($requestFoo); // Will execute the first request, but will not wait for it to finish.

$requestBar = new Request();
$requestBar->setUrl('http://localhost/data.php?action=bar');

$manager->addRequest($requestBar); // Will execute the second request, having both run parallel.

// Some other code.

$manager->waitForAllRequests(); // Will wait for both requests to be finished.

// Do something with the responses
var_dump($requestFoo->getResponse());
var_dump($requestBar->getResponse());

Here is another example demonstrating limiting the number of parallel requests:, (*8)

<?php 

use BluePsyduck\MultiCurl\MultiCurlManager;
use BluePsyduck\MultiCurl\Entity\Request;

$manager = new MultiCurlManager();
$manager->setNumberOfParallelRequests(4); // Limit number of parallel requests.

for ($i = 0; $i < 16; ++$i) {
    $request = new Request();
    $request->setUrl('http://localhost/data.php?i=' . $i);
    $request->setOnInitializeCallback(function(Request $request) use ($i) {
        echo 'Initialize #' . $i . PHP_EOL;
    });
    $request->setOnCompleteCallback(function(Request $request) use ($i) {
        echo 'Complete #' . $i . PHP_EOL;
    });
    $manager->addRequest($request);
}

// Now 16 requests have been scheduled to be executed, but only 4 requests will run in parallel.
// Once a request finishes, the next one will be executed.

$importantRequest = new Request();
$importantRequest->setUrl('http://localhost/data.php?type=important');
$importantRequest->setOnInitializeCallback(function(Request $request) use ($i) {
    echo 'Initialize important request' . PHP_EOL;
});
$importantRequest->setOnCompleteCallback(function(Request $request) use ($i) {
    echo 'Complete important request' . PHP_EOL;
});
$manager->addRequest($importantRequest); // Important request will not be executed because of the limit.

$manager->waitForSingleRequest($importantRequest); // This will execute the request, ignoring the limit.
// So now actually 5 requests are running in parallel.

echo 'Important request has finished.' . PHP_EOL;

$manager->waitForAllRequests(); // Wait for all the other requests.

The Versions

29/11 2017

dev-master

9999999-dev https://github.com/BluePsyduck/MultiCurl

Simple library helping with requesting multiple resources at once using multi cUrl.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3.0
  • ext-curl *

 

The Development Requires

by Avatar BluePsyduck

curl multicurl bluepsyduck

29/11 2017

2.0.1

2.0.1.0 https://github.com/BluePsyduck/MultiCurl

Simple library helping with requesting multiple resources at once using multi cUrl.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3.0
  • ext-curl *

 

The Development Requires

by Avatar BluePsyduck

curl multicurl bluepsyduck

20/11 2017

2.0.0

2.0.0.0 https://github.com/BluePsyduck/MultiCurl

Simple library helping with requesting multiple resources at once using multi cUrl.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3.0
  • ext-curl *

 

The Development Requires

by Avatar BluePsyduck

curl multicurl bluepsyduck

13/11 2017

1.2.2

1.2.2.0 https://github.com/BluePsyduck/MultiCurl

Simple library helping with requesting multiple resources at once using multi cUrl.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar BluePsyduck

curl multicurl bluepsyduck

14/08 2016

1.2.1

1.2.1.0 https://github.com/BluePsyduck/MultiCurl

Simple library helping with requesting multiple resources at once using multi cUrl.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar BluePsyduck

curl multicurl bluepsyduck

18/05 2016

1.2.0

1.2.0.0 https://github.com/BluePsyduck/MultiCurl

Simple library helping with requesting multiple resources at once using multi cUrl.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar BluePsyduck

curl multicurl bluepsyduck

15/08 2015

1.1.1

1.1.1.0 https://github.com/BluePsyduck/MultiCurl

Simple library helping with requesting multiple resources at once using multi cUrl.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar BluePsyduck

curl multicurl bluepsyduck

12/07 2015

1.1.0

1.1.0.0 https://github.com/BluePsyduck/MultiCurl

Simple library helping with requesting multiple resources at once using multi cUrl.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar BluePsyduck

curl multicurl bluepsyduck

23/03 2015

1.0.0

1.0.0.0 https://github.com/BluePsyduck/MultiCurl

Simple library helping with requesting multiple resources at once using multi cUrl.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar BluePsyduck

curl multicurl bluepsyduck