how to get list of all products amazon MWS

can you explain how to get all product list, while i submit requestReport (_GET_MERCHANT_LISTINGS_ALL_DATA_)

I got the following request:

<?xml version="1.0"?>
<RequestReportResponse xmlns="http://mws.amazonaws.com/doc/2009-01-01/">
  <RequestReportResult>
    <ReportRequestInfo>
      <ReportType>_GET_MERCHANT_LISTINGS_ALL_DATA_</ReportType>
      <ReportProcessingStatus>_SUBMITTED_</ReportProcessingStatus>
      <EndDate>2016-11-02T12:12:30+00:00</EndDate>
      <Scheduled>false</Scheduled>
      <ReportRequestId>50148017107</ReportRequestId>
      <SubmittedDate>2016-11-02T12:12:30+00:00</SubmittedDate>
      <StartDate>2016-11-02T12:12:30+00:00</StartDate>
    </ReportRequestInfo>
  </RequestReportResult>
  <ResponseMetadata>
    <RequestId>05d33eb0-dbaf-42d0-88c1-794605d55980</RequestId>
  </ResponseMetadata>
</RequestReportResponse>

Then how can i get the list of all products?

Upvotes: 2

Views: 9196

Answers (2)

cottton
cottton

Reputation: 1607

Example script: get your own products from amazon

Composer require: cpigroup/php-amazon-mws

This script does 3 steps:

  1. request a report (returns ReportRequestId)
  2. list the report and get the report id (returns ReportId) (calling this until we find our requested report)
  3. load the report (and save ofc)

Its not pretty, but does the job. And you can change it the way you need it.

/**
 * How to at stack:
 * https://stackoverflow.com/questions/40379883/how-to-get-list-of-all-products-amazon-mws
 *
 * Docu at amazon:
 * https://docs.developer.amazonservices.com/en_US/reports/Reports_ReportType.html#ReportTypeCategories__ListingsReports
 *
 * We need to call the api (min) 3 times:
 * 1. request a report                           (returns ReportRequestId)
 * 2. list the report and get the report id      (returns ReportId)
 *    (calling this until we find our requested report)
 * 3. load the report                            (returns report data)
 */


// If got id already, then set here:
$reportRequestId = null;
$reportId = null;

// Set the report type to request.
#$reportType = '_GET_MERCHANT_LISTINGS_DATA_LITE_';
$reportType = '_GET_MERCHANT_LISTINGS_DATA_';

// This method sets the start and end times for the report request.
// If this parameter is set, the report will only contain data that was updated
// between the two times given.
// If these parameters are not set, the report will only contain the most recent data.
$reportTimeFrom = null;#'2015-01-01'; // null or string
$reportTimeTo = null;#'2021-08-28'; // null or string

// This method sets the list of marketplace IDs to be sent in the next request.
// If this parameter is set, the report will only contain data relevant to the marketplaces listed.
$marketPlaces = null; // null, string or array of strings
// $marketPlaces = [
//     // 'A1PA6795UKMFR9', // amazon_de
//     // 'A13V1IB3VIYZZH', // amazon_fr
//     // 'A1F83G8C2ARO7P', // amazon_uk
//     // 'A1RKKUPIHCS9HS', // amazon_es
//     // 'APJ6JRA9NG5V4', // amazon_it
// ];

// For w/e reason we must provide a sales channel.
// But we get all (or the $marketPlaces restricted) products anyway.
// And - tested - amazon_de or _fr ... does not change the response.
// So this does not matter. Just leave it amazon_de.
$salesChannel = 'amazon_de';

// Set file name where to save the report.
$filenamePrefix = '';
if (is_string($marketPlaces)) {
    $filenamePrefix = "{$marketPlaces}_";
} elseif (is_array($marketPlaces)) {
    $filenamePrefix = implode('_', $marketPlaces) . '_';
}
$destinationFile = __DIR__ . "/{$filenamePrefix}products.csv";

// Set the path to the config file.
$configPath = 'config/amazon/amazon_config.php';

// =====================================================================
// 1. Report Request.
// =====================================================================
if ($reportRequestId === null and $reportId === null) {
    echo "Got no report request id and no report id - do a new report request ....\r\n";
    $api = new \AmazonReportRequest(
        $salesChannel,
        false, // mock param
        null, // mock param
        $configPath
    );
    $api->setThrottleStop();
    $api->setReportType($reportType);
    if ($reportTimeFrom !== null and $reportTimeTo !== null) {
        $api->setTimeLimits($reportTimeFrom, $reportTimeTo);
    }
    $api->setShowSalesChannel(true);
    if ($marketPlaces !== null) {
        $api->setMarketplaces($marketPlaces);
    }
    $api->requestReport(); // Actual api call.
    $response = $api->getResponse();
    // Example response:
    // [
    //     'ReportRequestId'        => '111111111111',
    //     'ReportType'             => '_GET_MERCHANT_LISTINGS_DATA_LITE_',
    //     'StartDate'              => '2021-08-27T17:17:52+00:00',
    //     'EndDate'                => '2021-08-27T17:17:52+00:00',
    //     'Scheduled'              => 'false',
    //     'SubmittedDate'          => '2021-08-27T17:17:52+00:00',
    //     'ReportProcessingStatus' => '_SUBMITTED_',
    // ];
    if (!isset($response['ReportRequestId'])) {
        echo "Missing report request response[ReportRequestId].\r\n";
        exit(1);
    }
    $reportRequestId = $response['ReportRequestId'];
    echo var_export($response, true) . PHP_EOL; // debug
    echo "ReportRequestId: '{$reportRequestId}' --OK.\r\n";
    echo "\r\n";
}

