1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Google Books APIを用いた書籍検索

Posted at

はじめに

私が,初めてAPIを用いたアプリ開発したものを紹介します.今回は簡単に,ユーザからクエリを取得し,GoogleBooksAPIを叩いて取得した書籍情報を画面に表示するプログラムの一部を紹介します.言語はPHPを使用しています.
※CSSは省略しています.

流れ

書籍検索の流れは以下です.

・クエリ情報の取得
・APIから情報の取得と表示

クエリの入力

クエリは今回,タイトルと著者の情報を取得することにします.タイトルと著者情報を取得し,GoogleBooksAPIを叩くファイルにformで送信しています.

<div>
	<h1>Search Book</h1>
	<hr>
</div>
<div>
	<form action="パス名" method="post" enctype="multipart/form-data">	
		<div>
			<h2>Title:</h2>
			<textarea name="bookTitle" value maxlength="100" placeholder="Title"></textarea>
		</div>
		<div>
			<h2>Author:</h2>
			<textarea name="author" value maxlength="100" placeholder="Author"></textarea>
		</div>
		<button type="submit" name="submit">Send</button>
	</form>
</div>

タイトルと著者のほかにも以下の情報で検索をかけることができます.詳細はこちら

検索可能情報
テキスト文字列 説明
intitle タイトルの検索
inauthor 著者の検索
inpublisher 出版社の検索
subject カテゴリリストの検索
isbn ISBN番号の検索
lccn 米国議会図書館管理番号の検索
oclc Online Computer Library Center 番号の検索

APIから情報の取得と表示

GoogleBooksAPIにタイトルと著者情報のクエリを送信して,json形式で取得し,object型に変換します.このとき,クエリに合致した本が見つからなかった時の処理を随時追加してください.

書籍情報の取得

// 検索条件を配列にする
$params = array(
  'intitle'  => $_POST["bookTitle"],
  'inauthor' => $_POST["author"],
);

// 半角,全角の空白削除
$params["intitle"] = str_replace(array(" ", " "), "", $params["intitle"]);
$params["inauthor"] = str_replace(array(" ", " "), "", $params["inauthor"]);

$maxResults = 40;
$startIndex = 0;

$base_url = 'https://www.googleapis.com/books/v1/volumes?q=';

// クエリ未入力の時
if($params["intitle"] == "" && $params["inauthor"] == ""){
	set_message(MESSAGE_NO_QUERY);
	header('Location: post_search.php');
	exit();
}

// クエリが未入力でなければURLに追加
if($params["intitle"] != ""){
	$base_url .= 'intitle:' . $params["intitle"] . '+';
}
if($params["inauthor"] != ""){
	$base_url .= 'inauthor:' . $params["inauthor"] . '+';
}

$params_url = substr($base_url, 0, -1);  // 末尾につく「+」を削除
$url = $params_url.'&maxResults='.$maxResults.'&startIndex='.$startIndex; // 最大40冊取得
$json = file_get_contents($url);  // 書籍情報を取得
$data = json_decode($json);  // デコード(objectに変換)

if(isset($data->items)){
	$books = $data->items;
}

書籍情報の表示

・タイトルが長すぎるものはstrlenを使って...で省略する
・著者は3人目以降を...で省略する
・さらに,コメント数を取得して表示している

function get_author($book){
	$author_count = 0;
	$all_author = "";
	
	foreach ($book->volumeInfo->authors as $author) {
		if ($author_count < 2) {
			$all_author .= ($all_author ? ' ' : '') . $author;
		}
		$author_count++;
	}
	
	if ($author_count > 2) {
		$all_author .= ' ...etc';
	}
	
	return $all_author;
}

function create_block($book, $count){
	$image_link = $book->volumeInfo->imageLinks->thumbnail;
	$title = $book->volumeInfo->title;
	$author = get_author($book);

	if(strlen($title) > 23) {
		$title = mb_substr($title, 0, 6, "UTF-8") . '...';
	}
	print("
		<img src=$image_link alt='画像'>
		<div>
    		<hr>
			<h4> <b>『 $title 』</b> </h4>
			著者:$author ");
    	print("</div>");
}

タイトルを「こころ」,著者を「夏目漱石」で検索した結果の一部
image.png

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?