HTTP Client
The HTTP(S) Client is a standalone package that uses your PHP code to make requests. Built on Laravel (Doesn't require the entire framework, just its component) expressive HTTP shell, it allows you to customize things like authorization, retries, and more.
Install
Go to the project directory and run the command:
$ composer require sajya/client
Usage
use Illuminate\Support\Facades\Http;
use Sajya\Client\Client;
// Create the JSON RPC client
$client = new Client(Http::baseUrl('http://localhost:8000/api/v1/endpoint'));
// Call the 'tennis' method on the server
$response = $client->execute('tennis@ping');
// Print the response from the server
$response->result(); // pong
By default, the request identifier will be generated using the UUID, you can get it by calling the id()
method
$response->id();
To get the result of an error, you need to call the error()
method
$response->error();
Parameters
Example with positional parameters:
$response = $client->execute('tennis@ping', [3, 5]);
Example with named arguments:
$response = $client->execute('tennis@ping', ['end' => 10, 'start' => 1]);
Batch requests
Call several procedures in a single HTTP request:
$responses = $client->batch(function (Client $client) {
$client->execute('tennis@ping');
$client->execute('tennis@ping');
});
Notify requests
$client->notify('procedure@method');