Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set('display_errors',1);
- class MWSProduct{
- private $keys = array();
- private $serviceURL = "https://mws-eu.amazonservices.com/Products/2011-10-01";
- public function __construct($key, $secret, $name, $version, $merchant, $marketplace) {
- $this->keys = array('ACCESS_KEY'=>$key,
- 'SECRET_KEY'=>$secret,
- 'APP_NAME'=>$name,
- 'APP_VERSION'=>$version,
- 'MERCHANT_ID'=>$merchant,
- 'MARKETPLACE_ID'=>$marketplace);
- }
- private function mwsRequestURL($request, $version = '2011-10-01') {
- // breaks the url appart into its components (query, host etc)
- $uri_elements = parse_url($request);
- // pulls the querystring out from the array of url elements
- $request = $uri_elements['query'];
- // parses the string as if it were a querystring and passes it into an array
- parse_str($request, $parameters);
- // Add the new required paramters
- $parameters['Timestamp'] = gmdate("Y-m-d\TH:i:s\Z");
- $parameters['Version'] = $version;
- //adds the access key if it hsa been sent
- $parameters['AWSAccessKeyId'] = $this->keys['ACCESS_KEY'];
- // The new authentication requirements need the keys to be sorted (by byte value)
- ksort($parameters);
- // loops through the array, and breaks apart the parameter and key at the same time
- foreach ($parameters as $parameter => $value) {
- // We need to be sure we properly encode the value of our parameter
- $parameter = str_replace("%7E", "~", rawurlencode(str_replace('_', '.', $parameter)));
- $value = str_replace("%7E", "~", rawurlencode($value));
- $request_array[] = $parameter . '=' . $value;
- }
- // Put our & symbol at the beginning of each of our request variables and put it in a string
- $new_request = implode('&', $request_array);
- // Create our signature string
- $signature_string = "GET\n{$uri_elements['host']}\n{$uri_elements['path']}\n{$new_request}";
- // Create our signature using hash_hmac
- $signature = urlencode(base64_encode(hash_hmac('sha256', $signature_string, $this->keys['SECRET_KEY'], true)));
- // Return our new request
- return "https://{$uri_elements['host']}{$uri_elements['path']}?{$new_request}&Signature={$signature}";
- }
- public function cheapestPricing($asins) {
- $call = $this->serviceURL;
- $call.= "?AWSAccessKeyId=".$this->keys['ACCESS_KEY'];
- $call.= "&Action=GetLowestOfferListingsForASIN";
- $call.= "&SellerId=".$this->keys['MERCHANT_ID'];
- $call.= "&SignatureVersion=2";
- $call.= "&SignatureMethod=HmacSHA256";
- $call.= "&MarketplaceId=".$this->keys['MARKETPLACE_ID'];
- $call.= "&ExcludeMe=true";
- $count = 0;
- foreach($asins as $asin) {
- $count++;
- $call.= "&ASINList.ASIN.{$count}={$asin}";
- }
- $call = $this->mwsRequestURL($call);
- $response = file_get_contents($call);
- $response = simplexml_load_string($response);
- if(isset($response->GetLowestOfferListingsForASINResult->Error)) {
- return false;
- } else {
- $prices = array();
- foreach($response->GetLowestOfferListingsForASINResult as $product) {
- $asin = (string) $product->Product->Identifiers->MarketplaceASIN->ASIN;
- $prices[$asin] = array('new'=>array(),'used'=>array());
- foreach($product->Product->LowestOfferListings->LowestOfferListing as $price) {
- $cond = (string) strtolower($price->Qualifiers->ItemCondition);
- $sale = (float) $price->Price->ListingPrice->Amount;
- $ship = (float) $price->Price->Shipping->Amount;
- $landed = number_format($sale + $ship, 2, '.', '');
- $prices[$asin][$cond][] = array('landed'=>$landed, 'price'=>$sale, 'shipping'=>$ship);
- }
- }
- return $prices;
- }
- }
- public function getProduct($query) {
- $call = $this->serviceURL;
- $call.= "?AWSAccessKeyId=".$this->keys['ACCESS_KEY'];
- $call.= "&Action=ListMatchingProducts";
- $call.= "&SellerId=".$this->keys['MERCHANT_ID'];
- $call.= "&SignatureVersion=2";
- $call.= "&SignatureMethod=HmacSHA256";
- $call.= "&MarketplaceId=".$this->keys['MARKETPLACE_ID'];
- $call.= "&Query=".$query;
- $call = $this->mwsRequestURL($call);
- $response = str_replace('ns2:', '', file_get_contents($call));
- $response = new SimpleXmlElement($response);
- if(isset($response->ListMatchingProductsResult->Products->Product[0])) {
- $best = $response->ListMatchingProductsResult->Products->Product[0];
- $attributes = array();
- foreach($best->AttributeSets->ItemAttributes as $atts) {
- //preg_match_all('/[A-Z][^A-Z]*/',$str,$results);
- $atts = get_object_vars($atts);
- foreach($atts as $key => $value) {
- preg_match_all('/[A-Z][^A-Z]*/',$key,$name);
- $name = implode(' ', $name[0]);
- $attributes[$key] = array('name'=>$name, 'value'=>$value);
- }
- }
- $title = $attributes['Title']['value'];
- unset($attributes['Title']);
- $asin = (string) $best->Identifiers->MarketplaceASIN->ASIN;
- $imgSize = max((string)$attributes['SmallImage']['value']->Height, (string)$attributes['SmallImage']['value']->Width);
- $imgURL = (string)$attributes['SmallImage']['value']->URL;
- $imgURL = str_replace('._SL'.$imgSize.'_', '', $imgURL);
- unset($attributes['SmallImage']);
- unset($attributes['Languages']);
- unset($attributes['ListPrice']);
- $best = array('name'=>$title,
- 'asin'=>$asin,
- 'img'=>$imgURL,
- 'attributes'=>$attributes);
- return $best;
- } else {
- return false;
- }
- }
- }
- ?>
Add Comment
Please, Sign In to add comment