插件79:搜索Google图书

<?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);
}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值