PHP api wrapper for igdb.

dev-master 2018-12-11 10:51 UTC

README

Introduction

This PHP extension works as a wrapper for interface queries at the IGDB API. It contains a query builder where you can add search criteria.

You must have an IGDB account in order to use the API. You can register a new account at (https://api.igdb.com/).

Project: (https://gitlab.com/jschubert/igdb/)

Installation

Run composer to require this package:

composer require jschubert/igdb:@dev

Usage

Note the parameters (url, key, endpoint) are required.

Default Workflow:

//Create new SearchBuilder object.
$searchBuilder = new SearchBuilder($apiKey);

//Add the endpoint to be requested.
$searchBuilder = $searchBuilder->addEndpoint('games');

//Add the fields you want to return.
$searchBuilder = $searchBuilder->addFields(['id', 'name']);

//Add multiple filters to refine the search.
$searchBuilder = $searchBuilder->addFilter('rating', 'eq', '75');

//Add a limit.
$searchBuilder = $searchBuilder->addLimit('10');

//Add an offset.
$searchBuilder = $searchBuilder->addOffset('0');

//Add an order. (field:direction => popularity:desc)
$searchBuilder = $searchBuilder->addOrder('popularity:desc');

//Trigger the search. It returns an Response object.
$searchBuilder = $searchBuilder->search();

//Decode the response from the server and return an array of objects.
$response = $searchBuilder->get();

Request by id.

//Create new SearchBuilder object.
$searchBuilder = new SearchBuilder($apiKey);

//Add endpoint and search by id.
$response = $searchBuilder
    ->addEndpoint('games')
    ->searchById(1)
    ->get();

Request by search

//Create new SearchBuilder object.
$searchBuilder = new SearchBuilder($apiKey);

//Add endpoint, fields and search needle.
$response = $searchBuilder
    ->addEndpoint('games')
    ->addFields(['id'])
    ->addSearch('witcher')
    ->search()
    ->get();

Request by scroll.

//Create new SearchBuilder object.
$searchBuilder = new SearchBuilder($apiKey);

//Create search and add scroll parameter.
$response = $searchBuilder
    ->addEndpoint('games')
    ->addFields(['id'])
    ->addScroll()
    ->search();

//Get header url for next scroll page.
$nextPage = $response->getResponse()->getHeader('X-Next-Page');

//Clear old criteria parameter and search by scroll.
$response = $searchBuilder
    ->clear()
    ->searchByScroll($nextPage[0]);

Format of response

The data is returned in Json format and converted to an array of PHP objects.

Sample Response:

[
    0 => {
        'id' => '1',
        'name' => 'Witcher'
    },
    1 => {
        'id' => '2',
        'name' => 'Fallout'
    }
]

Run Unit Test

Install phpunit in your environment and run:

$ phpunit

IGDB API

Credits

License

The MIT License (MIT).