yangusik/shikimori-api-php

A PHP wrapper for Shikimori API

v0.5.2 2022-08-17 14:02 UTC

This package is auto-updated.

Last update: 2024-05-17 17:54:03 UTC


README

🔌 A PHP wrapper for the http://shikimori.one API

Packagist build Coverage Status

This is a PHP wrapper for Shikimori Web API. It includes the following:

  • Helper methods for all API endpoints
  • Authorization Code.
  • Automatic refreshing of access tokens.
  • Automatic retry of rate limited requests.
  • PSR-4 autoloading support.

Requirements

Installation

Install it using Composer:

composer require yangusik/shikimori-api-php

Usage

Before using the Shikimori API, you'll need to create an app at Shikimori app.

Simple example displaying a user's profile:

require 'vendor/autoload.php';

$session = new ShikimoriAPI\Session(
    'CLIENT_ID',
    'CLIENT_SECRET',
    'REDIRECT_URI'
);

$api = new ShikimoriAPI\ShikimoriAPI(['auto_refresh' => false]);

if (isset($_GET['code'])) {
    $session->requestAccessToken($_GET['code']);
    
    // use session to make auto-refresh token auto_refresh = true
    $api->setSession($session);
    // or just a token if you don't need auto-refresh token 
    $api->setAccessToken($session->getAccessToken());

    print_r($api->whoami());
} else {
    $options = [
        'scope' => [
            'user-read-email',
        ],
    ];

    header('Location: ' . $session->getAuthorizeUrl($options));
    die();
}

Some resources do not require a token, such as anime

require 'vendor/autoload.php';

$animes = new \ShikimoriAPI\Resources\Animes();

$animeList = $animes->getAll([['order' => 'popularity', 'status' => 'latest', 'limit' => 50]]);

print_r($animeList); 
/** 
 * [
 *  ["id" => 1069,
 *  "name" => "Chou Denji Machine Voltes V",
 *  "russian" => "Суперэлектромагнитная машина Вольтес V",
 *  "image": { ...

All options for getAll can see at documentation Shikimori.

If the resource requires a token, such as Dialog

require 'vendor/autoload.php';

$api = new \ShikimoriAPI\ShikimoriAPI();
$api->setAccessToken('TOKEN');

$dialog = new \ShikimoriAPI\Resources\Dialogs($api);
print_r($dialog->getAll());

// or 

$session = new ShikimoriAPI\Session(
    'CLIENT_ID',
    'CLIENT_SECRET',
    'REDIRECT_URI'
);
$session->setAccessToken('TOKEN');
$session->setRefreshToken('TOKEN');

$api = new ShikimoriAPI\ShikimoriAPI(['auto_refresh' => true]);
$api->setSession($session);

$dialog = new \ShikimoriAPI\Resources\Dialogs($api);
print_r($dialog->getAll());

For more instructions and examples, check out the documentation. (soon)

License

MIT license. Please see LICENSE for more info.