pulkitjalan/requester

This package is abandoned and no longer maintained. No replacement package was suggested.

Requester class to wrap guzzle and retry subscriber

1.1.0 2015-02-26 22:17 UTC

This package is auto-updated.

Last update: 2019-09-06 07:06:21 UTC


README

Simple Requester class to wrap guzzle and the retry subscriber

Build Status Scrutinizer Code Quality Coverage Status License Latest Version Total Downloads

This package requires PHP >=5.4

Installation

Install via composer - edit your composer.json to require the package.

"require": {
    "pulkitjalan/requester": "2.*"
}

Then run composer update in your terminal to pull it in.

Laravel 5

There is a Laravel 5 service provider and facade available.

Add the following to the providers array in your config/app.php

'PulkitJalan\Requester\RequesterServiceProvider'

Next add the following to the aliases array in your config/app.php

'Requester' => 'PulkitJalan\Requester\Facades\Requester'

Next run php artisan vendor:publish --provider="pulkitjalan\requester\RequesterServiceProvider" --tag="config" to publish the config file.

Looking for a Laravel 4 compatible version?

Checkout the 1.0 branch

Usage

The requester class has a dependency of guzzle and takes in an instance of guzzle as the first param.

This package also uses a few guzzle subscribers. https://github.com/guzzle/retry-subscriber for retry functionality and https://github.com/guzzle/log-subscriber for logging.

Available request methods: get, head, delete, put, patch, post, options

<?php

use PulkitJalan\Requester\Requester;
use GuzzleHttp\Client as GuzzleClient;

$requester = new Requester(new GuzzleClient());

// simple get request
$requester->url('example.com')->get();

Altering the default retry behaviour. See retry-subscriber for more info.

// retry 10 times, with a 1 second wait on a 503 error
$requester->url('example.com')->retry(10)->every(1000)->on([503])->get();

// disabling retry
$requester->url('example.com')->retry(false)->get();

Disabling ssl check

// ssl check disabled
$requester->url('example.com')->verify(false)->get();

Use http instead of https

// disable https and use http
$requester->url('example.com')->secure(false)->get();

// use http
$requester->url('http://example.com')->get();

Create a Post request

// Create a post request
$requester->url('example.com/update/1')->post([
    'body' => [
        'title' => 'some title'
    ]
]);

// Upload a file
$requester->url('example.com/upload')->addFile('/tmp/image.jpg')->post([
    'body' => [
        'title' => 'Some image',
        'description' => 'Some image description'
    ]
]);

Guzzle 5 uses RingPHP and has the added functionality of performing request asynchronously.

Performing asynchronous requests

// Create a post request
$response = $requester->url('example.com')->async(true)->get();

// Use the response asynchronously
$this->response = $response->then(function ($response) {
    return $response->getBody();
});

// Use the response synchronously
$this->response = $response->getBody();

Logging guzzle requests to file. See log-subscriber for more info.

use PulkitJalan\Requester\Requester;
use GuzzleHttp\Client as GuzzleClient;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('/path/to/your.log', Logger::INFO));

$requester = new Requester(new GuzzleClient());
$requester->addLogger($log);

// request and response logged to file
$requester->url('example.com')->get();

// Use the second param to update the format
$requester = new Requester(new GuzzleClient());
$requester->addLogger($log, 'DEBUG');