sauls / xkcd-api-client
Sauls xkcd.com api client
v1.0.0
2018-03-07 17:30 UTC
Requires
- php: >=7.2
- guzzlehttp/guzzle: ^6.0
- sauls/helpers: ^1.0
- sauls/options-resolver: ^1.0
- symfony/cache: ^4.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2024-12-06 09:45:48 UTC
README
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 returns404
responseServiceDownException
- is thrown on all other response statusesXkcdClientException
- is thrown when there is something wrong with your configuration or when errors occur.