• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Amazon MWS Class

iBrightDev

Jay Street
NLC
NLC
so, i know amazon has an API that you are able to utilize, but, i found it pretty confusing and more than what i need for a current project, so, i started building a much smaller PHP object. feel free to use it. I will be changing it and adding on more features, but, you can at least get the idea. :)

PHP:
<?php

header("Content-Type:text/xml");

if(!extension_loaded('curl')) {
    die("this script requires the curl extension<br />see: <a href=\"http://www.php.net/manual/en/ref.curl.php\">http://www.php.net/manual/en/ref.curl.php</a>");
}

class amazon {
    
    // ENTER YOUR ACCOUNT SETTINGS
    private $awsKey     =   "---YOUR AWS ACCESS KEY ID---";
    private $sellerId   =   "---YOUR MERCHANT ID---";
    private $marketID   =   "---YOUR MARKETPLACE ID---";
    private $secretKey  =   "---YOUR SECRET KEY---";
    
    // DONT TOUCH
    private $method     =   "GET";
    private $host       =   "mws.amazonservices.com";
    private $uri        =   "/Orders/2011-01-01";
    private $action     =   "ListOrders";
    private $sigVersion =   "2";
    private $version    =   "2011-01-01";
    private $sigMethod  =   "HmacSHA256";
    private $timestamp;
    private $createdAf;
    private $request;
    
    public function __construct($startDate=null) {
        
        if (!empty($startDate)) {
            $this->createdAf    =   "&CreatedAfter={$startDate}";
        } else {
            $this->createdAf    =   '';
        }
        
        $this->request      =   "AWSAccessKeyId={$this->awsKey}"
                            .   "&Action={$this->action}"
                            .   "&SellerId={$this->sellerId}"
                            .   "&SignatureVersion={$this->sigVersion}"
                            .   "&Timestamp=" . gmdate("Y-m-d\TH:i:s\Z")
                            .   "&Version={$this->version}"
                            .   "&SignatureMethod={$this->sigMethod}"
                            .   $this->createdAf
                            .   "&MarketplaceId.Id.1={$this->marketID}";
        
    }
    
    public function listOrders() {
        
        // Clean up and sort
        $this->request = explode('&',$this->request);
        
        foreach ($this->request as $key => $value) {
            $t = explode("=",$value);
            $params[$t[0]] = $t[1];
        }
        unset($this->request);
        
        ksort($params);
        
        foreach ($params as $param=>$value) {
            $param = str_replace("%7E", "~", rawurlencode($param));
            $value = str_replace("%7E", "~", rawurlencode($value));
            $canonicalized_query[] = $param."=".$value;
        }
        
        $canonicalized_query = implode("&", $canonicalized_query);
        
        // create the string to sign
        $string_to_sign = $this->method."\n".$this->host."\n".$this->uri."\n".$canonicalized_query;
        
        // calculate HMAC with SHA256 and base64-encoding
        $signature = base64_encode(hash_hmac("sha256", $string_to_sign, $this->secretKey, True));
        
        // encode the signature for the request
        $signature = str_replace("%7E", "~", rawurlencode($signature));
        
        // create request
        $url = "https://".$this->host.$this->uri."?".$canonicalized_query."&Signature=".$signature;
        
        $ch   = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        
        $data = curl_exec($ch);
        
        curl_close($ch);
        
        return $data;
        
    }
    
}

$startDate  =   gmdate("Y-m-d\TH:i:s\Z",time()-86400*30);

$az = new amazon($startDate);

echo $az->listOrders();

?>
 
Back
Top