Requester
Simple Requester class to wrap guzzle and the retry subscriber, (*1)
, (*2)
This package requires PHP >=5.4, (*3)
Installation
Install via composer - edit your composer.json
to require the package., (*4)
"require": {
"pulkitjalan/requester": "2.*"
}
Then run composer update
in your terminal to pull it in., (*5)
Laravel 5
There is a Laravel 5 service provider and facade available., (*6)
Add the following to the providers
array in your config/app.php
, (*7)
'PulkitJalan\Requester\RequesterServiceProvider'
Next add the following to the aliases
array in your config/app.php
, (*8)
'Requester' => 'PulkitJalan\Requester\Facades\Requester'
Next run php artisan vendor:publish --provider="pulkitjalan\requester\RequesterServiceProvider" --tag="config"
to publish the config file., (*9)
Looking for a Laravel 4 compatible version?
Checkout the 1.0 branch, (*10)
Usage
The requester class has a dependency of guzzle
and takes in an instance of guzzle
as the first param., (*11)
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., (*12)
Available request methods: get
, head
, delete
, put
, patch
, post
, options
, (*13)
<?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., (*14)
// 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, (*15)
// ssl check disabled
$requester->url('example.com')->verify(false)->get();
Use http instead of https, (*16)
// disable https and use http
$requester->url('example.com')->secure(false)->get();
// use http
$requester->url('http://example.com')->get();
Create a Post request, (*17)
// 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., (*18)
Performing asynchronous requests, (*19)
// 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., (*20)
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');