sauls / xkcd-api-client
Sauls xkcd.com api client
Installs: 31
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/sauls/xkcd-api-client
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: 2025-10-06 11:57:21 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 returns404responseServiceDownException- is thrown on all other response statusesXkcdClientException- is thrown when there is something wrong with your configuration or when errors occur.