*/ class Amazon { /** * Class version * @var string component version */ public $version = '1.0'; /** * Suported Amazon Product Advertising API version * @var string */ private $api_version = '2011-08-01'; /** * API_KEY * @var string */ private $api = ''; /** * SECRET_CODE * @var string */ private $secret; /** * ASSOCIATE_TAG * @var string */ private $tag; /** * * @param array $params * @throws Exception if API_KEY is not provided * @throws Exception if SECRET_CODE is not provided * @throws Exception if ASSOCIATE_TAG is not provided */ public function __construct($params = array()) { if (!isset($params['api'])) { throw new Exception('Parameter API_KEY is not set.'); } $this->api = $params['api']; if (!isset($params['secret'])) { throw new Exception('Parameter SECRET_CODE is not set.'); } $this->secret = $params['secret']; if (!isset($params['tag'])) { throw new Exception('Parameter ASSOCIATE_TAG is not set'); } $this->tag = $params['tag']; } /** * Search items in Amazon Store by keywords and category * @param string $keywords * @param string $searchIndex * @param string $locale * @param array $params * @return SimpleXMLElement */ public function ItemSearch($keywords, $section = 'All', $locale = 'US', $params = array()) { $params += array( 'Keywords' => $keywords, 'SearchIndex' => $section, ); $request = $this->form_http_query('ItemSearch', $params, $locale); $response = simplexml_load_string(file_get_contents($request)); if ($response->Items->Request->IsValid != 'True') { return $response->Items->Request; } return $response; } /** * Method forms request url and to Amazon Product Advertising API * * @see http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/rest-signature.html * * @param string $operation * @param array $parameters * @param string $locale * @return string */ public function form_http_query($operation, $parameters = array(), $locale = 'US') { $parameters += array( 'Service' => 'AWSECommerceService', 'Version' => $this->api_version, 'AWSAccessKeyId' => $this->api, 'Operation' => $operation, 'AssociateTag' => $this->tag, 'Timestamp' => gmdate("Y-m-d\TH:i:s") . 'Z', ); ksort($parameters); $params = array(); //encode params foreach ($parameters as $key => $value) { if (is_array($value)) { $value = implode(',', $value); } $param = str_replace("%7E", "~", rawurlencode($key)); $value = str_replace("%7E", "~", rawurlencode($value)); $params[] = $param . '=' . $value; } //Sign Request $query_string = implode('&', $params); $parsed_url = parse_url($this->getLocale($locale)); $host = strtolower($parsed_url['host']); $string_to_sign = "GET\n$host\n{$parsed_url['path']}\n$query_string"; $signature = base64_encode(hash_hmac('sha256', $string_to_sign, $this->secret, TRUE)); $signature = str_replace("%7E", "~", rawurlencode($signature)); $query_string .= "&Signature=$signature"; $url = $this->getLocale($locale) . '?' . $query_string; return $url; } /** * Method from country code returns api url. * @param string $locale * @return string * @throws UnexpectedValueException */ function getLocale($locale = 'US') { switch (strtoupper($locale)) { case 'US': return 'http://ecs.amazonaws.com/onca/xml'; case 'UK': return 'http://ecs.amazonaws.co.uk/onca/xml'; case 'JP': return 'http://ecs.amazonaws.jp/onca/xml'; case 'FR': return 'http://ecs.amazonaws.fr/onca/xml'; case 'DE': return 'http://ecs.amazonaws.de/onca/xml'; case 'CA': return 'http://ecs.amazonaws.ca/onca/xml'; case 'default': throw new UnexpectedValueException(sprintf('Locale \'%s\' is not suported', $locale)); } } }