将 Google Book Search 集成到 PHP 应用程序中(2)
<h2>Search</h2>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Search for:
<input type="text" name="q" value="<?php echo isset($_POST['q']) ?
$_POST['q'] : ''; ?>" />
<input type="submit" name="submit" value="Go" />
</form>
<?php if (isset($feed)): ?>
<h2>Search results for '<?php echo $_POST['q']; ?>'</h2>
<div>
<?php echo $feed->totalResults; ?> result(s) found.
</div>
<div id="results")
<ol>
<?php foreach ($feed as $entry): ?>
<li>
<a href="<?php echo $entry->getInfoLink()->getHref(); ?>">
<?php echo $entry->getTitle(); ?></a>
</li>
<?php endforeach; ?>
</ol>
</div>
<?php endif; ?>
</body>
</html>
图 2 展示了搜索 “indian cooking” 生成的结果示例:

您可能已经注意到了 清单 3 中增加的内容:它使用了 Zend_Gdata_Books_VolumeEntry::getInfoLink() 方法,该方法返回一个指向 Google Book Search Web 站点上图书信息页面的链接,单击该链接将把用户重定向到一个包含详细图书说明和用户评论等信息的页面。图 3 显示了该页面的一个示例:
图 3. 一个包含图书细节的页面

还有一点值得注意:传递到 Google Book Search Data API 的查询字符串必须是 URL 编码字符串。在前面的清单中,您应该已经看到,URL 编码是通过 PHP 的 urlencode() 方法完成的。
检索详细图书信息
如 清单 1 所示,除图书标题之外,Google Book Search Data API 返回的卷提要包含相当多的信息:作者和出版商名称、ISBN 编号、页数、主题等。使用 Zend_Gdata_Books,所有这些信息都被表示为一组对象,您可以在一个 PHP 脚本范围内访问并操作这些对象来创建更便于提供信息的搜索结果页面。
清单 4 展示了如何检索图书细节:
清单 4. 检索图书细节
| <?php if (isset($_POST['submit'])) { // load Zend Gdata libraries require_once 'Zend/Loader.php'; Zend_Loader::loadClass('Zend_Gdata_Books'); Zend_Loader::loadClass('Zend_Gdata_Books_VolumeQuery'); Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); // set credentials for ClientLogin authentication $user = "[email protected]"; $pass = "secret"; try { // perform login $client = Zend_Gdata_ClientLogin::getHttpClient( $user, $pass, 'print'); $client->setHeaders('X-Forwarded-For', $_SERVER['REMOTE_ADDR']); $books = new Zend_Gdata_Books($client); // prepare and execute search query $query = new Zend_Gdata_Books_VolumeQuery; $query->setQuery(urlencode($_POST['q'])); $feed = $books->getVolumeFeed($query); } catch (Exception $e) { die('ERROR:' . $e->getMessage()); } } ?> |
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Searching for book titles</title> <style> .entry { height: 120px; border-bottom: dashed silver 2px; padding-top: 10px; } .thumbnail { float: left; border: solid black 2px; padding: 2px; margin-right: 10px; } .desc { font-style: italic; } .small { font-size: smaller; } </style> </head> <body> <h2>Search</h2> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> Search for: <input type="text" name="q" value="<?php echo isset($_POST['q']) ? $_POST['q'] : ''; ?>" /> <input type="submit" name="submit" value="Go" /> </form> <?php if (isset($feed)): ?> <h2>Search results for '<?php echo $_POST['q']; ?>'</h2> <div> <?php echo $feed->totalResults; ?> result(s) found. </div> <div id="results"> <?php $x = 1; ?> <?php foreach ($feed as $entry): ?> <?php //print_r($entry); $book = new stdClass; // get title if (is_array($entry->getTitles())) { foreach ($entry->getTitles() as $title) { $book->titles[] = $title->getText(); } } // get authors if (is_array($entry->getCreators())) { foreach ($entry->getCreators() as $creator) { $book->authors[] = $creator->getText(); } } // get publishers if (is_array($entry->getPublishers())) { foreach ($entry->getPublishers() as $publisher) { $book->publishers[] = $publisher->getText(); } } // get publication date if (is_array($entry->getDates())) { $arr = $entry->getDates(); $book->pubdate = (is_object($arr[0])) ? $arr[0]->getText() : 'Unspecified'; } // get ISBN numbers if (is_array($entry->getIdentifiers())) { foreach ($entry->getIdentifiers() as $id) { if (preg_match('/ISBN/', $id->getText())) { $book->isbn[] = $id->getText(); } } } // get first subject if (is_array($entry->getSubjects())) { $arr = $entry->getSubjects(); $book->subject = is_object($arr[0]) ? $arr[0]->getText() : 'Unspecified'; } // get first description if (is_array($entry->getDescriptions())) { $arr = $entry->getDescriptions(); $book->desc = is_object($arr[0]) ? $arr[0]->getText() : 'No description available'; } ?> <div class="entry"> <div class="thumbnail"> <img src="<?php echo ($entry->getThumbnailLink()) ? $entry->getThumbnailLink()->getHref() : ''; ?>" /> </div> <div class="data"> <?php echo $x; ?>. <?php echo ucwords(@implode(': ', $book->titles)); ?><br/> <?php echo @implode(', ', $book->authors); ?> | <?php echo @implode(', ', $book->publishers); ?> | <?php echo $book->subject; ?> | <?php echo date('d M Y', strtotime($book->pubdate)); ?> <br/> <span class="desc"><?php echo $book->desc; ?></span> <br/> <span class="small"><?php echo @implode(', ', $book->isbn); ?> | <a href="<?php echo $entry->getInfoLink()->getHref(); ?>"> More information</a> </span> </div> </div> <?php $x++; ?> <?php endforeach; ?> <?php endif; ?> </body> </html> |
清单 4 介绍了 Zend_Gdata_Books_VolumeEntry 类的几个新方法。以下是其中几个重要方法的简单列表:
•getDates() 方法返回图书出版日期。
•getCreators() 方法返回作者姓名。
•getPublishers() 方法返回出版商名称。
•getSubjects() 和 getDescriptions() 方法返回图书主题和说明。
•getTitles() 方法返回图书标题(和副标题,如果有的话)。
•getIdentifiers() 方法返回图书的 ISBN-10 和 ISBN-13 编号数组,以及 Google Book Search 记录 ID。
•getThumbnailLink() 方法返回图书封面缩略图的 URL。
•getVolumeId() 返回 Google Book Search 服务中的唯一卷标识符。
所有这些方法都返回一个对象数组,对应于各个 Dublin Core metadata 元素;接下来将由开发人员访问每个数组的元素并使用其中的信息来重新组织搜索结果。
图 4. 一个 Google Books API 搜索的结果,搜索结果经过增强,可包含一个封面图像和其他图书数据

使用查询过滤器
您也许已经注意到,在前面的所有示例中,搜索结果提要只包含 10 个条目,尽管 <openSearch:> 元素实际表明匹配值的数量要高得多。这是因为,在默认情况下,Google Book Search 提要限制为每个提要 10 个匹配值。这个限制并非一层不变的,您可以对您的 REST 查询添加以下参数,轻松定制 API 输出:
•start-index 参数指定提要中的条目的起始偏移。
•max-results 参数指定提要中的条目数量。
•min-viewability 参数指定提要条目是否只包含那些存在部分或完整预览的图书。
除了这些参数之外,您还可以对一个搜索查询添加其他过滤器,根据标题、作者、出版商、语言、主题、说明或 ISBN 编号进行搜索。清单 5 展示了如何添加过滤器:
清单 5. 通过作者、标题和可见性过滤搜索结果
| <?php if (isset($_POST['submit'])) { // load Zend Gdata libraries require_once 'Zend/Loader.php'; Zend_Loader::loadClass('Zend_Gdata_Books'); Zend_Loader::loadClass('Zend_Gdata_Books_VolumeQuery'); Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); // set credentials for ClientLogin authentication $user = "[email protected]"; $pass = "secret"; try { // perform login $client = Zend_Gdata_ClientLogin::getHttpClient( $user, $pass, 'print'); $client->setHeaders('X-Forwarded-For', $_SERVER['REMOTE_ADDR']); $books = new Zend_Gdata_Books($client); // prepare and execute search query $query = new Zend_Gdata_Books_VolumeQuery; $queryStr = ''; if (!empty($_POST['title'])) { $queryStr .= '+intitle:'.urlencode($_POST['title']); } if (!empty($_POST['author'])) { $queryStr .= '+inauthor:'.urlencode($_POST['author']); } $query->setQuery($queryStr); $query->setMinViewability($_POST['v']); $query->setMaxResults(20); $feed = $books->getVolumeFeed($query); } catch (Exception $e) { die('ERROR:' . $e->getMessage()); } } ?> |
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Searching for book titles</title> <style> .entry { height: 120px; border-bottom: dashed silver 2px; padding-top: 10px; } .thumbnail { float: left; border: solid black 2px; padding: 2px; margin-right: 10px; } .desc { font-style: italic; } .small { font-size: smaller; } </style> </head> <body> <h2>Search</h2> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> Title: <input type="text" name="title" value="<?php echo isset($_POST['title']) ? $_POST['title'] : ''; ?>" /> Author: <input type="text" name="author" value="<?php echo isset($_POST['author']) ? $_POST['author'] : ''; ?>" /> Viewability: <select name="v"> <option value="none">Any</option> <option value="partial_view">Partial</option> <option value="full_view">Full</option> </select> <input type="submit" name="submit" value="Go" /> </form> <?php if (isset($feed)): ?> <h2>Search results for '<?php echo $query->getQuery(); ?>' </h2> <div> <?php echo $feed->totalResults; ?> result(s) found. </div> <div id="results"> <?php $x = 1; ?> <?php foreach ($feed as $entry): ?> <?php //print_r($entry); $book = new stdClass; // get title if (is_array($entry->getTitles())) { foreach ($entry->getTitles() as $title) { $book->titles[] = $title->getText(); } } // get authors if (is_array($entry->getCreators())) { foreach ($entry->getCreators() as $creator) { $book->authors[] = $creator->getText(); } } // get publishers if (is_array($entry->getPublishers())) { foreach ($entry->getPublishers() as $publisher) { $book->publishers[] = $publisher->getText(); } } // get publication date if (is_array($entry->getDates())) { $arr = $entry->getDates(); $book->pubdate = (is_object($arr[0])) ? $arr[0]->getText() : 'Unspecified'; } // get ISBN numbers if (is_array($entry->getIdentifiers())) { foreach ($entry->getIdentifiers() as $id) { if (preg_match('/ISBN/', $id->getText())) { $book->isbn[] = $id->getText(); } } } 顶(1)
踩(0)
|
