Skip to content

About

Official PHP client for the ISBNLab book metadata API: books by ISBN, authors, publishers, BISAC genres, covers, and ISBN conversion.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

ISBNLab PHP client

The official PHP client for the ISBNLab book metadata API. Look up a book by ISBN and get its title, authors, publishers, series, BISAC genres, formats, page count, description and every other edition, from a catalog of more than 100 million editions. It also validates and converts ISBN-10, ISBN-13, EAN and Amazon ASIN.

Install

composer require isbnlab/isbnlab-php

PHP 8.1 or later, with the curl, json and mbstring extensions.

Get an API token

Create a free ISBNLab account and copy your API token from your profile. The Free plan includes 1,000 calls a day; see pricing for more.

Quick start

<?php

require 'vendor/autoload.php';

use GuzzleHttp\Client;
use ISBNLab\Api\BooksApi;
use ISBNLab\Configuration;

$config = Configuration::getDefaultConfiguration()
    ->setAccessToken(getenv('ISBNLAB_API_TOKEN'));

$books = new BooksApi(new Client(), $config);

$book = $books->getBookByIsbn('9780765302304');

echo $book->getTitle(), "\n";                  // The Gathering Storm
echo $book->getAuthors()[0]->getName(), "\n";  // Robert Jordan
echo $book->getPublishDate()?->format('Y'), "\n";

echo $book->getOtherEditionsCount(), " other editions\n";

Many fields are empty for some books, so every getter can return null.

Linked records carry ids you can look up directly (author, publisher, subject and genre lookups need a Standard or Pro plan):

use ISBNLab\Api\AuthorsApi;

$authorId = $book->getAuthors()[0]->getId();
$author = (new AuthorsApi(new Client(), $config))->getAuthor((string) $authorId);

echo $author->getBio(), "\n";

Several books at once

getBooksByIsbns() looks up several ISBN-10s or ISBN-13s in one request: up to 25 on Standard, 100 on Pro and 500 on Business. Each ISBN counts as one call toward your daily limit.

use ISBNLab\Model\BookBatchRequest;

$found = $books->getBooksByIsbns(new BookBatchRequest([
    'isbns' => ['9780765302304', '0-7653-1756-7'],
]));

foreach ($found as $book) {
    echo $book->getIsbn13(), ' ', $book->getTitle(), "\n";
}

The books come back in the order asked, once each, and an ISBN the catalog lacks is left out, so match results by ISBN rather than by position. One invalid ISBN, or more ISBNs than your plan allows, refuses the whole request with a 400; the Free plan gets a 403.

otherEditions is empty in a batch unless the request includes it ('include' => ['otherEditions']), which needs Pro or above; otherEditionsCount is always set. With it, a large batch of popular books can run to tens of megabytes.

Fields that depend on your plan

A book lookup lists the other editions (getOtherEditions(), getPreviousEdition(), getNextEdition()) on the Pro plan. On the Free and Standard plans those fields are empty, getOtherEditionsCount() still gives the number of other editions, and two response headers name what was left out: X-Plan-Restricted-Fields lists the fields and X-Plan-Upgrade links to the plans. PlanRestriction reads them from a ...WithHttpInfo() call:

use ISBNLab\PlanRestriction;

[$book, , $headers] = $books->getBookByIsbnWithHttpInfo('9780765302304');

if (PlanRestriction::isRestricted($headers, 'otherEditions')) {
    echo $book->getOtherEditionsCount(), ' other editions; see ', PlanRestriction::upgradeUrl($headers), "\n";
} else {
    foreach ($book->getOtherEditions() ?? [] as $edition) {
        echo $edition->getIsbn13(), ' ', $edition->getTitle(), "\n";
    }
}

PlanRestriction::fields($headers) returns every field that was left out, for example ['otherEditions', 'previousEdition', 'nextEdition'], or [] when the response is complete.

A cover lookup (Standard or Pro) gives each cover image's URL in getCovers(), and in getSizes() the same image 200, 400 and 800 pixels wide (getW200(), getW400(), getW800()); a width is null when that size is not available.

$cover = $books->getBookCoverByIsbn('9780765302304');
echo $cover->getSizes()[0]->getW400(), "\n";  // https://covers.isbnlab.com/w400/9780765302304.jpg

Errors

Any response other than 2xx throws ISBNLab\ApiException. Its code is the HTTP status:

use ISBNLab\ApiException;

try {
    $book = $books->getBookByIsbn('9780000000000');
} catch (ApiException $e) {
    match ($e->getCode()) {
        404 => print("Not in the catalog\n"),
        429 => print("Daily limit reached\n"),
        403 => print("Your plan does not include this endpoint\n"),
        default => throw $e,
    };
}

What you can call

Class Method Plan
BooksApi getBookByIsbn($isbn), getBookByAsin($asin), getBookById($id) Free
IsbnApi parseIsbn, isbn10ToIsbn13, isbn13ToIsbn10, isbnToEan, eanToIsbn, isbnToAsin, asinToIsbn Free
BooksApi getBookCoverByIsbn($isbn), getBookCoverByAsin($asin) Standard
BooksApi getBooksByIsbns($request): up to 25 ISBNs on Standard, 100 on Pro, 500 on Business Standard
AuthorsApi getAuthor($id), findAuthor($name) Standard
PublishersApi getPublisher($id), findPublisher($name), getPublishersByPrefix($prefix) Standard
SubjectsApi getSubject($id), findSubject($name) Standard
GenresApi getGenre($id), findGenre($nameOrBisacCode) Standard
BooksApi searchBooks($query, $limit, $page, $maxResults) Pro
BooksApi getEditionsByIsbn($isbn), getEditionsByAsin($asin) Pro

Every method also has an ...Async() version that returns a Guzzle promise, and a ...WithHttpInfo() version that also returns the status code and headers (for example the rate-limit headers).

Reference for every method and model: docs/. Field-by-field documentation: API documentation.

Development

lib/ and docs/ are generated from the API's OpenAPI document with OpenAPI Generator, so changes to them are made in the generator and arrive with the next release; please open an issue rather than a pull request for those folders. README.md, composer.json and tests/ are maintained by hand.

composer install
vendor/bin/phpunit

License

MIT. Maintained by ISBNLab, the book metadata API.

About

Official PHP client for the ISBNLab book metadata API: books by ISBN, authors, publishers, BISAC genres, covers, and ISBN conversion.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages