PHP Classes

PHP Google Books API: Search for books using Google Books API

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 189 All time: 8,589 This week: 114Up
Version License PHP version Categories
cgooglebooksapi 1.0.4BSD License5PHP 5, Searching, Web services
Description 

Author

This class can search for books using Google Books API.

It takes an array of optuons and sends HTTP requests to Google Books API Web server using either file_get_contents or curl.

The class retrieves and decodes the results and store it in an array that lists the found books title, authors, publisher, categories, ISBN and thumbnail URL.

The search parameters include the book title, categories, start index, results limit, and get book details.

Innovation Award
PHP Programming Innovation award nominee
October 2016
Number 7


Prize: One downloadable copy of Komodo IDE
Google Books is a well known service provided by Google that indexes a large number of books that were scanned, so users can search them by title, author, and even their contents.

This class provides an interface to the Google Books API to perform searches for books indexed on Google Books.

Manuel Lemos
Picture of Mohammed Asad
  Performance   Level  
Name: Mohammed Asad <contact>
Classes: 2 packages by
Country: India India
Age: 35
All time rank: 3418227 in India India
Week rank: 312 Up22 in India India Up
Innovation award
Innovation award
Nominee: 2x

Example

<!DOCTYPE html>
<html>
<head>
<title>Simple Book Search</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
<style>
/* #### Dark Matter #### */
.dark-matter {
    margin-left: auto;
    margin-right: auto;
    max-width: 500px;
    background: #555;
    padding: 20px 30px 20px 30px;
    font: 12px "Helvetica Neue", Helvetica, Arial, sans-serif;
    color: #D3D3D3;
    text-shadow: 1px 1px 1px #444;
    border: none;
    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
}
.dark-matter h1 {
    padding: 0px 0px 10px 40px;
    display: block;
    border-bottom: 1px solid #444;
    margin: -10px -30px 30px -30px;
}
.dark-matter h1>span {
    display: block;
    font-size: 11px;
}
.dark-matter label {
    display: block;
    margin: 0px 0px 5px;
}
.dark-matter label>span {
    float: left;
    width: 20%;
    text-align: right;
    padding-right: 10px;
    margin-top: 10px;
    font-weight: bold;
}
.dark-matter input[type="text"], .dark-matter input[type="email"], .dark-matter textarea, .dark-matter select {
    border: none;
    color: #525252;
    height: 25px;
    line-height:15px;
    margin-bottom: 16px;
    margin-right: 6px;
    margin-top: 2px;
    outline: 0 none;
    padding: 5px 0px 5px 5px;
    width: 70%;
    border-radius: 2px;
    -webkit-border-radius: 2px;
    -moz-border-radius: 2px;
    -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
    background: #DFDFDF;
}
.dark-matter select {
    background: #DFDFDF url('down-arrow.png') no-repeat right;
    background: #DFDFDF url('down-arrow.png') no-repeat right;
    appearance:none;
    -webkit-appearance:none;
    -moz-appearance: none;
    text-indent: 0.01px;
    text-overflow: '';
    width: 70%;
    height: 35px;
    color: #525252;
    line-height: 25px;
}
.dark-matter textarea{
    height:100px;
    padding: 5px 0px 0px 5px;
    width: 70%;
}
.dark-matter .button {
    background: #FFCC02;
    border: none;
    padding: 10px 25px 10px 25px;
    color: #585858;
    border-radius: 4px;
    -moz-border-radius: 4px;
    -webkit-border-radius: 4px;
    text-shadow: 1px 1px 1px #FFE477;
    font-weight: bold;
    box-shadow: 1px 1px 1px #3D3D3D;
    -webkit-box-shadow:1px 1px 1px #3D3D3D;
    -moz-box-shadow:1px 1px 1px #3D3D3D;
}

.dark-matter .button:hover {
    color: #333;
    background-color: #EBEBEB;
}
.dark-matter .error{
    color:red;
    font-size:16px;
    text-align:center;
}
table{
    width:50%;
    margin:auto;
}
table img{
    width:75%;
    height:40%;
}
</style>
<?php
   
/**
    * A simple web form example to use the class
    * Currently, I fetch title, authors,publisher, description, categories, isbn, thumbnail_url
    * If you need more data you are free to add and use it,
    *
    * More parameter details can be found here. //https://developers.google.com/books/docs/v1/reference/volumes
    *
    */
