PHP Google Books API
vi 10
ISBN

<?php
$base = 'https://www.googleapis.com/';
$path = 'books/v1/volumes';
echo "ISBN : ";
$handle = fopen("php://stdin", "r");
$line = fgets($handle);
$isbn = trim($line);
if (empty($isbn)) {
echo "\n";
exit(2);
}
$response = file_get_contents(
$url = $base . $path . "?" . http_build_query(['q' => "isbn:{$isbn}"]),
);
$json = json_decode($response, true);
if (($json['totalItems'] ?? 0) === 0) {
echo "\n";
exit(1);
}
$book = Book::fromArray($json);
var_dump($book);
class Book {
public function __construct(
public string $title,
public array $authors,
public string $publisher,
public string $publishedDate,
){}
public static function fromArray(array $data): self
{
// google books api
return new self(
title: $data["items"][0]["volumeInfo"]["title"] ?? '',
authors: $data["items"][0]["volumeInfo"]["authors"] ?? [],
publisher: $data["items"][0]["volumeInfo"]["publisher"] ?? '',
publishedDate: $data["items"][0]["volumeInfo"]["publishedDate"] ?? '',
);
}
}