Git Product home page Git Product logo

php-google-books's Introduction

Build Status Scrutinizer code quality Packagist Gitter

php-google-books

Simple PHP package for working with the Google Books API. With Laravel5 integration. Doesn't yet support authentication, so it only works with public data. PRs are welcome.

Install using Composer

Make sure you have Composer installed, then run

composer require scriptotek/google-books

in your project directory to get the latest stable version of the package.

Usage

Start by creating a new client:

require_once('vendor/autoload.php');
use Angelleger\GoogleBooks\GoogleBooks;

$books = new GoogleBooks(['key' => 'YOUR_API_KEY_HERE']);

Note that you can also use the API without specifying an API key, but you will then get a lower request quota. A UsageLimitExceeded exception is thrown when you reach the quota.

Working with volumes

Getting a single volume by id:

$volume = $books->volumes->get('kdwPAQAAMAAJ');

or by ISBN:

$volume = $books->volumes->byIsbn('0521339057');

Search:

foreach ($books->volumes->search('Hello world') as $vol) {
    echo $vol->title . "\n";
}

Note that the search() method returns a generator that automatically fetches more results until the result list is depleted. If there are thousands of results this will of course take a long time to fetch, so you probably want to define a limit. Limits can be defined as an option: ['maxResults' => 10] inside the GoogleBooks class.

Working with bookshelves

Getting a single bookshelf by user id and shelf id:

$shelf = $books->bookshelves->get('113555231101190020526', '1002');

List the public bookshelves of a user, and their volumes:

foreach ($books->bookshelves->byUser('113555231101190020526') as $shelf) {
    echo "<h2>$shelf->title</h2>\n";
    echo "<ul>\n";
    foreach ($shelf->getVolumes() as $vol) {
        echo "  <li>$vol->title</li>\n";
    }
    echo "</ul>\n";
}

Laravel 5 integration

This project ships with a service provider that you can add to the $providers array in your config/app.php:

Angelleger\GoogleBooks\GoogleBooksServiceProvider::class,

There's also a facade you can add to the $aliases array if you like:

'GoogleBooks' => Angelleger\GoogleBooks\GoogleBooksFacade::class,

Run

$ php artisan vendor:publish --provider="Angelleger\GoogleBooks\GoogleBooksServiceProvider"

to create the config/googlebooks.php configuration file.

Troubleshooting

If you get 403 Forbidden with

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "unknownLocation",
    "message": "Cannot determine user location for geographically restricted operation."
   }
  ],
  "code": 403,
  "message": "Cannot determine user location for geographically restricted operation."
 }
}

it means the Books API failed to locate you based on your ip address. Fix this by specifying the 2 letter ISO639 country code manually using the country option to the constructor:

$books = new GoogleBooks(['country' => 'NO']);

php-google-books's People

Contributors

danmichaelo avatar angelleger avatar josephmancuso avatar macfja avatar mrcage avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.