if( true == isset($_POST['get_books_details'])) {
   
    require
'../src/cgoogleBooksApi.class.php';

   
$objgoogleBooksApi = new cgoogleBooksApi();
   
$objgoogleBooksApi->setSearchData($_POST['books']);
   
$objgoogleBooksApi->setUsePHPCurl(true);
   
$objgoogleBooksApi->setIsFromAjax(false);
   
$strJSONData = $objgoogleBooksApi->fetchGoogleBooks();
   
   
$arrstrGoogleData = json_decode( $strJSONData, true );
   
$arrstrGoogleBooksData = (array ) json_decode( $arrstrGoogleData['books'], true );
    if(
0 == count( $arrstrGoogleBooksData ) ) {
        
           
$strWarning = 'No Books Found!';
           
    }
}
?>
</head>
<body style="margin:auto;">
<form method="post" action="" class="dark-matter">
<h1>
    Simple Book Search Form<span>Please fill the texts in the fields(atleast one from the first three).</span><br/>
    <span class="error"><?php echo @$strWarning?></span>

</h1>
<p>Author:<br>
  <input type="text" name="books[authors]" value="<?php echo @$_POST['books']['authors']?>"/>
  <br>
   Title<br>
  <input type="text" name="books[title]" value="<?php echo @$_POST['books']['title']?>"/>
  <br>
   Category<br>
  <input type="text" name="books[categories]" value="<?php echo @$_POST['books']['categories']?>"/>
  <br>
     Start Index <br>
  <input type="text" name="books[start_index]" value="<?php echo @$_POST['books']['start_index']?>"/>
  <br>
    Max Results(0-40)<br>
  <input type="text" name="books[max_result]" value="<?php echo @$_POST['books']['max_result']?>"/>
  <br>

  <input type="submit" value="fetch books" class="button" name="get_books_details"/></p>
 
</form>
<?php
if( true == isset($_POST['get_books_details'])) {
   
    if(
0 < count( $arrstrGoogleBooksData ) ) {
       
?>
<br/>
    <br/>
    <table align="center" class="pure-table pure-table-bordered">
        <tr>
            <td>Thumbnail</td>
            <td>Title</td>
            <td>Author</td>
            <td>Publisher</td>
            <td>Category</td>
            <td>ISBN</td>
        </tr>
    <?php
   
foreach( $arrstrGoogleBooksData as $strIndex => $strGoogleData ) {
       
?>
<tr>
        <td><img src="<?php echo $strGoogleData['thumbnail_url'];?>"/></td>
        <td><?php echo $strGoogleData['title'];?></td>
        <td><?php echo $strGoogleData['authors'];?></td>
        <td><?php echo $strGoogleData['publisher'];?></td>
        <td><?php echo $strGoogleData['categories'];?></td>
        <td><?php echo $strGoogleData['isbn'];?></td>
    </tr>
        <?php
   
}
   
?>
</table>
    <?php
   
} }
?>

</body>
</html>


Details

This class is created in a good intention to fetch google books api without using any google auth credentials Hence, its accuracy is 70%. Well, it also has limitations based on the google,as it can fetch only 40 at a time.

Screenshots  
  • SimpleBookSearchForm
  Files folder image Files  
File Role Description
Files folder imagecgooglebooksAPi (2 files, 2 directories)
Accessible without login Plain text file README.txt Doc. Documentation

  Files folder image Files  /  cgooglebooksAPi  
File Role Description
Files folder imagesrc (1 file)
Files folder imagetests (2 files)
  Accessible without login Plain text file LICENSE Lic. License text
  Accessible without login Plain text file README.txt Doc. Documentation

  Files folder image Files  /  cgooglebooksAPi  /  src  
File Role Description
  Plain text file cgoogleBooksApi.class.php Class Class source

  Files folder image Files  /  cgooglebooksAPi  /  tests  
File Role Description
  Accessible without login Plain text file simpleFetch.php Example Example script
  Accessible without login Plain text file simpleWebForm.php Example Example script

Downloadcgooglebooksapi-2016-12-15.zip 37KB
Downloadcgooglebooksapi-2016-12-15.tar.gz
Install with ComposerInstall with Composer
Needed packages  
Class DownloadWhy it is needed Dependency
cURL Download .zip .tar.gz incase if their file_get_contents does not work Optional
 Version Control Unique User Downloads Download Rankings  
 85%
Total:189
This week:0
All time:8,589
This week:114Up