// =====================================================================
// 2. Report List.
// =====================================================================
if ($reportRequestId !== null and $reportId === null) {
    echo "Got a report request id '{$reportRequestId}' (no report id) - list the report request ...\r\n";
    $api = new \AmazonReportList(
        $salesChannel,
        false, // mock param
        null, // mock param
        $configPath
    );
    $api->setThrottleStop();
    // Search for the exact report we requested above (by report request id and type).
    $api->setRequestIds($reportRequestId);
    $api->setReportTypes($reportType);
    do {
        $api->fetchReportList(); // Actual api call.
        $list = $api->getList();
        // Example list:
        // [
        //     [
        //         'ReportId'        => '22222222222222222',
        //         'ReportType'      => '_GET_MERCHANT_LISTINGS_DATA_LITE_',
        //         'ReportRequestId' => '111111111111',
        //         'AvailableDate'   => '2021-08-27T17:18:11+00:00',
        //         'Acknowledged'    => 'false',
        //     ],
        // ];
        // Search our report request in the list.
        foreach ($list as $row) {
            if (!isset($row['ReportRequestId'])) {
                echo "Missing report list row[ReportRequestId].\r\n";
                exit(1);
            }
            if ((string)$row['ReportRequestId']
                === (string)$reportRequestId
            ) {
                // Found our report request.
                // Get the report id.
                if (!isset($row['ReportId'])) {
                    echo "Missing report list row[ReportId].\r\n";
                    exit(1);
                }
                $reportId = $row['ReportId'];
                echo var_export($row, true) . PHP_EOL; // debug
                break 2; // Break out of foreach AND do loop.
            } else {
                echo "Different report request '{$row['ReportRequestId']}' --SKIP.\r\n";
            }
        }
        if ($reportId === null) {
            $waitSec = 4;
            echo "Could not find report with request id '{$reportRequestId}'. --RETRY in {$waitSec} sec.\r\n";
            // See https://docs.developer.amazonservices.com/en_US/reports/Reports_RequestReport.html
            //      Throttling
            //      Maximum request quota    Restore rate                Hourly request quota
            //      15 requests              One request every minute    60 requests per hour
            for ($i = 0; $i < $waitSec; $i++) {
                echo ".";
                sleep(1);
            }
            echo "\r\n";
        }
    } while ($reportId === null);
    echo "ReportId: '{$reportId}' --OK.\r\n";
    echo "\r\n";
}

// =====================================================================
// 3. Load Report by id.
// =====================================================================
if ($reportId !== null) {
    echo "Load Report by id '{$reportId}' ...\r\n";
    $api = new \AmazonReport(
        $salesChannel,
        null, // report id (optional)
        false, // mock param
        null, // mock param
        $configPath
    );
    $api->setThrottleStop();
    $api->setReportId($reportId);
    $api->fetchReport(); // Actual api call.
    $r = file_put_contents($destinationFile, $api->getRawReport());
    if ($r === false) {
        echo "Cannot save report to file '{$destinationFile}'.\r\n";
    } else {
        echo "Report saved to file '{$destinationFile}'.\r\n";
    }
}
echo "Finished.\r\n";

Example output:

// Got no report request id and no report id - do a new report request ....
// array (
//     'ReportRequestId'        => '111111111111',
//     'ReportType'             => '_GET_MERCHANT_LISTINGS_DATA_',
//     'StartDate'              => '2014-12-31T23:58:00+00:00',
//     'EndDate'                => '2021-08-26T23:58:00+00:00',
//     'Scheduled'              => 'false',
//     'SubmittedDate'          => '2021-08-27T18:25:53+00:00',
//     'ReportProcessingStatus' => '_SUBMITTED_',
// )
// ReportRequestId: '111111111111' --OK.
//
// Got a report request id '111111111111' (no report id) - list the report request ...
// Could not find report with request id '111111111111'. --RETRY in 15 sec.
// ...............
// array (
// 'ReportId'           => '22222222222222222',
// 'ReportType'         => '_GET_MERCHANT_LISTINGS_DATA_',
// 'ReportRequestId'    => '111111111111',
// 'AvailableDate'      => '2021-08-27T18:26:07+00:00',
// 'Acknowledged'       => 'false',
// )
// ReportId: '22222222222222222' --OK.
//
// Load Report by id '22222222222222222' ...
// Report saved to file 'tmp/_test.log'.
// Finished.

Have fun =)

Upvotes: 0

ScottG
ScottG

Reputation: 11121

It's all spelled out right here: http://docs.developer.amazonservices.com/en_US/reports/index.html

Basically, you submit a request with a RequestReport operation, check the status of the request with GetReportRequestList. When complete, you'll get a GeneratedReportId, and you'll use that to call GetReportList and/or GetReport with the report id to get the report data.

Since you've already submitted a report request, use the ReportRequestId that you have received and call GetReportRequestList. http://docs.developer.amazonservices.com/en_US/reports/Reports_GetReportRequestList.html

That will tell you the status and let you know when it's done and well as giving you a GeneratedReportId

Since you're using PHP, download the SDK for PHP and most of the work is already done for you. https://developer.amazonservices.com/doc/bde/reports/v20090101/php.html/154-1105707-5344447

Upvotes: 4

Related Questions