GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - lessmore92/api-consumer: Build REST API consumer (client) easier than ever

BranchesTags
Open more actions menu

Latest commit

 

History

18 Commits
18 Commits

Folders and files

NameName
Last commit message
Last commit date
src
src
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
.travis.yml
.travis.yml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
composer.json
composer.json
 
 
phpunit.xml.dist
phpunit.xml.dist
 
 

Repository files navigation

api-consumer

Build REST API consumer (client) easier than ever

Installing

Easily install it through Composer:

composer require lessmore92/api-consumer

Support

PHP >=5.5

Usage

Easily extends your class from ApiConsumer and impalements ConfigApi method, and builds your awesome API Client.

Example:

Below is the minimum requirement to start building API client: ConfigApi

use Lessmore92\ApiConsumer\ApiConsumer;
use Lessmore92\ApiConsumer\Builders\ApiBuilder;

class MyApi extends ApiConsumer
{
    /**
     * @return ApiBuilder
     */
    protected function ConfigApi()
    {
        $api = new ApiBuilder();
        $api->setHeaderApiKey('API-TOKEN','X-API-Key');
        $api->setBaseUrl('https://MY-API-BASE-URL.COM/');
        return $api;
    }
}

If the api key needs to be placed in the query string use setQueryApiKey instead of setHeaderApiKey for example $api->setQueryApiKey('API-TOKEN','TOKEN');


The Magic of $this->Request()

By inheriting ApiConsumer your class will be able to utilize $this->Request() method, which supports chaining. With $this->Request() you will be able to access all the features and functionalities to make your request.

Define your first method to receive data from api: Simple GET Request

To specify an endpoint to be called, you must use ->Endpoint() method. After that by chaining ->Get() method at the end, REQUEST METHOD is specified as GET.

use Lessmore92\ApiConsumer\ApiConsumer;
use Lessmore92\ApiConsumer\Builders\ApiBuilder;

class MyApi extends ApiConsumer
{
    /**
     * @return ApiBuilder
     */
    protected function ConfigApi()
    {
        $api = new ApiBuilder();
        $api->setHeaderApiKey('API-TOKEN','X-API-Key');
        $api->setBaseUrl('https://MY-API-BASE-URL.COM/');
        return $api;
    }

    public function Users()
    {
        $users = $this->Request()
                      ->Endpoint('users')
                      ->Get()
        ;

        return $users->body;
    }
}

In the above example we defined a method to GET Users list from server.

By calling Users() method, in fact we are getting https://MY-API-BASE-URL.COM/users


Make another request: Add Query String

To pass data in query string (e.g to search, order or filter) you can use ->AddQueryString() method.

public function SearchUsers($search)
{
    $users = $this->Request()
                  ->Endpoint('users')
                  ->AddQueryString('search', $search)
                  ->Get()
    ;

    return $users->json_body;
}

In the above example we defined a method to search in users.

By calling SearchUsers('alex') method, in fact we are getting https://MY-API-BASE-URL.COM/users?search=alex


Make another request: Get result in json format

To receive data as json, you must use ->AcceptJson() method.

public function SearchUsers($search)
{
    $users = $this->Request()
                  ->Endpoint('users')
                  ->AcceptJson()
                  ->Get()
    ;

    return $users->json_body;
}

As you can see in the code above, by chaining ->AcceptJson() in request we are telling to api server that we accept json, then in the return line we are returning a json formatted search result.

For json data format, your api server must be able to provide json formatted response and support HEADER 'accept : application/json'

About

Build REST API consumer (client) easier than ever

Topics

api-clientapi-consumerapi-restclientconsumerjsonjson-apijson-parserjsonapiphpreceive-datarestrest-apirest-clientrestful-api

Resources

Readme
MIT license
Activity

Stars

4 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.