Skip to content

Instantly share code, notes, and snippets.

@underdown
Last active December 2, 2022 12:58
Show Gist options
  • Select an option

  • Save underdown/5edcdf247dea0e6a6dc1 to your computer and use it in GitHub Desktop.

Select an option

Save underdown/5edcdf247dea0e6a6dc1 to your computer and use it in GitHub Desktop.
Amazon MWS Orders API PHP Example
<?php
/*******************************************************************************
* Copyright 2009-2015 Amazon Services. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
*
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at: http://aws.amazon.com/apache2.0
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*******************************************************************************
* PHP Version 5
* @category Amazon
* @package Marketplace Web Service Orders
* @version 2013-09-01
* Library Version: 2015-04-30
* Generated: Thu May 28 17:42:26 GMT 2015
*/
/**
* List Orders Sample
*/
require_once('.config.inc.php');
/************************************************************************
* Instantiate Implementation of MarketplaceWebServiceOrders
*
* AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY constants
* are defined in the .config.inc.php located in the same
* directory as this sample
***********************************************************************/
// More endpoints are listed in the MWS Developer Guide
// North America:
$serviceUrl = "https://mws.amazonservices.com/Orders/2013-09-01";
// Europe
//$serviceUrl = "https://mws-eu.amazonservices.com/Orders/2013-09-01";
// Japan
//$serviceUrl = "https://mws.amazonservices.jp/Orders/2013-09-01";
// China
//$serviceUrl = "https://mws.amazonservices.com.cn/Orders/2013-09-01";
date_default_timezone_set('America/Phoenix');
$config = array (
'ServiceURL' => $serviceUrl,
'ProxyHost' => null,
'ProxyPort' => -1,
'ProxyUsername' => null,
'ProxyPassword' => null,
'MaxErrorRetry' => 3,
);
$service = new MarketplaceWebServiceOrders_Client(
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY,
APPLICATION_NAME,
APPLICATION_VERSION,
$config);
// Get total from 31 days ago for 30 days
$t1 = date("c", time()-31*24*60*60);
$t2 = date("c", time()-1*24*60*60);
/************************************************************************
* Setup request parameters and uncomment invoke to try out
* sample for List Orders Action
***********************************************************************/
// @TODO: set request. Action can be passed as MarketplaceWebServiceOrders_Model_ListOrders
$request = new MarketplaceWebServiceOrders_Model_ListOrdersRequest();
$request->setSellerId(MERCHANT_ID);
$request->setCreatedAfter($t1);
$request->setCreatedBefore($t2);
$request->setMarketplaceId("ATVPDKIKX0DER");
//I want all orders in this case
//$request->setFulfillmentChannel("MFN HTTP/1.1");
// object or array of parameters
invokeListOrders($service, $request);
/**
* Get List Orders Action Sample
* Gets competitive pricing and related information for a product identified by
* the MarketplaceId and ASIN.
*
* @param MarketplaceWebServiceOrders_Interface $service instance of MarketplaceWebServiceOrders_Interface
* @param mixed $request MarketplaceWebServiceOrders_Model_ListOrders or array of parameters
*/
function invokeListOrders(MarketplaceWebServiceOrders_Interface $service, $request)
{
try {
$response = $service->ListOrders($request);
$dom = new DOMDocument();
$dom->loadXML($response->toXML());
$dom->preserveWhiteSpace = true;
$dom->formatOutput = true;
$xml = $dom->saveXML();
$orderdata = new SimpleXMLElement($xml);
$array=json_encode($orderdata, TRUE);
// print_r($array);
$result = json_decode($array);
if ($result) {
(float)$total=0;
foreach ($result->ListOrdersResult->Orders->Order as $order) {
// echo '-----------------------'.PHP_EOL;
// echo 'phone: '.$order->ShippingAddress->Phone.PHP_EOL;
// echo 'amount: '.$order->OrderTotal->Amount.' '.$order->OrderTotal->CurrencyCode.PHP_EOL;
$runningtotal = (float)$order->OrderTotal->Amount;
$total=$runningtotal+$total;
//echo "running count: ".$total.PHP_EOL;
}
} else {
echo 'error decoding json';
}
echo "{ \"Total Orders\": \"$total\"}";
} catch (MarketplaceWebServiceOrders_Exception $ex) {
echo("Caught Exception: " . $ex->getMessage() . "\n");
echo("Response Status Code: " . $ex->getStatusCode() . "\n");
echo("Error Code: " . $ex->getErrorCode() . "\n");
echo("Error Type: " . $ex->getErrorType() . "\n");
echo("Request ID: " . $ex->getRequestId() . "\n");
echo("XML: " . $ex->getXML() . "\n");
echo("ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n");
}
}
@underdown

Copy link
Copy Markdown
Author

this gist will get the last 30 days of sales data from your MWS account (starting from 31 days ago) in a simple json format. I use it to plug the numbers into my geckoboard

This doesnt work in all cases and doesnt account for throttling, tokens, associative arrays, and probably much more.

@taslimwaris

Copy link
Copy Markdown

This code is not giving result... i m facing problem.
Service Response
String 1969-07-21T02:56:03Z 1969-07-21T02:56:03Z String String 1969-07-21T02:56:03Z 1969-07-21T02:56:03Z String String String String String String String String String String String String String String String String String String 1 1 String String String String String String String String true String String String 1969-07-21T02:56:03Z 1969-07-21T02:56:03Z 1969-07-21T02:56:03Z 1969-07-21T02:56:03Z true String true true String
ResponseHeaderMetadata:

@nhathuytran

Copy link
Copy Markdown

Hello
I have an error: "Caught Exception: Access to Orders.ListOrders is denied"
I don't understand this probplem.
Can you help me?
Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment