Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AmazonAPI{
- private static $instance;
- private $public_key;
- private $secret_key;
- private $associate_tag;//This is only for Amazon affiliates
- private $wsdl_url='http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl';
- private $webservices_url='https://webservices.amazon.com/onca/soap?Service=AWSECommerceService';
- public function __construct($publicKey, $secretKey, $associateTag){
- if(!empty($publicKey)){
- $this->public_key = $publicKey;
- }
- if(!empty($secretKey)){
- $this->secret_key = $secretKey;
- }
- if(!empty($associateTag)){
- $this->associate_tag = $associateTag;
- }
- }
- //Send a request to amazon
- public function sendRequest($request_params){
- $params['Request']=$request_params;
- $operation=$request_params['Operation'];
- if(isset($this->associate_tag)){
- $params['AssociateTag']=$this->associate_tag;
- }
- $soapy=new SoapClient(
- $this->wsdl_url,
- array('exceptions'=>1)
- );
- $soapy->__setLocation($this->webservices_url);
- $current_timestamp=gmdate("Y-m-dTH:i:sZ");
- $req_sig=$this->createSignature($operation,$current_timestamp);
- $headers_array=array(
- new SoapHeader(
- 'http://security.amazonaws.com/doc/2007-01-01/',
- 'AWSAccessKeyId',
- $this->public_key
- ),
- new SoapHeader(
- 'http://security.amazonaws.com/doc/2007-01-01/',
- 'Timestamp',
- $current_timestamp
- ),
- new SoapHeader(
- 'http://security.amazonaws.com/doc/2007-01-01/',
- 'Signature',
- $req_sig
- )
- );
- $soapy->__setSoapHeaders($headers_array);
- return $soapy->__soapCall($operation,array($params));
- }
- //Create signature for request
- protected function createSignature($operation,$timestamp){
- $the_string=$operation.$timestamp;
- return base64_encode(hash_hmac("sha256",$the_string,$this->secret_key,true));
- }
- }
- <?php
- require('../AmazonApi.php');
- //Create API access object
- $public_key = 'PUBLIC-KEY';
- $secret_key = 'SECRET-KEY';
- $associate_tag = 'TAG';
- $amazon_api = new AmazonAPI($public_key, $secret_key, $associate_tag);
- $collection = Mage::getModel('catalog/product')
- ->getCollection()
- ->addStoreFilter(Mage_Core_Model_App::ADMIN_STORE_ID) // default store
- ->addAttributeToSelect('sku');
- $asin = substr($sku,0,10);
- //Array of request parameters
- $params_array = array(
- 'Operation' => 'ItemLookup',
- 'IdType' => 'ASIN',
- 'ItemId' => $asin ,
- 'ResponseGroup' => 'Tracks'
- );
- // returns a list of items for the search query 'Slow Magic'
- $response = $amazon_api->sendRequest($params_array);
- echo '<pre>';
- print_r($response);
- echo '</pre>';
Add Comment
Please, Sign In to add comment