AmazonWebサービス(Product Advertising API)に入門する。
目標
キーワードなどに連動して、人気のランキングを出力するブログパーツを作る。
→サンプルサイト
まず
http://www.ajaxtower.jp/ecs/を見ながら、勉強と下準備。
amazonAPIを利用するには、
・Amazonアソシエイト・プログラムへの参加
・Access Key ID
・Secret Access Keyの取得
が必要。
アソシエイトは参加済みなので、API参加し、IDとKEYを取得。
手順に従い、リクエストコードを取得↓
1 2 3 4 5 6 7 8 9 | http://ecs.amazonaws.jp/onca/xml? Service=AWSECommerceService &AWSAccessKeyId=[AccessKeyId] &AssociateTag=[AssociateID] &Operation=ItemSearch &SearchIndex=Books &Title=HarryPotter &Version=2009-07-01 &Timestamp=2012-11-13T10%3A38%3A55.000Z &Signature=QhXHRaqbahhbyDIHwL4hWDi9WHJNlbwALLQJ2OQNVmY%3D |
これをurlに入れると、XMLで出力される。タイムスタンプと署名は毎回取得しなければいけないらしく、そこんとこが厄介そう。
最終目標としては、amazonのリンクを作ってくれる、ウェブサービスを提供したい。
Services_Amazon
PEARのライブラリ『Services_Amazon』ならAmazon APIを簡単に使えるらしい。
ので、下記サイトを参考に作ってみた。
PEARの導入参考:http://blue-spiral.matrix.jp/20110528-pear-in-sakura.html
PHP処理プログラムコード初心者向け参考:http://nplll.com/archives/2011/06/_pearservices_amazon.php
PHP処理プログラムコード参考:http://or2.to/2012/04/service-amazon-2.html
入力フォーム参考:「Amazones」(http://aokobe.sakura.ne.jp/php/amazones.php)
最終コード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <?php set_include_path(get_include_path() . PATH_SEPARATOR . '絶対パス' ); require_once ( 'Services/Amazon.php' ); // インクルードファイル header( "Content-type: application/x-javascript" ); //js $amazon = new Services_Amazon( 'XXXXXX' , 'XXXXXX' , 'XXXXXX-22' ); // 『Services_Amazon』の定形 $category = $_GET [ 'category' ]; //JavaScriptで飛ばしてくる変数。 $param = $_GET [ 'param' ]; $key = $_GET [ 'key' ]; $id = $_GET [ 'id' ]; $number = $_GET [ 'number' ]; $select = $_GET [ 'select' ]; $sort_select = $_GET [ 'sort_select' ]; if (! empty ( $key )){ //もしキーワードがあれば検索。 $result = $amazon ->ItemSearch( $category , array ( 'Keywords' => $key , 'ResponseGroup' => 'ItemAttributes, Images' )); } elseif ( $sort_select == 'daterank' ){ //もしキーワードが空ならリリース日順。 $result = $amazon ->ItemSearch( $category , array ( 'BrowseNode' => $param , 'Sort' => 'daterank' , 'ResponseGroup' => 'ItemAttributes, Images' )); } elseif ( empty ( $key )){ //もしキーワードが空ならランキング。 $result = $amazon ->ItemSearch( $category , array ( 'BrowseNode' => $param , 'Sort' => 'salesrank' , 'ResponseGroup' => 'ItemAttributes, Images' )); } for ( $i = 0; $i < $number ; $i ++ ){ $asin = $result [ 'Item' ][ $i ][ 'ASIN' ]; $gazolink = "<div class='azimg'><a target='_blank' href=http://www.amazon.co.jp/exec/obidos/ASIN/" . $asin . "/" . $id . "/ref=nosim/><img src='" . $result ['Item '][$i][' LargeImage '][' URL ']."' ></a></div>"; $titlelink = "<div class='aztitle'><a target='_blank' href=http://www.amazon.co.jp/exec/obidos/ASIN/" . $asin . "/" . $id . "/ref=nosim/>" . $result [ 'Item' ][ $i ][ 'ItemAttributes' ][ 'Title' ]. "</a></div>" ; if ( $result [ 'Item' ][ $i ][ 'ItemAttributes' ][ 'Title' ] == "" ) { break ;} //タイトルが空白なら、繰り返しがBREAK。 //▼//$selectで表示方法を分岐。 if ( $select == 'txt' ){ //テキストだけ $mix = "<div class='az'>" . $titlelink . "</div>" ; } elseif ( $select == 'img' ){ //画像だけ $mix = "<div class='az'>" . $gazolink . "</div>" ; } else { //全部 $mix = "<div class='az'>" . $gazolink . $titlelink . "</div>" ; } //▲// $output = $output . $mix ; //ループさせるための自己読み込み } echo "document.write(\"$output\")" ; ?> |