2017 © Pedro Peláez
 

library scylla

Multi curl client

image

retrinko/scylla

Multi curl client

  • Sunday, June 19, 2016
  • by retrinko
  • Repository
  • 1 Watchers
  • 2 Stars
  • 374 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 5 % Grown

The README.md

retrinko/scylla

Scylla is a curl client that can execute multiple requests at the same time., (*1)

Installation

Install the latest version with, (*2)

$ composer require retrinko/scylla

Dependencies

This library requires the php's curl extension to work., (*3)

Logging

You can use your prefered PSR-3 compliant logger with this library (see Basic usage section)., (*4)

Basic usage

Simple request

<?php

use Retrinko\Scylla\Client;
use Retrinko\Scylla\Request\Requests\DefaultRequest;
use Retrinko\Scylla\Util\HttpCodes;

require_once __DIR__.'/../vendor/autoload.php';
date_default_timezone_set('UTC');

$logger = new Monolog\Logger('test');
$logger->pushHandler(new \Monolog\Handler\StreamHandler('php://stdout'));

try
{
    // Instance new client
    $client = new Client();
    // Configure your client
    $client->setLogger($logger);
    // Configure your requests
    $sampleRequest = new DefaultRequest('https://httpbin.org/html');
    $sampleRequest->verificatePeersSSLCertificate(true);
    // Execute requests
    $responsesCollection = $client->exec($sampleRequest);
    // Get current response
    /** @var \Retrinko\Scylla\Response\ResponseInterface $response */
    $response = $responsesCollection->current();
    // Check http code and read response data or error message
    $code = $response->getCode();
    if (false == HttpCodes::isError($code))
    {
        $logger->notice('Execution success!');
        // Get response body
        $resposeBody = $response->getDecodedContent();
        var_dump($resposeBody);
    }
    else
    {
        // Get response message (error)
        $logger->error($response->getMessage());
    }
}
catch (\Exception $e)
{
    $logger->alert($e->getMessage());
}

Multi request

<?php

use Retrinko\Scylla\Client;
use Retrinko\Scylla\Request\Requests\JsonRequest;
use Retrinko\Scylla\Request\RequestsCollection;
use Retrinko\Scylla\Response\Factories\JsonResponsesFactory;
use Retrinko\Scylla\Response\ResponseInterface;
use Retrinko\Scylla\Util\HttpCodes;

require_once __DIR__ . '/../vendor/autoload.php';
date_default_timezone_set('UTC');

$logger = new Monolog\Logger('test');
$logger->pushHandler(new \Monolog\Handler\StreamHandler('php://stdout'));

try
{
    // Instance new client
    $client = new Client();
    // Configure your client
    $client->setLogger($logger);
    $client->setResponsesFactory(new JsonResponsesFactory());
    $client->usePipelining(true);

    // Create requests
    $urls = ['ip' => 'https://httpbin.org/ip',
             'userAgent' => 'https://httpbin.org/user-agent',
             'get' => 'https://httpbin.org/get'];
    $requestsCollection = new RequestsCollection();
    foreach ($urls as $id => $url)
    {
        // Configure request
        $request = new JsonRequest($url);
        $request->setId($id);
        // Add request to requests collecction
        $requestsCollection->add($request);
    }

    // Execute requests
    $responsesCollection = $client->exec($requestsCollection);
    // Get responses
    foreach ($responsesCollection as $requestId => $response)
    {
        /** @var ResponseInterface $response */
        // Check http code and read response data or error message
        $code = $response->getCode();
        if (false == HttpCodes::isError($code))
        {
            $logger->notice('Execution success!', ['requestId' => $requestId]);
            // Get response body
            $data = $response->getDecodedContent();
            var_dump($data);
        }
        else
        {
            // Get response message (error)
            $logger->error($response->getMessage(), ['requestId' => $requestId]);
        }
    }

}
catch (\Exception $e)
{
    $logger->alert($e->getMessage());
}

The Versions

19/06 2016

dev-master

9999999-dev

Multi curl client

  Sources   Download

MIT

The Requires

 

The Development Requires

by Miguel Ángel Graña

curl client multi

19/06 2016

v0.3

0.3.0.0

Multi curl client

  Sources   Download

MIT

The Requires

 

The Development Requires

by Miguel Ángel Graña

curl client multi

12/04 2016

v0.2

0.2.0.0

Multi curl client

  Sources   Download

MIT

The Requires

 

The Development Requires

by Miguel Ángel Graña

curl client multi

11/04 2016

0.1

0.1.0.0

Multi curl client

  Sources   Download

MIT

The Requires

 

The Development Requires

by Miguel Ángel Graña

curl client multi