user1388352
user1388352

Reputation: 151

Amazon PHP API MWS to get Fulfillment

I'm trying to get the inventory of my fulfillment using the PHP API of the MWS. When I use the parameters given by amazon on the scratchpad I get the answer perfectly, but when I try using my code it says that the signature doesn't match, what am I doing wrong?

<?php
$base_url = "https://mws.amazonservices.co.uk/FulfillmentInventory/2010-10-01";
$method = "POST";
$host = "mws.amazonservices.co.uk";
$uri = "/FulfillmentInventory/2010-10-01";

function amazon_xml() {

    $params = array(
        'AWSAccessKeyId' => "123456788",
        'Action' => "ListInventorySupply",
        'SellerId' => "234234123412341",
        'SignatureVersion' => "2",
        //'Timestamp'=> gmdate("Y-m-d\TH:i:s.Z", time()),
        'Timestamp'=> gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time()),
        'Version'=> "2010-10-01",
        'SignatureMethod' => "HmacSHA256",
        'SellerSkus.member.1' => '1EU-AMZ-FIRE-TV-MOUNT');

    // 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 = "POST\nmws.amazonservices.co.uk\n/FulfillmentInventory/2010-10-01\n" . $url_string;
    echo $string_to_sign."<br/>";

    // Sign the request
    $signature = hash_hmac("sha256", $string_to_sign, 'sdgsdfgsdfgsdgsdgsdg', TRUE);
    echo $signature."<br />";
    // Base64 encode the signature and make it URL safe
    $signature = urlencode(base64_encode($signature));
    echo $signature."<br />";

    $url = "https://mws.amazonservices.co.uk/FulfillmentInventory/2010-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);
    echo "La respuesta es ".$response;
    $parsed_xml = simplexml_load_string($response);

    //return ($parsed_xml);
    echo $parsed_xml;

}


amazon_xml();

?>

Upvotes: 0

Views: 348

Answers (1)

Hazzit
Hazzit

Reputation: 6882

You appear to be sorting your $url_parts array by value, where you should be sorting it by key.

ksort($url_parts);

I cannot see any other systematic problem in your code. Another point where my code differs from yours though, is that mine uses a Merchant parameter instead of SellerId - I have not checked if using SellerId actually works as well.

Upvotes: 1

Related Questions