ylliX - Online Advertising Network Tutorial: Fetching stock data from Amazon using FBAInventoryServiceMWS - Yet Another Programmer's Blog

Tutorial: Fetching stock data from Amazon using FBAInventoryServiceMWS

First thing you need is PHP client for Fulfillment Inventory API, you can download at https://developer.amazonservices.ca/doc/fba/inventory/v20101001/php.html – yes, version is 4 years old, they don’t have any newer… Unpack zip at you library folder and you should see such files:

Zrzut ekranu 2014-10-18 o 12.24.22

Next step is preparing config, all you need is below:

define('AWS_ACCESS_KEY_ID', '........');
define('AWS_SECRET_ACCESS_KEY', '........');
define('APPLICATION_NAME', '......');
define('APPLICATION_VERSION', '1');
define('MERCHANT_ID', '......');
define('MARKETPLACE_ID', '......');

Just fill in proper values. Then setup autoloader for your client (if you have it already done for other libraries, just skip this step):

set_include_path(get_include_path() . PATH_SEPARATOR . '../../'); // this should point at your library folder wherever it is
spl_autoload_register(function ($className) {
	$filePath = str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
	$includePaths = explode(PATH_SEPARATOR, get_include_path());
	foreach ($includePaths as $includePath) {
		if (file_exists($includePath . DIRECTORY_SEPARATOR . $filePath)) {
			require_once $filePath;
			return;
		}
	}
});

Then invoke client itself:

$serviceUrl = "https://mws-eu.amazonservices.com/FulfillmentInventory/2010-10-01";
$configData = array(
'ServiceURL' => $serviceUrl,
'ProxyHost' => null,
'ProxyPort' => -1,
'MaxErrorRetry' => 3,
);

$service = new FBAInventoryServiceMWS_Client(
AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, $configData, APPLICATION_NAME, APPLICATION_VERSION);

Variable $serviceUrl depends on your region:

North America – https://mws.amazonservices.com/FulfillmentInventory/2010-10-01
Europe – https://mws-eu.amazonservices.com/FulfillmentInventory/2010-10-01
Japan – https://mws.amazonservices.jp/FulfillmentInventory/2010-10-01
China – https://mws.amazonservices.com.cn/FulfillmentInventory/2010-10-01

Next step is preparing request itself, you need to use product SKU:

$request = new FBAInventoryServiceMWS_Model_ListInventorySupplyRequest();
$request->setSellerId(MERCHANT_ID);
$skus = new FBAInventoryServiceMWS_Model_SellerSkuList();
$skus->setmember(array('SKU1', 'SKU2', 'SKU3'));
$request->setSellerSkus($skus);

And the final step is calling webservice:

try {
   $response = $service->ListInventorySupply($request);
} catch (FBAInventoryServiceMWS_Exception $ex) {
   $code = $ex->getStatusCode();
   
   $simplexml = new SimpleXMLElement($ex->getXML());
   $message = (string) $simplexml->Error->Message;
}

The strange thing is that even with proper response (without any errors, Amazon throws exception, maybe there is some config entry for this, have no idea. Anyway in $code variable you should have HTTP reponse code, if this is 200 then everything went ok, returned XML should look like:

<ListInventorySupplyResponse xmlns="http://mws.amazonaws.com/FulfillmentInventory/2010-10-01/">
  <ListInventorySupplyResult>
    <InventorySupplyList>
      <member>
        <SellerSKU>SKU1</SellerSKU>
        <TotalSupplyQuantity>0</TotalSupplyQuantity>
        <InStockSupplyQuantity>0</InStockSupplyQuantity>
      </member>
      <member>
        <SellerSKU>SKU2</SellerSKU>
        <ASIN>XXXXXX</ASIN>
        <TotalSupplyQuantity>73</TotalSupplyQuantity>
        <FNSKU>YYYYYY</FNSKU>
        <Condition>NewItem</Condition>
        <InStockSupplyQuantity>73</InStockSupplyQuantity>
        <SupplyDetail/>
        <EarliestAvailability>
          <TimepointType>Immediately</TimepointType>
        </EarliestAvailability>
      </member>
    </InventorySupplyList>
  </ListInventorySupplyResult>
  <ResponseMetadata>
    <RequestId>b9fb5095-d94d-4bb0-bbd7-70881d486bbb</RequestId>
  </ResponseMetadata>
</ListInventorySupplyResponse>

Now all you need is to update your DB with proper values.

Leave a Reply