sauls/xkcd-api-client

Sauls xkcd.com api client

v1.0.0 2018-03-07 17:30 UTC

This package is auto-updated.

Last update: 2024-03-06 08:17:57 UTC


README

Build Status Packagist Total Downloads Coverage Status Scrutinizer Code Quality License

Sauls xkcd.com api client.

Requirements

PHP >= 7.2

Installation

Using composer

$ composer require sauls/xkcd-api-client

Apppend the composer.json file manually

{
    "require": {
        "sauls/xkcd-api-client": "^1.0"
    }
}

Client configuration

Client default configuration that can be overridden by passing new values on client creation.

[
    'client' => [
        'url' => [
            'latest' => '/info.0.json',
            'comic' => '/{num}/info.0.json',
        ],
        'cache' => [
            'prefix' => '__xkcd__',
            'ttl' => 720,
        ],
    ],
    'http_client' => [
        'base_uri' => 'http://xkcd.com',
    ],
]

The info object

This object is returned when you call the getLatest, getRandom and get($num) methods

class Info
{
    private $month;
    private $num;
    private $link;
    private $year;
    private $news;
    private $safeTitle;
    private $transcript;
    private $alt;
    private $img;
    private $title;
    private $day;
    
    /* getters and setters */
}

Usage without cache

Sauls\Component\xkcd\Api\Client\Client;

$client = new Client();

$latestInfo = $client->getLatest(); // returns latest xkcd.com comic info
$randomInfo = $client->getRandom(); // returns random xkcd.com comic info 
$concreteInfo = $client->get(614); // returns concrete xkcd.com comic info

Usage with cache

Uses symfony/cache component to cache the responses. For how to configure the cache see symfony/cache component documentation

Sauls\Component\xkcd\Api\Client\Client;

$client = new Client();

$cache = new FilesystemCache(/* parameters */);

$client->setCache($cache);

$latestInfo = $client->getLatest(); 

Exceptions

  • ComicNotFoundException - is thrown when requested comic was not found e.g. the xkcd api returns 404 response
  • ServiceDownException - is thrown on all other response statuses
  • XkcdClientException - is thrown when there is something wrong with your configuration or when errors occur.