Converting amazon MWS scratchpad queries to API calls Converting amazon MWS scratchpad queries to API calls php php

Converting amazon MWS scratchpad queries to API calls


I struggled with this for a long time as well, here is how I solved it for the Products API:

<?phprequire_once('.config.inc.php');$base_url = "https://mws.amazonservices.com/Products/2011-10-01";$method = "POST";$host = "mws.amazonservices.com";$uri = "/Products/2011-10-01";function amazon_xml($searchTerm) {    $params = array(        'AWSAccessKeyId' => AWS_ACCESS_KEY_ID,        'Action' => "ListMatchingProducts",        'SellerId' => MERCHANT_ID,        'SignatureMethod' => "HmacSHA256",        'SignatureVersion' => "2",        'Timestamp'=> gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time()),        'Version'=> "2011-10-01",        'MarketplaceId' => MARKETPLACE_ID,        'Query' => $searchTerm,        'QueryContextId' => "Books");    // Sort the URL parameters    $url_parts = array();    foreach(array_keys($params) as $key)        $url_parts[] = $key . "=" . str_replace('%7E', '~', rawurlencode($params[$key]));    sort($url_parts);    // Construct the string to sign    $url_string = implode("&", $url_parts);    $string_to_sign = "GET\nmws.amazonservices.com\n/Products/2011-10-01\n" . $url_string;    // Sign the request    $signature = hash_hmac("sha256", $string_to_sign, AWS_SECRET_ACCESS_KEY, TRUE);    // Base64 encode the signature and make it URL safe    $signature = urlencode(base64_encode($signature));    $url = "https://mws.amazonservices.com/Products/2011-10-01" . '?' . $url_string . "&Signature=" . $signature;    $ch = curl_init();    curl_setopt($ch, CURLOPT_URL,$url);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    curl_setopt($ch, CURLOPT_TIMEOUT, 15);    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);    $response = curl_exec($ch);    $parsed_xml = simplexml_load_string($response);    return ($parsed_xml);}?>

The .inc.config.php file contains my access key, secret key etc.

EDIT:
$searchterm is the isbn I am passing from my form.


You need to actually CALL the API. The string you are using doesn't specify the http:// portion of the URL.


I had a little trouble with this code, I had it working when uploaded to a hosted web server but not when running local using windows and xampp.

If you are having problems, try adding this to the end of the curl-setopt block.

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

btw you also need to modify your php.ini file in the xampp folder to enable curl.