Amazon.com MWS Integration

ddaugherty3 picture ddaugherty3 · Sep 8, 2011 · Viewed 16.5k times · Source

I am currently developing a very basic site which will, at this time, simply display order information from Amazon's Marketplace.

  • I have all of the MWS Security Credentials.
  • I have downloaded and reviewed, with much confusion, the PHP Client Library.
  • I am kind of new to PHP but I feel like I can handle this project.

I need to know how to install and access information from this API. I feel like I've tried everything. Amazon does not supply enough information to get this going. They make it sound like it takes 5 or 6 easy steps and you can access your information; this is not true.

Is there a detailed tutorial on MWS? I need as much information as possible. If you can help me out, maybe outline the steps required to get it going, that would be very appreciated!!!! I'm pulling my hair out over this. Thanks again

Answer

Josiah picture Josiah · Mar 10, 2014

A rough file to get you started. This is taken from several pages, including this one from @Vaidas. I don't have links yet, sorry. My only contribution is to put this together in one place.

None of the PHP code Amazon supplied worked for me out of the box. I'm assuming you have XAMPP with cURL or an equivalent environment. This code SHOULD work out of the box to get you started on what needs to happen. Just plug in your credentials.

<?php
$param = array();
$param['AWSAccessKeyId']   = 'YourAccessKeyID'; 
$param['Action']           = 'GetLowestOfferListingsForASIN'; 
$param['SellerId']         = 'YourSellerID'; 
$param['SignatureMethod']  = 'HmacSHA256';  
$param['SignatureVersion'] = '2'; 
$param['Timestamp']        = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
$param['Version']          = '2011-10-01'; 
$param['MarketplaceId']    = 'YourMarketplaceID'; 
$param['ItemCondition']    = 'new';
$param['ASINList.ASIN.1']  = 'B00C5XBAOA';
$secret = 'YourSecretKey';

$url = array();
foreach ($param as $key => $val) {

    $key = str_replace("%7E", "~", rawurlencode($key));
    $val = str_replace("%7E", "~", rawurlencode($val));
    $url[] = "{$key}={$val}";
}

sort($url);

$arr   = implode('&', $url);

$sign  = 'GET' . "\n";
$sign .= 'mws.amazonservices.com' . "\n";
$sign .= '/Products/2011-10-01' . "\n";
$sign .= $arr;

$signature = hash_hmac("sha256", $sign, $secret, true);
$signature = urlencode(base64_encode($signature));

$link  = "https://mws.amazonservices.com/Products/2011-10-01?";
$link .= $arr . "&Signature=" . $signature;
echo($link); //for debugging - you can paste this into a browser and see if it loads.

$ch = curl_init($link);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

echo('<p>' . $response . '</p>');
print_r('<p>' . $info . '</p>');
?>

Please note that it is VITAL to have the curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); line, at least in my case. CURL was working fine for any page except for the MWS page (it was just giving me a blank page with -1s in the info, and it took me most of a day to figure out I needed that line. It's in the MWS forums somewhere.

For good measure, here's a link to MWS ScratchPad.

Once I get a better handle on working with MWS maybe I'll do a tutorial. Or someone who is better at HTML and has a need for more of the features could do it.