Guest User

Stack Overflow question

a guest
Sep 28th, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. <?php
  2. ini_set('display_errors',1);
  3.  
  4. class MWSProduct{
  5.  
  6. private $keys = array();
  7.  
  8. private $serviceURL = "https://mws-eu.amazonservices.com/Products/2011-10-01";
  9.  
  10. public function __construct($key, $secret, $name, $version, $merchant, $marketplace) {
  11. $this->keys = array('ACCESS_KEY'=>$key,
  12. 'SECRET_KEY'=>$secret,
  13. 'APP_NAME'=>$name,
  14. 'APP_VERSION'=>$version,
  15. 'MERCHANT_ID'=>$merchant,
  16. 'MARKETPLACE_ID'=>$marketplace);
  17. }
  18.  
  19. private function mwsRequestURL($request, $version = '2011-10-01') {
  20. // breaks the url appart into its components (query, host etc)
  21. $uri_elements = parse_url($request);
  22. // pulls the querystring out from the array of url elements
  23. $request = $uri_elements['query'];
  24. // parses the string as if it were a querystring and passes it into an array
  25. parse_str($request, $parameters);
  26. // Add the new required paramters
  27. $parameters['Timestamp'] = gmdate("Y-m-d\TH:i:s\Z");
  28. $parameters['Version'] = $version;
  29. //adds the access key if it hsa been sent
  30. $parameters['AWSAccessKeyId'] = $this->keys['ACCESS_KEY'];
  31. // The new authentication requirements need the keys to be sorted (by byte value)
  32. ksort($parameters);
  33. // loops through the array, and breaks apart the parameter and key at the same time
  34. foreach ($parameters as $parameter => $value) {
  35. // We need to be sure we properly encode the value of our parameter
  36. $parameter = str_replace("%7E", "~", rawurlencode(str_replace('_', '.', $parameter)));
  37. $value = str_replace("%7E", "~", rawurlencode($value));
  38. $request_array[] = $parameter . '=' . $value;
  39. }
  40. // Put our & symbol at the beginning of each of our request variables and put it in a string
  41. $new_request = implode('&', $request_array);
  42. // Create our signature string
  43. $signature_string = "GET\n{$uri_elements['host']}\n{$uri_elements['path']}\n{$new_request}";
  44. // Create our signature using hash_hmac
  45. $signature = urlencode(base64_encode(hash_hmac('sha256', $signature_string, $this->keys['SECRET_KEY'], true)));
  46. // Return our new request
  47. return "https://{$uri_elements['host']}{$uri_elements['path']}?{$new_request}&Signature={$signature}";
  48. }
  49.  
  50.  
  51.  
  52. public function cheapestPricing($asins) {
  53. $call = $this->serviceURL;
  54. $call.= "?AWSAccessKeyId=".$this->keys['ACCESS_KEY'];
  55. $call.= "&Action=GetLowestOfferListingsForASIN";
  56. $call.= "&SellerId=".$this->keys['MERCHANT_ID'];
  57. $call.= "&SignatureVersion=2";
  58. $call.= "&SignatureMethod=HmacSHA256";
  59. $call.= "&MarketplaceId=".$this->keys['MARKETPLACE_ID'];
  60. $call.= "&ExcludeMe=true";
  61.  
  62. $count = 0;
  63. foreach($asins as $asin) {
  64. $count++;
  65. $call.= "&ASINList.ASIN.{$count}={$asin}";
  66. }
  67.  
  68. $call = $this->mwsRequestURL($call);
  69. $response = file_get_contents($call);
  70. $response = simplexml_load_string($response);
  71.  
  72. if(isset($response->GetLowestOfferListingsForASINResult->Error)) {
  73. return false;
  74. } else {
  75.  
  76. $prices = array();
  77.  
  78. foreach($response->GetLowestOfferListingsForASINResult as $product) {
  79. $asin = (string) $product->Product->Identifiers->MarketplaceASIN->ASIN;
  80. $prices[$asin] = array('new'=>array(),'used'=>array());
  81. foreach($product->Product->LowestOfferListings->LowestOfferListing as $price) {
  82. $cond = (string) strtolower($price->Qualifiers->ItemCondition);
  83. $sale = (float) $price->Price->ListingPrice->Amount;
  84. $ship = (float) $price->Price->Shipping->Amount;
  85. $landed = number_format($sale + $ship, 2, '.', '');
  86. $prices[$asin][$cond][] = array('landed'=>$landed, 'price'=>$sale, 'shipping'=>$ship);
  87. }
  88. }
  89.  
  90. return $prices;
  91. }
  92. }
  93.  
  94. public function getProduct($query) {
  95. $call = $this->serviceURL;
  96. $call.= "?AWSAccessKeyId=".$this->keys['ACCESS_KEY'];
  97. $call.= "&Action=ListMatchingProducts";
  98. $call.= "&SellerId=".$this->keys['MERCHANT_ID'];
  99. $call.= "&SignatureVersion=2";
  100. $call.= "&SignatureMethod=HmacSHA256";
  101. $call.= "&MarketplaceId=".$this->keys['MARKETPLACE_ID'];
  102. $call.= "&Query=".$query;
  103.  
  104. $call = $this->mwsRequestURL($call);
  105. $response = str_replace('ns2:', '', file_get_contents($call));
  106. $response = new SimpleXmlElement($response);
  107.  
  108. if(isset($response->ListMatchingProductsResult->Products->Product[0])) {
  109. $best = $response->ListMatchingProductsResult->Products->Product[0];
  110.  
  111. $attributes = array();
  112. foreach($best->AttributeSets->ItemAttributes as $atts) {
  113. //preg_match_all('/[A-Z][^A-Z]*/',$str,$results);
  114. $atts = get_object_vars($atts);
  115. foreach($atts as $key => $value) {
  116. preg_match_all('/[A-Z][^A-Z]*/',$key,$name);
  117. $name = implode(' ', $name[0]);
  118. $attributes[$key] = array('name'=>$name, 'value'=>$value);
  119. }
  120. }
  121.  
  122. $title = $attributes['Title']['value'];
  123. unset($attributes['Title']);
  124.  
  125. $asin = (string) $best->Identifiers->MarketplaceASIN->ASIN;
  126.  
  127. $imgSize = max((string)$attributes['SmallImage']['value']->Height, (string)$attributes['SmallImage']['value']->Width);
  128. $imgURL = (string)$attributes['SmallImage']['value']->URL;
  129. $imgURL = str_replace('._SL'.$imgSize.'_', '', $imgURL);
  130. unset($attributes['SmallImage']);
  131.  
  132. unset($attributes['Languages']);
  133. unset($attributes['ListPrice']);
  134.  
  135. $best = array('name'=>$title,
  136. 'asin'=>$asin,
  137. 'img'=>$imgURL,
  138. 'attributes'=>$attributes);
  139.  
  140.  
  141. return $best;
  142. } else {
  143. return false;
  144. }
  145. }
  146.  
  147.  
  148. }
  149.  
  150. ?>
Add Comment
Please, Sign In to add comment