Guest User

Untitled

a guest
Feb 8th, 2016
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. class AmazonAPI{
  2. private static $instance;
  3.  
  4. private $public_key;
  5. private $secret_key;
  6. private $associate_tag;//This is only for Amazon affiliates
  7.  
  8. private $wsdl_url='http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl';
  9. private $webservices_url='https://webservices.amazon.com/onca/soap?Service=AWSECommerceService';
  10.  
  11. public function __construct($publicKey, $secretKey, $associateTag){
  12. if(!empty($publicKey)){
  13. $this->public_key = $publicKey;
  14. }
  15.  
  16. if(!empty($secretKey)){
  17. $this->secret_key = $secretKey;
  18. }
  19.  
  20. if(!empty($associateTag)){
  21. $this->associate_tag = $associateTag;
  22. }
  23. }
  24.  
  25. //Send a request to amazon
  26. public function sendRequest($request_params){
  27. $params['Request']=$request_params;
  28. $operation=$request_params['Operation'];
  29.  
  30. if(isset($this->associate_tag)){
  31. $params['AssociateTag']=$this->associate_tag;
  32. }
  33.  
  34. $soapy=new SoapClient(
  35. $this->wsdl_url,
  36. array('exceptions'=>1)
  37. );
  38.  
  39. $soapy->__setLocation($this->webservices_url);
  40.  
  41. $current_timestamp=gmdate("Y-m-dTH:i:sZ");
  42. $req_sig=$this->createSignature($operation,$current_timestamp);
  43.  
  44. $headers_array=array(
  45. new SoapHeader(
  46. 'http://security.amazonaws.com/doc/2007-01-01/',
  47. 'AWSAccessKeyId',
  48. $this->public_key
  49. ),
  50. new SoapHeader(
  51. 'http://security.amazonaws.com/doc/2007-01-01/',
  52. 'Timestamp',
  53. $current_timestamp
  54. ),
  55. new SoapHeader(
  56. 'http://security.amazonaws.com/doc/2007-01-01/',
  57. 'Signature',
  58. $req_sig
  59. )
  60. );
  61. $soapy->__setSoapHeaders($headers_array);
  62.  
  63. return $soapy->__soapCall($operation,array($params));
  64. }
  65.  
  66.  
  67. //Create signature for request
  68. protected function createSignature($operation,$timestamp){
  69. $the_string=$operation.$timestamp;
  70. return base64_encode(hash_hmac("sha256",$the_string,$this->secret_key,true));
  71. }
  72. }
  73.  
  74. <?php
  75. require('../AmazonApi.php');
  76.  
  77. //Create API access object
  78. $public_key = 'PUBLIC-KEY';
  79. $secret_key = 'SECRET-KEY';
  80. $associate_tag = 'TAG';
  81. $amazon_api = new AmazonAPI($public_key, $secret_key, $associate_tag);
  82.  
  83.  
  84.  
  85. $collection = Mage::getModel('catalog/product')
  86. ->getCollection()
  87. ->addStoreFilter(Mage_Core_Model_App::ADMIN_STORE_ID) // default store
  88. ->addAttributeToSelect('sku');
  89. $asin = substr($sku,0,10);
  90.  
  91.  
  92.  
  93. //Array of request parameters
  94. $params_array = array(
  95. 'Operation' => 'ItemLookup',
  96. 'IdType' => 'ASIN',
  97. 'ItemId' => $asin ,
  98. 'ResponseGroup' => 'Tracks'
  99. );
  100.  
  101. // returns a list of items for the search query 'Slow Magic'
  102. $response = $amazon_api->sendRequest($params_array);
  103.  
  104. echo '<pre>';
  105. print_r($response);
  106. echo '</pre>';
Add Comment
Please, Sign In to add comment