Amazon Web Service(AWS:アマゾンウェブサービス)を利用したPHP4記述の商品検索プログラムソース公開Amazon Web Service(AWS:アマゾンウェブサービス)を利用したPHP4記述の商品検索プログラムソースを公開しています。PHP4でXMLモジュール(xml_parser等)が使用できない環境でも動作します。「AWSのアカウントは取ったけれども、使い方がよくわからない」「PHPでどのようにAWSのサイトを構築したらいいのか?」という方は是非ご参照ください。■ サイト名:『KUNISAN.JPショップ』 URL: //kunisan.jp/shop/ ■ メインプログラム (プログラム名: "index.php") <html> <?php //タイトルの取得 $title = $_GET[TITLE]; //自動的に\が入ることがあるので、それを除去する処理 $title = stripcslashes($title); //検索ワードの取得 $word = $_GET[WORD]; //自動的に\が入ることがあるので、それを除去する処理 $word = stripcslashes($word); print '<head>'; if ($title != ""){ print '<title>'.$title.' / KUNISAN.JPショップ</title>'; } elseif ($word != "") { print '<title>'.$word.' / KUNISAN.JPショップ</title>'; } else { print '<title>KUNISAN.JPショップ</title>'; } print '</head>'; print '<meta name="description" content="KUNISAN.JPショップのページです。'.$title.'について">'; ?> <meta name="keywords" content="ショップ,販売,売,買,書籍,本,CD,DVD,ゲーム"> <meta http-equiv="Content-Type" content="text/html; charset=Shift-JIS"> <body> <?php //ページ番号の取得 $page = $_GET[PAGE]; if ($page == ""){ $page = 1; } //ノードIDの取得 $node = $_GET[NODE]; //サーチインデックスの取得 $search = $_GET[SEARCH]; if ($search == ""){ $search = 'Blended'; } $selected[$search] = 'selected'; //商品の説明を出すかの取得 $contents = $_GET[CONTENTS]; $checked[$contents] = 'checked'; ?> <center> <iframe src="https://rcm-jp.amazon.co.jp/e/cm?t=kj036-22&o=9&p=48&l=ur1&category=amazongeneral&banner=0VB4JABQ47S9T0RTCHG2&f=ifr" width="728" height="90" scrolling="no" border="0" marginwidth="0" style="border:none;" frameborder="0" > </iframe> <br> <br> <table cellpadding="0" cellspacing="0" border="0"> <tr><td width="728"> <h1> <a href="index.php" style="text-decoration:none"> <font color="888888">大きな画像と文字の<b>KUNISAN.JPショップ[β版]</b></font><br> </a> </h1> <form action="index.php" method="get" name="form1" target="_top"> <b>サーチ</b> <?php print '<select name="SEARCH">'; print '<option value="Books" '.$selected['Books'].'>和書</option>'; print '<option value="ForeignBooks" '.$selected['ForeignBooks'].'>洋書</option>'; print '<option value="Electronics" '.$selected['Electronics'].'>エレクトロニクス</option>'; print '<option value="Kitchen" '.$selected['Kitchen'].'>ホーム&キッチン</option>'; print '<option value="Music" '.$selected['Music'].'>音楽CD</option>'; print '<option value="Classical" '.$selected['Classical'].'>クラシックCD</option>'; print '<option value="DVD" '.$selected['DVD'].'>DVD</option>'; print '<option value="VHS" '.$selected['VHS'].'>VHSビデオ</option>'; print '<option value="Video" '.$selected['Video'].'>ビデオ</option>'; print '<option value="VideoGames" '.$selected['VideoGames'].'>ゲーム</option>'; print '<option value="Software" '.$selected['Software'].'>ソフトウェア</option>'; print '<option value="Toys" '.$selected['Toys'].'>おもちゃ</option>'; print '<option value="HealthPersonalCare" '.$selected['HealthPersonalCare'].'>ヘルス&ビューティ</option>'; print '</select>'; print ' <input type="text" name="WORD" size="40" value="'.$word.'">'; print ' <input type="submit" value="検索"><br>'; ?> </form> <?php //検索ワードのエンコード変換 $word = mb_convert_encoding($word, "UTF-8","SJIS"); //And条件は" + "で記述すること //Or条件は" | "で記述すること $word = mb_eregi_replace(' ','+',$word); $word = mb_eregi_replace(' ','+',$word); //商品検索プログラムのアドレス指定(★AccessKeyIDはご自分のものを入れてください) $addr = 'http://webservices.amazon.co.jp/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXXX&AssociateTag=kj036-22&Operation=ItemSearch&SearchIndex='.$search.'&ResponseGroup=Large&Sort=salesrank&Keywords='.$word.'&ItemPage='.$page.'&BrowseNode='.$node; //ホームページのHTML取得 $xml = file_get_contents($addr); //結果表示のUTF-8エンコードをSJISエンコードに変換する $xml = mb_convert_encoding($xml, "SJIS", "UTF-8"); //総ページ数の取得 $zd2 = spliti('<TotalPages>',$xml); $zd3 = spliti('</TotalPages>',$zd2[1]); $total_pages = $zd3[0]; //各情報の取得 $zd1 = spliti('<Item>',$xml); //HTML表示 if (($word != "")&&($search != 'Blended')) { print '<table width="728"> '; if ($title != "") { print '<tr><td colspan="2" height="1" bgcolor="#cccccc"> </td></tr>'; print '<tr><td colspan="2" height="4" bgcolor="#ffffff"> </td></tr>'; print '<tr><td colspan="2"><b>'.$title.'</b></td></tr>'; print '<tr><td colspan="2" height="3" bgcolor="#ffffff"> </td></tr>'; print '<tr><td colspan="2" height="1" bgcolor="#cccccc"> </td></tr>'; print '<tr><td colspan="2" height="12" bgcolor="#ffffff"> </td></tr>'; } else { print '<tr><td colspan="2" height="1" bgcolor="#cccccc"> </td></tr>'; print '<tr><td colspan="2" height="4" bgcolor="#ffffff"> </td></tr>'; print '<tr><td colspan="2"><b>検索結果</b></td></tr>'; print '<tr><td colspan="2" height="3" bgcolor="#ffffff"> </td></tr>'; print '<tr><td colspan="2" height="1" bgcolor="#cccccc"> </td></tr>'; print '<tr><td colspan="2" height="12" bgcolor="#ffffff"> </td></tr>'; } //商品最大10件分表示のため、その分のループ for ($i = 1; $i <= 10; $i++) { //ページのタイトル取得 $zd2 = spliti('<Title>',$zd1[$i]); $zd3 = spliti('</Title>',$zd2[1]); $zd_data[$i]["Title"] = $zd3[0]; //ブランドの取得 $zd2 = spliti('<Brand>',$zd1[$i]); $zd3 = spliti('</Brand>',$zd2[1]); $zd_data[$i]["Brand"] = $zd3[0]; //著者の取得 $zd2 = spliti('<Author>',$zd1[$i]); $zd3 = spliti('</Author>',$zd2[1]); $zd_data[$i]["Author"] = $zd3[0]; //アーティスト名の取得 $zd2 = spliti('<Artist>',$zd1[$i]); $zd3 = spliti('</Artist>',$zd2[1]); $zd_data[$i]["Artist"] = $zd3[0]; //曲目の取得 for ($j = 1; $j <= 10; $j++) { $zd2 = spliti('<Disc Number="'.$j.'">',$zd1[$i]); $zd3 = spliti('</Disc>',$zd2[1]); if ($zd3[0] != "") { $zd_data[$i]["Music_Contents"] = $zd_data[$i]["Music_Contents"].'<b>ディスク'.$j.'</b><br>'; for ($k = 1; $k <= 32; $k++) { $zd4 = spliti('<Track Number="'.$k.'">',$zd3[0]); $zd5 = spliti('</Track>',$zd4[1]); if ($zd5[0] != "") { $zd_data[$i]["Music_Contents"] = $zd_data[$i]["Music_Contents"].$k.' '.$zd5[0].'<br>'; } } } } //詳細ページのURL取得 $zd2 = spliti('<DetailPageURL>',$zd1[$i]); $zd3 = spliti('</DetailPageURL>',$zd2[1]); $zd_data[$i]["DetailPageUrl"] = $zd3[0]; //大型画像のアドレス取得 $zd2 = spliti('<LargeImage><URL>',$zd1[$i]); $zd3 = spliti('</URL>',$zd2[1]); $zd_data[$i]["LargeImageUrl"] = $zd3[0]; //価格取得 $zd2 = spliti('<FormattedPrice>',$zd1[$i]); $zd3 = spliti('</FormattedPrice>',$zd2[1]); $zd_data[$i]["Price"] = $zd3[0]; //在庫状況 $zd2 = spliti('<Availability>',$zd1[$i]); $zd3 = spliti('</Availability>',$zd2[1]); $zd_data[$i]["Availability"] = $zd3[0]; //仕様の取得 //$zd2 = spliti('<Feature>',$zd1[$i]); //for ($j = 1; $j <= 20; $j++) { // $zd3 = spliti('</Feature>',$zd2[j]); // if ($zd3[0] != ""){ // $zd_data[$i]["Feature"] = $zd_data[$i]["Feature"].'・'.$zd3[0].'<br>'; // } //} //説明の取得 //(1つめ) $zd4 = spliti('<EditorialReview>',$zd1[$i]); $zd5 = spliti('</EditorialReview>',$zd4[1]); $zd2 = spliti('<Content>',$zd5[0]); $zd3 = spliti('</Content>',$zd2[1]); $zd3[0] = mb_eregi_replace('<','<',$zd3[0]); $zd3[0] = mb_eregi_replace('>','>',$zd3[0]); $zd_data[$i]["Content"] = $zd_data[$i]["Content"].$zd3[0]; $zd_data[$i]["Content"] = $zd3[0]; //(2つめ。主にAmazonの商品紹介) $zd5 = spliti('</EditorialReview>',$zd4[2]); $zd2 = spliti('<Source>',$zd5[0]); $zd3 = spliti('</Source>',$zd2[1]); if ($zd3[0] != "") { $zd_data[$i]["Content"] = $zd_data[$i]["Content"].'<br><b>'.$zd3[0].'</b>'; } $zd2 = spliti('<Content>',$zd5[0]); $zd3 = spliti('</Content>',$zd2[1]); $zd3[0] = mb_eregi_replace('<','<',$zd3[0]); $zd3[0] = mb_eregi_replace('>','>',$zd3[0]); if ($zd3[0] != "") { $zd_data[$i]["Content"] = $zd_data[$i]["Content"].'<br>'.$zd3[0]; } if ($zd_data[$i]["Title"] != "") { print '<tr>'; print '<td valign="top">'; print '<a href="'.$zd_data[$i]["DetailPageUrl"].'" target="_blank">'; if ($zd_data[$i]["LargeImageUrl"] != "") { print '<img src="'.$zd_data[$i]["LargeImageUrl"].'" width="300" border="0"> '; } else { print '<img src="no_image.gif" width="300" border="0"> '; } print '</a>'; print '<br>'; print '<br>'; print '</td>'; print '<td valign="top">'; print '<big>'; print '<b>'; print '<a href="'.$zd_data[$i]["DetailPageUrl"].'" target="_blank">'; print $zd_data[$i]["Title"]; print '</a>'; print '</b>'; print '</big>'; print '<br>'; if ($zd_data[$i]["Brand"] != "") { print $zd_data[$i]["Brand"]; print '<br>'; } if ($zd_data[$i]["Author"] != "") { print $zd_data[$i]["Author"].' (著)'; print '<br>'; } if ($zd_data[$i]["Artist"] != "") { print $zd_data[$i]["Artist"]; print '<br>'; } if ($zd_data[$i]["Music_Contents"] != "") { print $zd_data[$i]["Music_Contents"]; print '<br>'; } print '<b>価格</b>: '.$zd_data[$i]["Price"]; print '<br>'; print '<b>在庫状況</b>: '.$zd_data[$i]["Availability"]; print '<br>'; //if ($zd_data[$i]["Feature"] != "") { // print '仕様:<br>'; // print $zd_data[$i]["Feature"]; //} if ($zd_data[$i]["Content"] != "") { print '<b>商品の説明</b>:<br>'; print $zd_data[$i]["Content"]; } //print $zd_data[$i]["Contents"]; print '<br>'; print '<a href="'.$zd_data[$i]["DetailPageUrl"].'" target="_blank">'; print 'この商品の詳細をAmazon.co.jpでチェック'; print '</a>'; print '<br>'; print '</td>'; print '</tr>'; print '<tr><td colspan="2" height="12" bgcolor="#ffffff"> </td></tr>'; print '<tr><td colspan="2" height="1" bgcolor="#cccccc"> </td></tr>'; print '<tr><td colspan="2" height="12" bgcolor="#ffffff"> </td></tr>'; } } print '</table>'; //検索ワードのエンコード変換 $word = mb_convert_encoding($word,"SJIS","UTF-8"); //ページ受け渡し処理 print '<center>'; print '<table><tr>'; print '<td>'; print 'トータル'.$total_pages.'ページ中の'.$page.'ページ目'; print '</td>'; print '<td width="32"> </td>'; print '<td>'; if ($page > 1) { $prev_page = $page - 1; print '| <a href="?PAGE='.$prev_page.'&SEARCH='.$search.'&WORD='.$word.'&CONTENTS='.$contents.'&TITLE='.$title.'&NODE='.$node.'">'; print '前ページを見る'; print '</a> |'; } else { print '| <font color="#888888">'; print '前ページを見る'; print '</font> |'; } print '</td>'; print '<td>'; if ($page < $total_pages) { $next_page = $page + 1; print ' <a href="?PAGE='.$next_page.'&SEARCH='.$search.'&WORD='.$word.'&CONTENTS='.$contents.'&TITLE='.$title.'&NODE='.$node.'">'; print '次ページを見る'; print '</a> |'; } else { print ' <font color="#888888">'; print '次ページを見る'; print '</font> |'; } print '</td>'; print '</tr></table>'; print '<br>'; print '| <a href="index.php">トップページ</a> |'; print '</center>'; } else { } ?> </td></tr></table> <br> <iframe src="https://rcm-jp.amazon.co.jp/e/cm?t=kj036-22&o=9&p=48&l=ur1&category=amazongeneral&banner=0VB4JABQ47S9T0RTCHG2&f=ifr" width="728" height="90" scrolling="no" border="0" marginwidth="0" style="border:none;" frameborder="0" > </iframe> </center> <br> <br> <center>Copyright (C) 2008 <a href="//kunisan.jp/" target=_"blank">KUNISAN.JP</a>. All Rights Reserved.</center> <br> <br> <br> </body> </html>
|