透過解析 Amazon 商品頁面 HTML,以 ISBN-10 取得書名與封面縮圖。注意:Amazon 禁止爬蟲,正式專案請使用 Product Advertising API (PA-API 5.0) 或 Open Library / Google Books API。本文代碼僅供教學參考。
<?php
function getBookFromISBN($isbn) {
$find = '
<?php
function getBookOpenLibrary($isbn) {
$url = "https://openlibrary.org/api/books?bibkeys=ISBN:$isbn&format=json&jscmd=data";
$json = @file_get_contents($url);
if (!$json) return null;
$data = json_decode($json, true);
$key = "ISBN:$isbn";
if (!isset($data[$key])) return null;
$book = $data[$key];
return [
'title' => $book['title'] ?? '未知書名',
'authors' => array_column($book['authors'] ?? [], 'name'),
'cover_l' => $book['cover']['large'] ?? null,
'cover_m' => $book['cover']['medium'] ?? null,
'cover_s' => $book['cover']['small'] ?? null,
'publish_date'=> $book['publish_date'] ?? null,
'pages' => $book['number_of_pages'] ?? null,
];
}
$book = getBookOpenLibrary('9780134685991');
if ($book) {
echo "<img src='{$book['cover_m']}'> {$book['title']} by " . implode(', ', $book['authors']);
}
<?php
function getBookGoogle($isbn, $apiKey) {
$url = "https://www.googleapis.com/books/v1/volumes?q=isbn:$isbn&key=$apiKey";
$json = @file_get_contents($url);
if (!$json) return null;
$data = json_decode($json, true);
if (empty($data['items'])) return null;
$vol = $data['items'][0]['volumeInfo'];
return [
'title' => $vol['title'] ?? '',
'authors' => $vol['authors'] ?? [],
'cover' => $vol['imageLinks']['thumbnail'] ?? null,
'published' => $vol['publishedDate'] ?? null,
'description' => $vol['description'] ?? null,
];
}
<?php
function normalizeISBN($isbn) {
$isbn = preg_replace('/[-\s]/', '', $isbn);
if (strlen($isbn) === 10) return $isbn; // ISBN-10
if (strlen($isbn) === 13) return $isbn; // ISBN-13
return null;
}
function isbn10to13($isbn10) {
$isbn10 = normalizeISBN($isbn10);
if (!$isbn10 || strlen($isbn10) !== 10) return null;
$prefix = '978' . substr($isbn10, 0, 9);
$sum = 0;
for ($i = 0; $i < 12; $i++) {
$sum += (int)$prefix[$i] * ($i % 2 === 0 ? 1 : 3);
}
$check = (10 - ($sum % 10)) % 10;
return $prefix . $check;
}
function isbn13to10($isbn13) {
$isbn13 = normalizeISBN($isbn13);
if (!$isbn13 || strlen($isbn13) !== 13 || substr($isbn13, 0, 3) !== '978') return null;
$core = substr($isbn13, 3, 9);
$sum = 0;
for ($i = 0; $i < 9; $i++) {
$sum += (int)$core[$i] * (10 - $i);
}
$check = $sum % 11;
$checkChar = $check === 10 ? 'X' : (string)$check;
return $core . $checkChar;
}
robots.txt 明確禁止爬蟲,違反可能導致 IP 被封鎖、法律風險。class BookLookupService {
private $cache;
private $googleApiKey;
public function __construct(CacheInterface $cache, string $googleApiKey = '') {
$this->cache = $cache;
$this->googleApiKey = $googleApiKey;
}
public function lookup(string $isbn): ?array {
$isbn = normalizeISBN($isbn);
if (!$isbn) return null;
$cacheKey = "book:$isbn";
if ($cached = $this->cache->get($cacheKey)) return $cached;
// 嘗試 Open Library (免費)
if ($book = $this->tryOpenLibrary($isbn)) {
$this->cache->set($cacheKey, $book, 86400); // 緩存 1 天
return $book;
}
// 回退 Google Books
if ($this->googleApiKey && $book = $this->tryGoogleBooks($isbn)) {
$this->cache->set($cacheKey, $book, 86400);
return $book;
}
return null;
}
// ... tryOpenLibrary, tryGoogleBooks 實作
}
建立合法、可維護、高效能的書誌查詢服務,請採用官方 API + 緩存策略,避免直接解析商業網站 HTML。