PHPackages                             nahid/apiz - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. nahid/apiz

ActivePhp[API Development](/categories/api)

nahid/apiz
==========

Apiz is a boilerplate for REST API HTTP call manager

v5.0.0(1y ago)6011.6k13[1 issues](https://github.com/nahid/apiz/issues)3MITPHPPHP &gt;=7.4

Since Jul 18Pushed 1y ago3 watchersCompare

[ Source](https://github.com/nahid/apiz)[ Packagist](https://packagist.org/packages/nahid/apiz)[ RSS](/packages/nahid-apiz/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (25)Used By (3)

APIZ
====

[](#apiz)

APIZ is a PHP API Client Development Kit, it helps you to manage HTTP API call in OOP way. You can easily handle and isolate all kinds of REST API calls and their responses by using this package.

Requirements
------------

[](#requirements)

- PHP &gt;= 5.5.9

Installations
-------------

[](#installations)

```
composer require nahid/apiz
```

Configurations
--------------

[](#configurations)

There are no extra configurations for this package.

Usage
-----

[](#usage)

Lets see an example to consume API from .

Suppose you need to create several API services for your project. Your service directory is `app/Services`. Now we are going to develop a service for  and make a class file `ReqResApiService.php`which will extend `\Apiz\AbstractApi` class.

```
namespace App\Services;

use Apiz\AbstractApi;

class ReqResApiService extends AbstractApi
{
    protected function getBaseURL()
    {
        return 'https://reqres.in';
    }
}
```

`getBaseURL()` is an abstract method of the `AbstractApi` class. You need to override this method to set the proper base URL for your API.

Few APIs have a common prefix in their URL. Like, here `reqres.in` have a prefix `api` on every endpoint. So, we'll override the `getPrefix()` method to define the Prefix.

```
namespace App\Services;

use Apiz\AbstractApi;

class ReqResApiService extends AbstractApi
{
    protected function getBaseURL()
    {
        return 'https://reqres.in';
    }

    protected function getPrefix()
    {
        return 'api';
    }
}
```

Now let's make a method to get all users info.

```
namespace App\Services;

use Apiz\AbstractApi;

class ReqResApiService extends AbstractApi
{
    protected function getBaseURL()
    {
        return 'https://reqres.in';
    }

    protected function getPrefix()
    {
        return 'api';
    }

    public function getAllUsers()
    {
        $response = $this->get('users');

        if ($response->getStatusCode() === 200) {
            return $response()->toArray();
        }

        return [];
    }
}
```

So, we are basically making a `GET` request to the URL `https://reqres.in/api/users`.

See, how easy it is to manage an API now?

Let's see another example.

Post Request with Form Params
-----------------------------

[](#post-request-with-form-params)

```
public function createUser(array $data)
{
    $response = $this->withFormParams($data)
            ->post('create');

    if ($response->getStatusCode() === 201) {
        return $response()->toArray();
    }

    return null;
}
```

Default Headers
---------------

[](#default-headers)

Sometimes we need to bind some headers with all the requests. Suppose if you want to deal with the Github API, you have to send `access_token` in every request with the headers. So APIZ provide you a handy way to deal with this problem. Just override `AbstractApi::getDefaultHeaders()`.

```
protected function getDefaultHeaders()
{
    return [
        'access_token' => $_ENV['GITHUB_ACCESS_TOKEN'],
    ];
}
```

Cool, right?

You can easily use all HTTP verbs like `get`, `post` etc. It's totally hassle free. See more examples in the [Examples Folder](./Examples).

Query over Response Data
------------------------

[](#query-over-response-data)

Sometimes we receive huge payload as a response from the APIs. It's quite daunting to parse proper data from that big payload.

No worries! We're using a powerful Query parser, named [QArray](https://github.com/nahid/qarray) by default to parse and query over the Response data.

Let's see how we can use this parser to parse the response we got for `getAllUsers` method from our previous example.

```
public function getFirstUser()
{
    $users = $this->get('users');
    return $users->query()->from('data')->first();
}
```

We're getting list of users in the `data` key in the response. From that we're collecting the first data. See, how easy it is!

You can find detail usage of the QArray [here](https://github.com/nahid/qarray).

Additionally, there is a secret sauce for you.

If you don't want to query like: `$users->query()`, you can just do it like this: `$users()`. That means the response object is invokable and behave exactly like calling the `query()` method.

You're welcome. :D

Overriding HTTP Client
----------------------

[](#overriding-http-client)

By default we are using `Guzzle` as our HTTP Client. But you are not bound to use this. You can easily use your own PSR7 supported HTTP Client with `Apiz`. Just pass an instance of your HTTP Client to our `setClient()` method. See an example [here](./Examples/API%20with%20Different%20Client).

Here is our GuzzleClient to get an idea how your Client should look like:

```
