This component helps you to create a client for an API powered by Neosolva.
The authentication resides on Basic HTTP authentication. The username and the API key is provided by your sales partner.
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
$ composer require neosolva/php-api-client
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
The class Neosolva\Component\Api\Client
extends class GuzzleHttp\Client
.
The static method create()
helps you to configure the client.
Please see documentation of Guzzle client to now khow to use the client.
require_once 'vendor/autoload.php';
use Neosolva\Component\Api\Client; # extends GuzzleHttp\Client
$client = Client::create('https://...', 'username', 'password');
#
# GET operation
#
$response = $client->get('/foo');
#
# POST operation
#
$data = [
'foo' => 'bar',
'baz' => 'qux'
];
$response = $client->post('/bar', [
'json' => $data
]);
The response is an instance of interface Psr\Http\Message\ResponseInterface
.
All API's powered by Neosolva returns contents as JSON. The client provides the method decode()
to
get decoded data from a response:
$data = $client->decode($response, false); # array|bool|float|int|object|string|null
$data = $client->decode($response, true); # array
To get associative results as array, use the shortcut method decodeData()
:
$data = $client->decodeData($response); # array
The client provides a shortcut to retrieve the configured base URI:
$baseUri = $client->getBaseUri(); # string
- Added method
Client::decodeData()
- Updated Guzzle package version from
"guzzlehttp/guzzle": "^7.0"
to"guzzlehttp/guzzle": "^6.0 || ^7.0"