如何获取亚马逊 MWS 的所有产品列表

Posted

技术标签:

【中文标题】如何获取亚马逊 MWS 的所有产品列表【英文标题】:how to get list of all products amazon MWS 【发布时间】:2017-03-15 18:16:51 【问题描述】:

在我提交 requestReport (_GET_MERCHANT_LISTINGS_ALL_DATA_) 时,你能解释一下如何获取所有产品列表吗

我收到以下请求:

<?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>

那我怎样才能得到所有产品的列表呢?

【问题讨论】:

【参考方案1】:

全部都在这里说明:http://docs.developer.amazonservices.com/en_US/reports/index.html

基本上,您使用RequestReport 操作提交请求,使用GetReportRequestList 检查请求的状态。完成后,您将获得一个 GeneratedReportId,然后您将使用它调用带有报告 ID 的 GetReportList 和/或 GetReport 以获取报告数据。

由于您已经提交了报告请求,请使用您收到的ReportRequestId 并致电GetReportRequestList。 http://docs.developer.amazonservices.com/en_US/reports/Reports_GetReportRequestList.html

这会告诉你状态,让你知道什么时候完成,并给你一个GeneratedReportId

由于您使用的是 php,请下载适用于 PHP 的 SDK,大部分工作已经为您完成。 https://developer.amazonservices.com/doc/bde/reports/v20090101/php.html/154-1105707-5344447

【讨论】:

他们没有比这更困难的了!? 我已经完成了所有这些,但我想将这些数据保存在 XML 中...我使用的代码是..$dom = new DOMDocument(); $dom->loadXML($response->toXML()); $dom->preserveWhiteSpace = false; $dom->格式输出=真; $dom->save('reportsample.xml');.....它给了我错误.. 灾难。一个简单的列表就是这么多废话。【参考方案2】:

示例脚本:从亚马逊获取您自己的产品

作曲家要求:cpigroup/php-amazon-mws

此脚本执行 3 个步骤:

    请求报告 (返回 ReportRequestId) 列出报告并获取报告ID (返回 ReportId) (调用这个直到我们找到我们请求的报告) 加载报告 (并保存 ofc)

它不漂亮,但可以。 您可以根据需要更改它。

/**
 * How to at stack:
 * https://***.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__ . "/$filenamePrefixproducts.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";

示例输出:

// 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.

玩得开心 =)

【讨论】:

以上是关于如何获取亚马逊 MWS 的所有产品列表的主要内容,如果未能解决你的问题,请参考以下文章

如何获取任何特定亚马逊商家/卖家的所有产品?

如何通过“亚马逊MWS订单API”获取“非亚马逊”销售渠道订单

亚马逊 MWS:如何指定 _GET_MERCHANT_LISTINGS_DATA_ 的语言

如何获取用户 Seller ID、Marketplace ID 和 MWS token?

使用 PHP 从亚马逊 MWS API 获取订单数据

亚马逊 MWS - 内嵌图片