aalfiann/ebook-api-php

一个用于从 Google Book 和 GoodReads API 获取电子书数据的 PHP 封装类。

1.2.3 2019-04-24 20:15 UTC

This package is auto-updated.

Last update: 2024-09-25 08:34:03 UTC


README

Version Total Downloads License

一个用于从 Google Book 或 GoodReads API 获取电子书信息的 PHP 封装类。

安装

通过 Composer 安装此包。

composer require "aalfiann/ebook-api-php:^1.0"

使用 Google Book API

这是搜索书籍的基本方法。

use aalfiann\EbookAPI\GoogleBook;
require_once ('vendor/autoload.php');

header('Content-Type: application/json');
$ebook = new GoogleBook('YOUR_GOOGLE_API_KEY');

// Search book
echo $ebook->search('vue js')
    
    // you can add filter to search only free-ebooks
    ->filter('free-ebooks')
    
    // you can add projection 'lite'. This includes only a subject of volume and access metadata.
    ->projection('lite')
    
    // you can add printType 'books'. This will return just books.
    ->printType('books')

    // you can add langRestrict 'en'. This will return books with english only.
    ->langRestrict('en')

    // you can add download 'epub'. Currently only support epub.
    ->download('epub')

    // you can add orderBy 'newest'. This will order books start from newest.
    ->orderBy('newest')

    // you can add startIndex 0. First page always start from 0.
    ->startIndex(0)

    // you can add maxResults 10. This will return only 10 item of books. Max value is 40.
    ->maxResults(10)

    // send request to Google Book API
    ->send()

    // get the response from Google Book API
    ->getResponse();

通过特定参数获取书籍。

通过标题获取书籍

// Get book by title name
$ebook->title('steve jobs')->maxResults(10);

// Get book by title name 'steve jobs' but only return which is having 'Biography' word in title
$ebook->title('steve jobs','Biography')->maxResults(10);

通过作者获取书籍

// Get book by author name
$ebook->author('keyes')->maxResults(10);

// Get book by author name 'keyes' but only return which is having 'flowers' word in title
$ebook->author('keyes','flowers')->maxResults(10);

通过主题名称获取书籍

// Get book by subject name
$ebook->subject('Fiction')->maxResults(10);

// Get book by subject name 'Fiction' but only return which is having 'flowers' word in title
$ebook->subject('Fiction','flowers')->maxResults(10);

通过出版社名称获取书籍

// Get book by publisher name
$ebook->publisher('OUP Oxford')->maxResults(10);

// Get book by publisher name 'OUP Oxford' but only return which is having 'Law' word in title
$ebook->publisher('OUP Oxford','Law')->maxResults(10);

通过 ID 获取书籍

// Get book by Google Book ID
$ebook->id('BOOK_ID');

// Get book by ISBN
$ebook->isbn('ISBN_ID');

// Get book by LCCN
$ebook->lccn('LCCN_ID');

// Get book by OCLC
$ebook->oclc('OCLC_ID');

获取书架

// Get bookshelves from user id '109862556655476830073' 
echo $ebook->bookshelves('109862556655476830073')
    
    // you can choose the shelf. For example only shelf with id '1001'
    ->shelf('1001')

    // if you want to show the items inside shelf
    ->retrieve()
    
    // send request to Google Book API
    ->send()
    
    // get response from Google Book API
    ->getResponse();

如果您想查看响应

echo $ebook->send()->getResponse();

如果您想统计结果项数

echo $ebook->send()->count();

如果您想查看调试响应

echo $ebook->send(false)->getResponse();

使用 GoodReads API

如何搜索书籍的示例

要使用 GoodReads 搜索书籍,您必须向 https://www.goodreads.com/search/index.xml 发送请求,并且需要四个参数,即 keyqpagesearch[field]更多信息请访问这里

use aalfiann\EbookAPI\GoodReads;
require_once ('vendor/autoload.php');

header('Content-Type: application/json');
$ebook = new GoodReads('YOUR_GOODREADS_API_KEY');

$ebook->path('search/index.xml')
    // Add param 'q' to query
    ->q('steve jobs')
    
    // Add param 'search[Field]' to query more spesific results. default is 'all'
    ->searchField('title')
    
    // You can add 'page' for pagination. Start page is always 1
    ->page(1)
    
    // Send request to GoodReads API
    ->send()
    
    // Get response from GoodReads API
    ->getResponse();

手动添加参数的示例

如果 Goodreads 发布了新功能,当然新参数的快捷方法在这个类中不可用。
因此,您可以使用 addParam($name,$value) 函数。

例如,我们想要 获取特定书籍的用户评论

$ebook->path('review/show_by_user_and_book.xml')
    // Add param 'user_id' which is not available inside the class
    ->addParam('user_id',1)
    
    // Add param 'book_id' which is not available inside the class
    ->addParam('book_id',50)

    // Add param 'include_review_on_work' which is not available inside the class
    ->addParam('include_review_on_work','false')
    
    // Send request to GoodReads API 
    ->send()

    // Get response from GoodReads API
    ->getResponse();

有关 path() 以及其他信息的更多信息,您必须阅读 GoodReads API 文档,因为此 GoodReads 类只是一个封装。

GoodReadsMethod 类

GoodReadsMethod 类是简单与 GoodReads API 通信的方法。这个类只包含常用或通用方法。

这是如何使用 GoodReadsMethod 类的方法

use aalfiann\EbookAPI\GoodReadsMethod;
require_once ('vendor/autoload.php');

header('Content-Type: application/json');
$ebook = new GoodReadsMethod('YOUR_GOODREADS_API_KEY');

// Search Book
echo $ebook->searchBook('fiction');

// Search Book about 'fiction' but show at page 3
echo $ebook->searchBook('fiction',3);

// Search Book by Genre 'comedy'
echo $ebook->searchBookByGenre('comedy');

// Get book by GoodReads Internal ID
echo $ebook->getBook('21825181');

// Get book by ISBN
echo $ebook->getBookByISBN('9781451648546');

// Get book by title
echo $ebook->getBookByTitle('vuejs');

限制

  • 此库仅用于从 Google BookGoodReads API 获取公共数据信息,因此在此库中没有实现 OAuth / 授权
  • 此库不包含缓存。您应该缓存结果,因为 GoogleGoodReads速率限制,并且如果请求过多将阻止您的 IP 地址服务器。
  • GoodReadsMethod 类是不稳定的,代码可能会根据 GoodReads 的变化在未来进行更改。