A RESTful API client around PHP Guzzle.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Jose Quintana de985016e2 Update README.md 2 years ago
src Fix request payload and query 2 years ago
tests php-cs-fixer with PSR2 2 years ago
.editorconfig init 2 years ago
.gitignore init 2 years ago
.travis.yml fix php version test 2 years ago
LICENSE.md init 2 years ago
Makefile init 2 years ago
README.md Update README.md 2 years ago
composer.json init 2 years ago
composer.lock init 2 years ago
phpunit.xml init 2 years ago

README.md

Guztav

A small RESTful API client around PHP Guzzle.

Build Status Latest Stable Version Latest Unstable Version Total Downloads License

Features

  • It’s only a wrapper around Guzzle. It means that you can use the same API like: Client, Request and Response objects.
  • Some helper functions for more friendly Response data handling like : $response->toArray(), $response->toString(), etc.
  • You can define your BASE_URI and ACCESS_TOKEN settings via .env file. For example using Dotenv package (optional).

Install

composer require joseluisq/guztav

Usage

Settings via .env file

# Define your API settings
GUZTAV_BASE_URI='http://localhost:8001/api/v1/'
GUZTAV_ACCESS_TOKEN='eyJ0eXBiOiJKV1QiLCN0bx.....'
  • GUZTAV_BASE_URI : The same Guzzle base_uri param for Client settings.
  • GUZTAV_ACCESS_TOKEN : It will be to added to the current header like Authorization: Bearer ... (optional)

Settings via constructor

You can also pass the same Guzzle options for Client object.

<?php

use Guztav\Client;

// Setting the client
$client = new \Guztav\Client([
    // Define these params if you don't use some .env file
    'base_uri' => 'http://localhost:8001/api/v1/',
    'access_token' => 'eyJ0eXBiOiJKV1QiLCN0bx.....', // Optional

    // More options...
    'headers' => ['Accept' => 'application/json'],
    'timeout'  => 2.0,
    ...
]);

Making a request

<?php

use Guztav\Client;

$client = new \Guztav\Client();

// My GET request
$response = $client->get('client/1', [
    'query' => ['a' => 1, 'b' => 2]
]);
$string = $response->toString();


// My JSON POST request
$params = [
    'name' => 'Octocat',
    'email' => 'octo.cat@github.com',
];
$array = $client->post('client', [
    'json' => $params
])->toArray();

Note: By now, Guztav supports application/json response data only.

License

MIT license

© 2017 José Luis Quintana