neosolva/php-api-client

v1.0.2 2020-09-06 14:02 UTC

This package is auto-updated.

Last update: 2024-05-08 20:19:46 UTC


README

Build Status Latest Stable Version Latest Unstable Version Total Downloads

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.

Installation

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.

Usage

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.

Create the client

require_once 'vendor/autoload.php';

use Neosolva\Component\Api\Client; # extends GuzzleHttp\Client

$client = Client::create('https://...', 'username', 'password');

Make a request

# 
# 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.

Decode JSON response

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

Miscellaneous

Retrieve Base URI

The client provides a shortcut to retrieve the configured base URI:

$baseUri = $client->getBaseUri(); # string

Update logs

v1.0.2

  • Added method Client::decodeData()

v1.0.1

  • Updated Guzzle package version from "guzzlehttp/guzzle": "^7.0" to "guzzlehttp/guzzle": "^6.0 || ^7.0"