PHP code example of sauls / xkcd-api-client

1. Go to this page and download the library: Download sauls/xkcd-api-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sauls / xkcd-api-client example snippets


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

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

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

$client = new Client();

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

$client->setCache($cache);

$latestInfo = $client->getLatest();