PHPで作るAmazon商品検索スクリプト(結果がJSON)
こんばんは、私です。
今日は簡単にソースを公開します。
どんなのかと言うと、AWSECommerceServiceのItemLookupを内包してJSONで結果を返すって物です。
先日、2016年8月29日「AWSECommerceServiceを使う方法」にてAWSECommerceServiceの使い方ってのを解説した。
今回はそれの実装である。
リクエストとしてISBN(バーコード)を指定してやれば、JSON形式のItem情報が出てきまっす。
<?php header("Cache-Control: no-cache, must-revalidate"); header("Content-type: text/html; charset=utf-8"); $ISBN = $_GET["isbn"]; require_once("./Book.php"); require_once("./Author.php"); $request = createURL($ISBN); $response = file_get_contents($request); $parsed_xml = simplexml_load_string($response); // Get AWS 判定 if( $parsed_xml->Items->Request->IsValid != 'True' ) return -1; $Item = $parsed_xml->Items->Item; $Attributes = $Item->ItemAttributes; $book = new Book(); $book->isbn = (string)$Attributes->ISBN; $book->asin = (string)$Item->ASIN; $book->ean = (string)$Attributes->EAN;; $book->title = (string)$Attributes->Title; $book->image_url = (string)$Item->MediumImage->URL; $book->pubdate = (string)$Attributes->PublicationDate; $book->price = (string)$Attributes->ListPrice->Amount; $book->pages = (string)$Attributes->NumberOfPages; $book->publisher = (string)$Attributes->Publisher; $book->binding = (string)$Attributes->Binding; $book->currency_cd = (string)$Attributes->ListPrice->CurrencyCode; $author_name = array(); foreach( $Attributes->Author as $author_name ) { array_push($author_name, $author_name); } foreach( $Attributes->Creator as $creator ) { array_push($author_name, $creator); } $author_array = array(); for ( $i=0; $i<sizeof($author_name); $i++ ) { $author = new Author(); $author->author = (string)($author_name[$i]); $author->role = (string)($author_name[$i]['Role']); $author_array[] = $author; } $book->authors = $author_array; if ( !$book->isbn ) { echo " No book found for ISBN =" . $ISBN . " in Amazon DB <br></br>"; $book = NULL; $author_array=NULL; return; } echo json_encode($book,JSON_UNESCAPED_UNICODE); $book = NULL; $author_array=NULL; ?>リクエストを出して入れ物に入れているだけのシンプル構造です。
<?php function createURL($isbn){ $config = parse_ini_file("awsaccount.ini",true); // Amazon Sample // http://webservices.amazon.co.jp/scratchpad/index.html // Your AWS Access Key ID, as taken from the AWS Your Account page $aws_access_key_id = $config['amazon']['AccessKeyId']; // Your AWS Secret Key corresponding to the above ID, as taken from the AWS Your Account page $aws_secret_key = $config['amazon']['SecretKey']; $associate_tag = $config['amazon']['AssociateTag']; // The region you are interested in $endpoint = "webservices.amazon.co.jp"; $uri = "/onca/xml"; $params = array( "Service" => "AWSECommerceService", "Operation" => "ItemLookup", "AWSAccessKeyId" => $aws_access_key_id , "AssociateTag" => $associate_tag , "ResponseGroup" => "Large", "ItemId" => $isbn , "SearchIndex" => "Books", "IdType" => "ISBN" ); // Set current timestamp if not set if (!isset($params["Timestamp"])) { $params["Timestamp"] = gmdate('Y-m-d\TH:i:s\Z'); } // Sort the parameters by key ksort($params); $pairs = array(); foreach ($params as $key => $value) { array_push($pairs, rawurlencode($key)."=".rawurlencode($value)); } // Generate the canonical query $canonical_query_string = join("&", $pairs); // Generate the string to be signed $string_to_sign = "GET\n".$endpoint."\n".$uri."\n".$canonical_query_string; // Generate the signature required by the Product Advertising API $signature = base64_encode(hash_hmac("sha256", $string_to_sign, $aws_secret_key, true)); // Generate the signed URL $request_url = 'http://'.$endpoint.$uri.'?'.$canonical_query_string.'&Signature='.rawurlencode($signature); return $request_url; } ?>function createURLは先日のBlogの最後に書いている
amazon scratchpad
にサンプルが表示されますがそれを関数にしています。
アクセス情報をawsaccount.iniファイルにはき出させてます。awsaccount.ini
[amazon] AccessKeyId = accessS AssociateTag = mmppwataru01-22 SecretKey = hogehogeBook.php
<?php class Book { public $isbn = ""; public $asin = ""; public $ean = ""; public $title = ""; public $image_url = ""; public $pubdate = ""; public $price = ""; public $page = ""; public $publisher = ""; public $binding = ""; public $currency_cd = ""; public $authors = array(); } ?>Author.php
<?php Class author { public $author = ""; public $role = ""; public function setAuthor($author) { $this->author = $author; } public function getRole() { return $this->role; } public function setRole($role) { $this->role = $role; } } ?>難しそうですがシンプルですわん
JSONなのでAJAXで通信させれば便利になりました。これで世界が加速した。