Sauls xkcd.com api client.
PHP >= 7.2
$ composer require sauls/xkcd-api-client
{
"require": {
"sauls/xkcd-api-client": "^1.0"
}
}
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',
],
]
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 */
}
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
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();
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.