插件79:搜索Google图书
来源:互联网 发布:大数据人才需求分析 编辑:程序博客网 时间:2024/05/17 23:35
<?php // Plug-in 79: Search Google Books/* * 搜索google图书 * 插件说明: * 插件接受一个搜索串,返回在Google图书数据库中找到的图书。 * 若操作成功,则返回一个两元素的数组,其中第一个元素表示返回的图书的数量,第二个元素是一个数组,保存这些图书的详细信息。 * 若操作失败,则返回单个元素的数组,元素的值为FALSE。 * 本插件需要以下参数: * $search 一个标准的搜索查询 * $start 返回的第一个结果 * $count 返回结果的最大个数 * $type 返回结果的类型,如果它的值为none,则表示返回全部图书, * 如果它的值为partial,表示只返回书的部分预览内容。 * 如果它的值为full,则只返回包含完整预览内容在内的全部图书。 */// This is an executable example with additional code supplied// To obtain just the plug-ins please click on the Download link$search = "Mark Twain";echo "<font face='Arial' size='2'>Google Books results " . "for: <b>$search</b>:<br /><br />";$result = PIPHP_SearchGoogleBooks($search, 1, 20, 'none');if (!$result[0]) echo "No books found for $search.";else{ foreach($result[1] as $book) { echo "<img src='$book[5]' align='left' border='1'>"; echo "<a href='$book[6]'>$book[0]</a> ($book[2], " . "$book[3])<br />$book[4]"; if ($book[7]) echo " (<a href='$book[7]'>preview</a>)"; echo "<br clear='left'/><br />"; }}function PIPHP_SearchGoogleBooks($search, $start, $count, $type){ // Plug-in 79: Search Google Books // // This plug-in takes a search query and returns matching // books from books.google.com. Upon success it returns // a two elemet array, the first being the number of books // returned and the second is an array whose elements are // each sub-arrays containing these details: 1) Title, 2) // Author, 3) Publisher, 4)Date, 5) Description, 6) Info // URL, 7) Preview URL. on failure it returns a single // element array with the value FALSE. It requires these // arguments: // // $search: A search query // $start: The first result to return // $count: The maximum number of results to return // $type: If 'none' return all books, if 'partial' // return books with partial previews, if // 'full' only return books where the entire // book can be read $results = array(); $url = 'http://books.google.com/books/feeds/volumes?' . 'q=' . rawurlencode($search) . '&start-index=' . "$start&max-results=$count&min-viewability=" . "$type"; $xml = @file_get_contents($url); if (!strlen($xml)) return array(FALSE); $xml = str_replace('dc:', 'dc', $xml); $sxml = simplexml_load_string($xml); foreach($sxml->entry as $item) { $title = $item->title; $author = $item->dccreator; $pub = $item->dcpublisher; $date = $item->dcdate; $desc = $item->dcdescription; $thumb = $item->link[0]['href']; $info = $item->link[1]['href']; $preview = $item->link[2]['href']; if (!strlen($pub)) $pub = $author; if ($preview == 'http://www.google.com/books/feeds/users/me/volumes') $preview = FALSE; if (!strlen($desc)) $desc = '(No description)'; if (!strstr($thumb, '&sig=')) $thumb = 'http://books.google.com/googlebooks/' . 'images/no_cover_thumb.gif'; $results[] = array($title, $author, $pub, $date, $desc, $thumb, $info, $preview); } return array(count($results), $results);}?>