/* Description: 获取利用豆瓣API根据ISBN取图书信息. Author: 老蟹 License: GPL v2 book对象利用方法: $book = get_book_data($isbn); $title = $book->getTitle(); if(!isset($title)) { $message = "此书未找到."; }else{ .... } */ // 取Book信息 function get_book_data($isbn) { $url = "https://api.douban.com/v2/book/isbn/:".$isbn; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); curl_close($curl); $book_array = (array) json_decode($result, true); if(!empty($book_array["title"])) { $book_title = $book_array["title"]; $book_author = $book_array["authors"][0]; $book_cover = $book_array["image"]; $book_isbn = $book_array["isbn13"]; // ISBN13 $book_info = $book_array["summary"]; $book = new Book($book_title, $book_isbn, $book_author, $book_cover, $book_info); return $book; } } // 这里只取几个主要信息 class Book { private $book_title; private $book_isbn; private $author; private $book_cover; private $book_info; public function __construct($book_title, $book_isbn, $author, $book_cover, $book_info) { $this->author = $author; $this->book_isbn = $book_isbn; $this->book_cover = $book_cover; $this->book_title = $book_title; $this->book_info = $book_info; } public function getTitle() { return $this->book_title; } public function getISBN() { return $this->book_isbn; } public function getAuthor() { return $this->author; } public function getCover() { return $this->book_cover; } public function getBookInfo() { return $this->book_info; } }