2017 © Pedro Peláez
 

library curl-multi

a simple curl wrapper for the multi handler

image

pbergman/curl-multi

a simple curl wrapper for the multi handler

  • Wednesday, December 27, 2017
  • by pbergman
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

CurlMulti

This a (light) abstract layer around the curl multi for handling asynchronously request., (*1)

The purpose of this library is to make it easier to handle to handle to responses but trying to keep as close to curl as possible. This should keep the api more stable, easier to work with (when familiar with curl) and less code around curl., (*2)

The provided request is therefor not more than an array like you wold register with curl_setopt_array. So urls, header etc. should be registered with the curl methods CURLOPT_URL, CURLOPT_WRITEHEADER, CURLOPT_HTTPHEADER and rest could be found here., (*3)

example

use PBergman\CurlMulti\MultiHandler;
use PBergman\CurlMulti\Request;

$null = fopen("/dev/null", "w");
$handler = new MultiHandler(
    new Request([
        CURLOPT_URL => "http://httpstat.us/200?sleep=5000",
        CURLOPT_FILE  => $null,
    ]),
    new Request([
        CURLOPT_URL => "http://httpstat.us/404",
        CURLOPT_FILE  => $null,
    ])
);

try {
    foreach ($handler->getResponse() as $response) {
        printf(
            "request '%s' finished with code %d in %0.2fs\n",
            $response->getInfo(CURLINFO_EFFECTIVE_URL),
            $response->getInfo(CURLINFO_RESPONSE_CODE),
            $response->getInfo(CURLINFO_TOTAL_TIME)
        );
    }
} finally {
    // register cleanup
    $handler->close();
}

should give a output like:, (*4)

request 'http://httpstat.us/404' finished with code 404 in 0.45s
request 'http://httpstat.us/200?sleep=5000' finished with code 200 in 5.49s

public api

__construct(RequestInterface ...$request): MultiHandler

constructor of MultiHandler accepts request so you could register your request on creating a new instance., (*5)

(new MultiHandler(
    new Request([
        CURLOPT_URL => "http://httpstat.us/200?sleep=5000",
        CURLOPT_FILE  => $null,
    ]),
    new Request([
        CURLOPT_URL => "http://httpstat.us/404",
        CURLOPT_FILE  => $null,
    ])
))->wait();
setOptions(array $options) :bool

set options for multi handler, (*6)

addOption(int $key, $value) :bool

set option for multi handler, (*7)

add(RequestInterface $request): MultiHandler

add request to defined instance., (*8)

defer(): void

register the close function for shutdown., (*9)

init(): void

will setup the curl multi hadle, this will be called when creating a new instance and can be useful when the close method was called., (*10)

getResponse(): \Generator|ResponseInterface[]

returns a finished request and/or block until one is finished., (*11)

wait() : array||ResponseInterface[]

wait till all request are finished, (*12)

The Versions

27/12 2017

dev-release/1.x

dev-release/1.x

a simple curl wrapper for the multi handler

  Sources   Download

MIT

The Requires

  • php >=5.6.0
  • ext-curl *

 

by Philip Bergman

curl console array symfony tree

27/12 2017

1.0.0

1.0.0.0

a simple curl wrapper for the multi handler

  Sources   Download

MIT

The Requires

  • php >=5.6.0
  • ext-curl *

 

by Philip Bergman

curl console array symfony tree