Searching the Google Books API with PHP [Quickstart]
APIs are always fun to me, because you gain access to a large pool of information. Today I want to take a quick look at how to access the Google Books API with PHP. The code snippets and general knowledge should be applicable to all kinds of other Google APIs like YouTube or Google+.
Requirements
To access the Google API you will need an API key, which firstly requires you to register a project as a developer and then register your application as a client for the Google Books API. I have heavily screenshotted that process, because I didn't find it very intuitive.
Secondly I'll be using Composer for the ones that have php
executable in their command line, most likely MacOS and Linux users. If you don't want anything to do with composer, you can also download the Google API client manually from github.
Getting a Google API Key
In order to get an API key, you can follow the guide at the Google Books API Docs. Basically it will take you to some of the following pages.Create a project and hit the burger menu in the top.
Pick API Manager.
Enter Books into the search field.
Create a Server to Server / web server API Key.
Copy the displayed key for use in our example.
Installing Composer
Go to the Composer website and grab an up to date snippet that looks like the following. Run it inside your projects directory:php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === 'e115a8dc7871f15d853148a7fbac7da27d6c0030b848d9b3dc09e2a0388afed865e6a3d6b3c0fad45c48e2b5fc1196ae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
After that, you will have an executable composer.phar
file in your projects directory.
The Google API PHP Client
Google makes it easy to access their API and they have provided an API client ready for installation with composer on the following github repository: Google API PHP Client. If you do not want to use composer follow their guide on how to download it manually:If you abhor using composer, you can download the package in its entirety. The Releases page lists all stable versions. Download any file with the name google-api-php-client-[RELEASE_NAME].zip for a package including this library and its dependencies.
Uncompress the zip file you download, and include the autoloader in your project:
require_once '/path/to/google-api-php-client/vendor/autoload.php';
source: https://github.com/google/google-api-php-client#download-the-releaseDownload the release
If you want to use composer, just run the command below, which will install the API client and other dependencies:
./composer.phar require google/apiclient:^2.0
If the command runs successfully you should see output that resembles the following in your terminal:
Output:
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing psr/http-message (1.0)
Downloading: 100%
- Installing guzzlehttp/psr7 (1.3.1)
Downloading: 100%
- Installing guzzlehttp/promises (1.2.0)
Downloading: 100%
- Installing guzzlehttp/guzzle (6.2.0)
Downloading: 100%
- Installing phpseclib/phpseclib (2.0.2)
Downloading: 100%
- Installing psr/log (1.0.0)
Downloading: 100%
- Installing monolog/monolog (1.20.0)
Downloading: 100%
- Installing firebase/php-jwt (v3.0.0)
Downloading: 100%
- Installing google/apiclient-services (v0.3)
Downloading: 100%
- Installing psr/cache (1.0.0)
Downloading: 100%
- Installing google/auth (v0.9)
Downloading: 100%
- Installing google/apiclient (v2.0.1)
Downloading: 100%
Making a Google Books Search Query with PHP
<?php
// your API key here
$API_KEY = '';
// this will include the libraries that were gotten with composer or downloaded directly
require_once 'vendor/autoload.php';
// instantiate the Google API Client
$client = new Google_Client();
$client->setApplicationName("Google Books with PHP Tutorial Application");
$client->setDeveloperKey( $API_KEY );
// get an instance of the Google Books client
$service = new Google_Service_Books($client);
// set options for your request
$optParams = [];
// make the API call to retrieve some search results
$results = $service->volumes->listVolumes('The Brain That Changes Itself', $optParams);
foreach ( $results as $item ) {
echo $item['volumeInfo']['title'], "<br /> \n";
echo '<a href="' . $item['volumeInfo']['previewLink'] . '">' . $item['volumeInfo']['previewLink'] . '</a>';
echo '<pre>';
// useful for debugging and checking which fields actually are in each item of the response
var_dump( $item );
echo '</pre>';
}
?>
If you prefer you can run this all in your local or remote web server, but using PHPs built in web server on the command line will do fine for our testing purposes:
php -S localhost:8000
After running that, you can go to the following address in your browser and check out the page:
Go to: http://localhost:8000
If everything went well, you should see the following in your browser:
If you are having trouble, you can always go back to the Google API stats page and see if you are seeing some blue lines that indicate actual requests. Their API limits are by default 1000 requests per day and 100 per second per user (I assume using OAuth tokens).
Wrapping Up
What do you think about API access and what are your languages of choice? Usually I would pick node.js as a weapon of choice for this, but PHP will do just fine to retrieve the data and save them temporarily.