首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 依 ISBN 查詢 Amazon 書誌資訊

     編輯:PHP 相關知識

透過解析 Amazon 商品頁面 HTML,以 ISBN-10 取得書名與封面縮圖。注意:Amazon 禁止爬蟲,正式專案請使用 Product Advertising API (PA-API 5.0) 或 Open Library / Google Books API。本文代碼僅供教學參考。

原始實作 (HTML 解析法)

<?php
function getBookFromISBN($isbn) {
    $find = '

現代替代方案:Open Library API (免費、無需金鑰)

<?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']);
}

Google Books API (需 API Key,配額較高)

<?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,
    ];
}

ISBN 格式處理工具

<?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;
}

法律與道德提醒

  • Amazon robots.txt 明確禁止爬蟲,違反可能導致 IP 被封鎖、法律風險。
  • 書名、封面圖片受版權保護,商業使用需授權。
  • Open Library、Google Books、ISBNdb 等提供合法 API,請優先使用。
  • 緩存查詢結果 (Redis/DB),避免重複請求外部 API。

完整服務類架構

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。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved