【PHP】Product Advertising API で商品検索をする
どうも。
今回はプログラムの覚え書きです。前回の BrowseNodeId で商品検索をしてみる。
config.json
{ "access_key_id" : "**********", "secret_access_key" : "**********", "associate_tag" : "**********" }
amazon_api_test_02.php
<?php $access_info = json_decode(file_get_contents("../config.json")); $baseurl = "http://ecs.amazonaws.jp/onca/xml"; $params = array(); $params["AWSAccessKeyId"] = $access_info->access_key_id; $params["AssociateTag"] = $access_info->associate_tag; $params["BrowseNode"] = "2400914051"; $params["ItemPage"] = "1"; $params["Operation"] = "ItemSearch"; $params["ResponseGroup"] = "Medium"; $params["SearchIndex"] = "Books"; $params["Service"] = "AWSECommerceService"; $params["Sort"] = "daterank"; $params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z"); $params["Version"] = "2011-08-01"; // sort the parameters ksort($params); // create the canonicalized query $canonical_string = ""; foreach ($params as $param => $value) { $param = str_replace("%7E", "~", rawurlencode($param)); $value = str_replace("%7E", "~", rawurlencode($value)); $canonical_string .= "&" . $param . "=" . $value; } $canonical_string = substr($canonical_string, 1); // create the string to sign $method = "GET"; $host = parse_url($baseurl)["host"]; $path = parse_url($baseurl)["path"]; $string_to_sign = $method . "\n" . $host . "\n" . $path . "\n" . $canonical_string; // calculate HMAC with SHA256 and base64-encoding $signature = base64_encode(hash_hmac("sha256", $string_to_sign, $access_info->secret_access_key, True)); // encode the signature for the request $signature = str_replace("%7E", "~", rawurlencode($signature)); // create request url $request_url = $baseurl . "?" . $canonical_string . "&Signature=" . $signature; echo $request_url; ?>
前回のソースコードとほぼ一緒です。異なる部分はパラメータの指定部分だけなので・・・
前回
$params = array(); $params["AWSAccessKeyId"] = $access_info->access_key_id; $params["AssociateTag"] = $access_info->associate_tag; $params["BrowseNodeId"] = "2128664051"; $params["Operation"] = "BrowseNodeLookup"; $params["Service"] = "AWSECommerceService"; $params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z"); $params["Version"] = "2011-08-01";
今回
$params = array(); $params["AWSAccessKeyId"] = $access_info->access_key_id; $params["AssociateTag"] = $access_info->associate_tag; $params["BrowseNode"] = "2400914051"; $params["ItemPage"] = "1"; $params["Operation"] = "ItemSearch"; $params["ResponseGroup"] = "Medium"; $params["SearchIndex"] = "Books"; $params["Service"] = "AWSECommerceService"; $params["Sort"] = "daterank"; $params["Timestamp"] = gmdate("Y-m-d\TH:i:s\Z"); $params["Version"] = "2011-08-01";
この部分だけですね。
これで可能な限りピンポイントで検索することが出来たのではないか?と思うのですが、電子書籍版が含まれているようで。要素の値で判別する方法しかないみたい。API 側で対応できたら良いのに。
(でも取得出来ているのだから、活用してみるのも・・・)
次は取得したXMLを解析しないとね。ヽ(゜▽、゜)ノ
以上です。