Created
December 18, 2010 06:15
-
-
Save jasonclark/746229 to your computer and use it in GitHub Desktop.
This is a proof of concept app. I wanted to see if an alternative browse/search app for Google Books was possible. Built with PHP, HTML and the Google Ajax Search API (version 1).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Searching Google Books using the Google Ajax Search API | |
//http://code.google.com/apis/books/docs/js/jsondevguide.html | |
//http://code.google.com/apis/ajaxsearch/documentation/#fonje | |
//http://code.google.com/apis/ajaxsearch/documentation/reference.html#_intro_fonje | |
//http://www.google.com/support/forum/p/booksearch-apis/thread?tid=0870ca17f002bfe1&hl=en | |
//assign value for title of page | |
$pageTitle = 'Search Books: Google Ajax Search API'; | |
$subTitle = 'MSU Libraries'; | |
//declare filename for additional stylesheet variable - default is "none" | |
$customCSS = 'master.css'; | |
//create an array with filepaths for multiple page scripts - default is meta/scripts/global.js | |
$customScript[0] = './meta/scripts/global.js'; | |
?> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> | |
<head> | |
<title><?php echo($pageTitle); ?> : Montana State University Libraries</title> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<link rel="alternate" type="application/rss+xml" title="MSU Libraries: Tools" href="http://feeds.feedburner.com/msulibrarySpotlightTools" /> | |
<style type="text/css" media="screen, projection, handheld"> | |
<!-- | |
<?php if ($customCSS != 'none') { | |
echo '@import url("'.dirname($_SERVER['PHP_SELF']).'/meta/styles/'.$customCSS.'");'."\n"; | |
} | |
?> | |
--> | |
</style> | |
<?php | |
if ($customScript) { | |
$counted = count($customScript); | |
for ($i = 0; $i < $counted; $i++) { | |
echo '<script type="text/javascript" src="'.$customScript[$i].'"></script>'."\n"; | |
} | |
} | |
?> | |
</head> | |
<body class="<?php if(!isset($_GET['view'])) { echo 'default'; } else { echo $_GET['view']; } ?>"> | |
<h1><?php echo $pageTitle; ?><span>: <?php echo $subTitle; ?></span><small>(Querying Google Books)</small></h1> | |
<div class="container"> | |
<ul id="tabs"> | |
<li id="tab1"><a href="./index.php">Demo App</a></li> | |
<li id="tab2"><a href="./what.php">What is this?</a></li> | |
<li id="tab3"><a href="./code.php">View Code</a></li> | |
</ul><!-- end tabs unordered list --> | |
<div class="main"> | |
<?php | |
//turn on full error reports for development purposes - should be turned off for production environment | |
error_reporting(E_ALL); | |
//set API version for Google Ajax Search API | |
$v = isset($_GET['v']) ? $_GET['v'] : '1.0'; | |
//set user API key for Google Ajax Search API | |
$key = isset($_GET['key']) ? $_GET['key'] : 'INSERT API KEY HERE'; | |
//set user IP for Google Ajax Search API | |
$ip = isset($_GET['ip']) ? $_GET['ip'] : $_SERVER['REMOTE_ADDR']; | |
//set default value for query to Google Ajax Search API | |
$query = isset($_GET['q']) ? $_GET['q'] : '0307387941'; | |
//set default value for search type to Google Ajax Search API | |
$type = isset($_GET['type']) ? $_GET['type'] : 'all'; | |
switch ($type) { | |
case 'all': | |
$index = 'books'; | |
$q = 'q='; | |
break; | |
case 'isbn': | |
$index = 'books'; | |
$q = 'q=ISBN'; | |
break; | |
case 'lccn': | |
$index = 'books'; | |
$q = 'q=LCCN'; | |
break; | |
case 'oclc': | |
$index = 'books'; | |
$q = 'q=OCLC'; | |
break; | |
default: | |
echo '<p>You must specify a search type such as "all" or "book". Check the url to make sure "type=" has a value.</p>'; | |
exit; | |
} | |
if(isset($_GET['q'])): | |
//if Google Ajax Search API has been queried using the form | |
//check and assign page of search results - are we on the first page? | |
$start = isset($_GET['start']) ? $_GET['start'] : 0; | |
echo '<p class="control"><a class="refresh" href="'.basename(__FILE__).'">Try another search?</a></p>'."\n"; | |
//set URL for the Google Ajax Search API | |
$url = 'http://ajax.googleapis.com/ajax/services/search/'.$index.'?v='.$v.'&key='.$key.'&userip='.$ip.'&'.$q.''.urlencode($query).'&start='.$start; | |
//$url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q='.urlencode($query).'%20site:'.$site.'&rsz=large&start='.$start; | |
//build request and send to Google Ajax Search API | |
$request = file_get_contents($url); | |
//decode json object(s) out of response from Google Ajax Search API | |
$body = json_decode($request); | |
//create an array for search results | |
$i = 0; | |
$search_results = array(); | |
//loop through all results | |
foreach ($body->responseData->results as $item) { | |
$search_results['results'][$i]['title'] = $item->title; | |
$search_results['results'][$i]['url'] = $item->url; | |
$search_results['results'][$i]['tbUrl'] = $item->tbUrl; | |
$search_results['results'][$i]['authors'] = $item->authors; | |
$search_results['results'][$i]['publishedYear'] = $item->publishedYear; | |
$search_results['results'][$i]['pageCount'] = $item->pageCount; | |
$search_results['results'][$i]['bookId'] = $item->bookId; | |
$i++; | |
} | |
//loop through all the pages of results | |
if(isset($body->responseData->cursor->pages)) { | |
foreach($body->responseData->cursor->pages as $p) { | |
$search_results['pages'][] = $p->start; | |
} | |
} | |
//make sure some results were returned, show results as html with result numbering and pagination | |
if(isset($search_results['results'])) { | |
?> | |
<h2 class="mainHeading">Results of your Google Books "<?php echo $type; ?>" search for "<?php echo @$_GET['q']; ?>" (About <?php echo $body->responseData->cursor->estimatedResultCount; ?> matches)</h2> | |
<ol<?php echo @$_GET['start'] > 0 ? ' start="' .($_GET['start'] + 1) .'"' : '' ?>> | |
<?php foreach ($search_results['results'] as $sr) { ?> | |
<li> | |
<p><a href="<?php echo rawurldecode($sr['url']); ?>"><?php echo $sr['title']; ?></a></p> | |
<p> | |
<img src="<?php echo rawurldecode($sr['tbUrl']); ?>" /><br /> | |
author: <?php echo $sr['authors']; ?><br /> | |
published: <?php echo $sr['publishedYear']; ?><br /> | |
page(s): <?php echo $sr['pageCount']; ?><br /> | |
id: <?php echo $sr['bookId']; ?><br /> | |
<a class="expand" href="<?php echo rawurldecode($sr['url']); ?>">more</a><br /> | |
</p> | |
</li> | |
<?php } ?> | |
</ol> | |
<?php if(isset($search_results['pages'])) { ?> | |
<ul class="pages"> | |
<?php $i = 0; foreach($search_results['pages'] as $p) { $i++; ?> | |
<li> | |
<?php if((!isset($_GET['start']) && $p == 0) || (@$_GET['start'] == $p)) { ?> | |
<strong class="active"><?php echo $i; ?></strong> | |
<?php } else { ?> | |
<a href="<?php echo basename(__FILE__); ?>?q=<?php echo @$_GET['q']; ?>&type=<?php echo $type; ?>&start=<?php echo $p; ?>"><?php echo $i; ?></a> | |
<?php } ?> | |
</li> | |
<?php } ?> | |
</ul> | |
<?php } ?> | |
<?php | |
} | |
else { | |
?> | |
<p><strong>Sorry, there were no results</strong></p> | |
<?php | |
} | |
//for testing purposes show actual request to API - REMOVE when finished | |
//$apiRequest = $url; | |
//echo '<p>API request: '.$apiRequest.'</p>'; | |
?> | |
<?php | |
else: //show form and allow the user to check for Google site search results and more | |
?> | |
<form id="searchForm" name="searchForm" action="<?php echo basename(__FILE__); ?>" method="get"> | |
<fieldset id="searchBox"> | |
<label>Find:</label> | |
<input class="text" id="q" name="q" type="text" value="keyword or id" onfocus="this.value=''; this.onfocus=null;" /> | |
<select id="type" name="type" size="1"> | |
<option selected value="all">All Books</option> | |
<option value="isbn">Books by ISBN</option> | |
<option value="lccn">Books by LCCN #</option> | |
<option value="oclc">Books by OCLC #</option> | |
</select> | |
<input class="submit" id="submit" name="submit" type="submit" value="find" /> | |
</fieldset> | |
</form> | |
<p><a href="http://www.lib.montana.edu/~jason/files.php">See more of Jason's code samples and site</a></p> | |
<?php | |
//end submit isset if statement on line 73 | |
endif; | |
?> | |
</div><!-- end div main --> | |
</div><!-- end container div --> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment