PEAR::Services_AmazonのItemSearchでキーワード検索した書籍のASINを使用してSimilarityLookupを実行し、キーワード検索した書籍の関連書籍を表示させるプログラムを作成してみました。
![]() | PHP 逆引きレシピ (PROGRAMMER’S RECiPE) | |
| 鈴木 憲治 (著) | ||
| 翔泳社 より 2009-06-30 発売 | ||
| ロープライス ¥ 2,500 or 新品 ¥ 2,730 | ||
| 関連書籍 | ![]() | よくわかるPHPの教科書 |
| ロープライス ¥ 3,333 or 新品 ¥ 2,604 | ||
| 関連書籍 | ![]() | PHPによるWebアプリケーションスーパーサンプル 第2版 |
| ロープライス ¥ 2,900 or 新品 ¥ 3,990 | ||
| 関連書籍 | ![]() | パーフェクトPHP (PERFECT SERIES 3) |
| ロープライス ¥ 3,440 or 新品 ¥ 3,780 | ||
| 関連書籍 | ![]() | 基礎からのMySQL [基礎からのシリーズ] (プログラマの種シリ… |
| ロープライス ¥ 2,700 or 新品 ¥ 3,129 | ||
| 関連書籍 | ![]() | Webサイト制作者のための PHP入門講座 |
| ロープライス ¥ 2,734 or 新品 ¥ 3,129 | ||
上記が、実際に作成したプログラムの実行結果です。HTML表示部分は、WordPressの投稿ページ内で特殊タグ([exec]〜[/exec])に挟まれた部分のPHPコードをで実行出来る「WP exec PHP」プラグインを使用して、WordPressの投稿ページに表示を行っています。
末尾に実際のソースコードを添付いたしましたが、プログラムの概要としては以下のようなことを行いました。
- 「Services/Amazon.php」をインクルードファイルとして読み込む。
- 定数として、アクセスキー、シークレットアクセスキー、アソシエイトID、Amazon ECSのバージョン、タイトルの文字数などを定義しました。Amazon ECSのバージョンは、最新の「2011-08-02」を使用することにしました。
- 変数として、PEAR::Services_AmazonのItemSearchとSimilarityLookupで使用するキーワード、サーチインデックス、ソート順、IDタイプ、ページ制御を行う変数などを定義しました。
- 定数として定義したアクセスキーID、シークレットアクセスキー、アソシエイトIDを使用して、Services_Amazonの呼び出しを行いました。
- 変数として定義したオプション(サーチインデックス、キーワード、ソート順)でItemSearchのオプション設定を行った後、ItemSearchを実行し検索結果を配列に格納しました。
- 上記の検索結果を格納した配列から、For文のループで1件の検索結果を表示させました。
- 続いて、ItemSearchを実行して得られたASIN及び変数として定義したオプション(サーチインデックス、IDタイプ)でSimilarityLookupのオプション設定を行った後、SimilarityLookupを実行し検索結果を配列に格納しました。
- 上記の検索結果を格納した配列から、For文のループで製品表示数分の検索結果を表示させました。
以下に、実際のソースコードを添付いたします。
<?php
// インクルードファイル
require_once("Services/Amazon.php");
// 定数
define('ACCESSKEY_ID', 'xxxxxxxxxxxxxxxxxxxx');
define('SECRET_ACCESSKEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
define('ASSOCIATE_ID', 'xxxxxxxxxx-22');
define('ECS_VERSION','2011-08-02');
define('RESPONSE_GROUP','Small,Images,ItemAttributes,OfferSummary,');
define('TITLE_STR_MAX','60'); // タイトル文字数
// 変数
$keyword='PEAR PHP';
$search_index='Books'; // サーチインデックス
$id_type='ISBN'; // 'ASIN' 又は 'ISBN','EAN'
$sort= 'salesrank';
$show_num= '5';
$item_page= '1';
// Services_Amazonの呼び出し
$amazon = new Services_Amazon(ACCESSKEY_ID,SECRET_ACCESSKEY,ASSOCIATE_ID);
// ItemSearchのオプション設定
$amazon->setLocale('JP');
$options = array();
$options['Keywords'] = $keyword;
$options['Sort'] = $sort;
$options['ResponseGroup'] = RESPONSE_GROUP;
$options['ItemPage'] = $item_page;
// 検索結果を配列に格納
$result = $amazon->ItemSearch($search_index, $options);
// 検索結果の表示
echo '<table border="0" width="640" cellpadding="1" cellspacing="1">';
if (!PEAR::isError($result)) {
for( $i = 0; $i < 1; $i++ ) {
//データを変数に格納
if(isset($result['Item'][$i]['DetailPageURL'])){
$detail_page_url = $result['Item'][$i]['DetailPageURL'];
}else{
$detail_page_url = '';
}
if(isset($result['Item'][$i]['MediumImage']['URL'])){
$medium_image = $result['Item'][$i]['MediumImage']['URL'];
}else{
$medium_image = '';
}
if(isset($result['Item'][$i]['MediumImage']['Width']['_content'])){
$medium_image_width = $result['Item'][$i]['MediumImage']['Width']['_content'];
}else{
$medium_image_width = '130';
}
if(isset($result['Item'][$i]['ItemAttributes']['Title'])){
$title = $result['Item'][$i]['ItemAttributes']['Title'];
}else{
$title = '無し';
}
if(isset($result['Item'][$i]['ItemAttributes']['Author'][0])){
$author = $result['Item'][$i]['ItemAttributes']['Author'][0];
}else{
$author = '無し';
}
if(isset($result['Item'][$i]['ItemAttributes']['Publisher'])){
$publisher = $result['Item'][$i]['ItemAttributes']['Publisher'];
}else{
$publisher = '';
}
if(isset($result['Item'][$i]['ItemAttributes']['PublicationDate'])){
$publication_date = $result['Item'][$i]['ItemAttributes']['PublicationDate'];
}else{
$publication_date = '';
}
if(isset($result['Item'][$i]['ItemAttributes']['ListPrice']['FormattedPrice'])){
$list_price= $result['Item'][$i]['ItemAttributes']['ListPrice']['FormattedPrice'];
}
if(isset($result['Item'][$i]['OfferSummary']['LowestNewPrice']['FormattedPrice'])){
$lowest_new_price= $result['Item'][$i]['OfferSummary']['LowestNewPrice']['FormattedPrice'];
}
if(isset($result['Item'][$i]['OfferSummary']['LowestUsedPrice']['FormattedPrice'])){
$lowest_used_price = $result['Item'][$i]['OfferSummary']['LowestUsedPrice']['FormattedPrice'];
}
if($list_price==0){
$new_price=$lowest_new_price;
}else{
$new_price=$list_price;
}
//データを表示
echo '<tr><td colspan="1" rowspan="5" width="'.$medium_image_width.'">';
echo '<a href="'.$detail_page_url.'"><img src="'.$medium_image.'"/></a></td>';
echo '<td colspan="2" rowspan="1"> <a href="'.$detail_page_url.'">'.$title.'</a></td></tr>';
echo '<tr><td colspan="2" rowspan="1"> '.$author.' (著) </td></tr>';
echo '<tr><td colspan="2" rowspan="1"> '.$publisher.' より '.$publication_date.' 発売</td></tr>';
echo '<tr><td colspan="2" rowspan="1"> <a href="'.$detail_page_url.'">ロープライス '.$lowest_used_price.'</a> or 新品 '.$new_price.'</td></tr>';
echo '<tr><td colspan="2" rowspan="1"> </td></tr>';
//ASINを$id_codeにセット
if(isset($result['Item'][$i]['ASIN'])){
$id_code=$result['Item'][$i]['ASIN'];
}
}
}
// 関係書籍の表示
// SimilarityLookupのオプション設定
$amazon->setLocale('JP');
$options = array();
$options['ResponseGroup'] = RESPONSE_GROUP;
$options['SearchIndex'] = $search_index;
$options['IdType'] = $id_type;
//検索結果を配列に格納
$result = $amazon->SimilarityLookup($id_code, $options);
//検索結果の表示
if (!PEAR::isError($result)) {
for( $i = 0; $i < $show_num; $i++ ) {
//データを変数に格納
if(isset($result['Item'][$i]['DetailPageURL'])){
$detail_page_url = $result['Item'][$i]['DetailPageURL'];
}else{
$detail_page_url = '';
}
if(isset($result['Item'][$i]['SmallImage']['URL'])){
$small_image = $result['Item'][$i]['SmallImage']['URL'];
}else{
$small_image = '-';
}
if(isset($result['Item'][$i]['SmallImage']['Width']['_content'])){
$smallimage_width = $result['Item'][$i]['SmallImage']['Width']['_content'];
}else{
$smallimage_width = '75';
}
if(isset($result['Item'][$i]['ItemAttributes']['Title'])){
$title = mb_strimwidth($result['Item'][$i]['ItemAttributes']['Title'], 0, TITLE_STR_MAX, "...");
}else{
$$title = '';
}
if(isset($result['Item'][$i]['ItemAttributes']['ListPrice']['FormattedPrice'])){
$list_price= $result['Item'][$i]['ItemAttributes']['ListPrice']['FormattedPrice'];
}
if(isset($result['Item'][$i]['OfferSummary']['LowestNewPrice']['FormattedPrice'])){
$lowest_new_price= $result['Item'][$i]['OfferSummary']['LowestNewPrice']['FormattedPrice'];
}
if(isset($result['Item'][$i]['OfferSummary']['LowestUsedPrice']['FormattedPrice'])){
$lowest_used_price = $result['Item'][$i]['OfferSummary']['LowestUsedPrice']['FormattedPrice'];
}
if($list_price==0){
$new_price=$lowest_new_price;
}else{
$new_price=$list_price;
}
//データを表示
echo '<tr>';
echo '<td rowspan="2" align="center">';
echo '関連書籍';
echo '</td>';
echo '<td align="center" colspan="1" rowspan="2" width="'.$smallimage_width.'">';
echo '<a href="'.$detail_page_url.'"><img src="'.$small_image.'"/></a>';
echo '</td>';
echo '<td style="font-size:x-small">';
echo ' <a href="'.$detail_page_url.'">'.$title.'</a>';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td style="font-size:x-small">';
echo '<a href="'.$detail_page_url.'">ロープライス '.$lowest_used_price.'</a> or 新品 '.$new_price;
echo '</td>';
echo '</tr>';
}
}
echo '</table>';
?>
上記のプログラムは、Product Advertising APIを使用していますので、実際にプログラミングを実行するためには、【amazon アソシエイト】でユーザー登録を行って、AmazonのアソシエイトID、アクセスキーID、シークレットアクセスキーの入手が必要です。

















