_service = "AWSECommerceService"; $this->_access_key_id = $access_key; $this->_secret_key = $secret_key; $this->_version = $version; $this->_tracking_id = $tracking_id; $this->_uri = "webservices.amazon.co.jp"; $this->_end_point = "/onca/xml"; $this->_amazon_base_url = "http://" . $this->_uri . $this->_end_point; } /** * アイテムを返す * @param string $asin アマゾンのアイテムID * @param array $options ItemLookupのオプション * @return mixed */ public function itemLookup($asin, $options=array()) { $options['Operation'] = "ItemLookup"; $options['ItemId'] = $asin; $item = $this->_sendRequest($options); return $item; } /** * アイテムを検索する * @param string $keywords 検索文字列 * @param array $options ItemSearchのオプション * @return mixed */ public function itemSearch($keywords, $options=array()) { $options['Operation'] = "ItemSearch"; $options['SearchIndex'] = "Blended"; $options['ResponseGroup'] = "Medium"; $options['Keywords'] = $keywords; $items = $this->_sendRequest($options); return $items; } /** * リクエストを送信する * @param array $options * @return mixed */ private function _sendRequest($options=array()) { $options['Service'] = $this->_service; $options['AWSAccessKeyId'] = $this->_access_key_id; $options['AssociateTag'] = $this->_tracking_id; $options['Version'] = $this->_version; // タイムスタンプをセットする $options['Timestamp'] = gmdate('c'); // オプションをアルファベット順にソートする ksort($options); // 署名作成 foreach($options as $key => $val) { $canonical_string .= "&" . $key . "=" . rawurlencode($val); } $canonical_string = substr($canonical_string, 1); $string_to_sign = trim("GET\n{$this->_uri}\n{$this->_end_point}\n{$canonical_string}"); $signature = base64_encode(hash_hmac('sha256', $string_to_sign, $this->_secret_key, true)); $url = $this->_amazon_base_url . '?' . $canonical_string . '&Signature=' . rawurlencode($signature); $file = fopen ($url, "r"); while(!feof($file)) { $line .= fgets($file); } fclose($file); return $line; } public function xml2json($xml, $callback = "") { $res = str_replace("\n", "", $xml); $res = simplexml_load_string($res); $items = $res->Items->Item; $res = "{Item:["; for($i = 0; $i < count($items); $i++) { $item = $items[$i]; $res .= "{"; $res .= "Title:'" . $item->ItemAttributes->Title . "',"; $res .= "Price:'" . $item->ItemAttributes->ListPrice->Amount . "',"; $res .= "URL:'" . $item->DetailPageURL . "',"; $res .= "Image:'" . $item->SmallImage->URL . "'"; $res .= "}"; if($i != count($items) - 1) $res .= ","; } $res .= "]}"; if($callback) { $res = "$callback($res)"; } return $res; } } $access_key = "07W0SYB25G2MZJRMAN82"; $secret_key = "ag58cD0WAw5s0L9QeTxGdb8VGrISKCE2xhaWK8F1"; $tracking_id = "shu0119"; $asin = ""; $keywords = $_GET["keywords"]; $apa = new AmazonProductAdvertising($access_key, $secret_key, $tracking_id); $xml = $apa->itemSearch($keywords); $jsonp = $apa->xml2json($xml, $_GET["callback"]); header("Content-Type: text/javascript; charset=utf-8"); echo $jsonp; ?>