MWS在PHP中提供更新数量 - 亚马逊

时间:2016-12-15 10:11:25

标签: php amazon-web-services amazon-mws

有几天我试图通过php和亚马逊MWS在php上更新亚马逊的库存数量(不使用MWS API,因为在我看来已经过时了)

这是我的代码:

$param = array();
    $param['AWSAccessKeyId']     = $this->CHIAVE_ACCESSO;
    $param['Action']             = 'SubmitFeed';
    $param['Merchant']           = $this->SELLER_ID;
    $param['FeedType']       = '_POST_INVENTORY_AVAILABILITY_DATA_';
    $param['SignatureMethod']    = 'HmacSHA256';
    $param['SignatureVersion']   = '2';
    $param['Timestamp']          = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
    $param['Version']            = '2009-01-01';
    $params['MarketplaceId.Id.1'] = $this->MARCKETPLACE_IT;
    $params['MarketplaceId.Id.2'] = $this->MARCKETPLACE_UK;
    $params['MarketplaceId.Id.3'] = $this->MARCKETPLACE_ES;
    $params['MarketplaceId.Id.4'] = $this->MARCKETPLACE_DE;
    $params['MarketplaceId.Id.5'] = $this->MARCKETPLACE_FR;
    $param['PurgeAndReplace']    = 'false';


    foreach ($param as $key => $val) {

        $key = str_replace("%7E", "~", rawurlencode($key));
        $val = str_replace("%7E", "~", rawurlencode($val));
        $url[] = "{$key}={$val}";
    }

    $amazon_feed = '<?xml version="1.0" encoding="utf-8"?>
                    <AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
                    <Header>
                    <DocumentVersion>1.01</DocumentVersion>
                    <MerchantIdentifier>' .$this->SELLER_ID. '</MerchantIdentifier>
                    </Header>

                    <MessageType>Inventory</MessageType>
                    <Message>
                    <MessageID>1</MessageID>
                    <OperationType>Update</OperationType>
                    <Inventory>
                    <SKU>'.$sku.'</SKU>
                    <Quantity>'.$qty.'</Quantity>
                    </Inventory>
                    </Message>
                    </AmazonEnvelope>';

    sort($url);

    $arr   = implode('&', $url);

    $sign  = 'POST' . "\n";
    $sign .= 'mws.amazonservices.it' . "\n";
    $sign .= '/Feeds/'.$param['Version'].'' . "\n";
    $sign .= $arr;

    $signature      = hash_hmac("sha256", $sign, $this->secretKey, true);
    $httpHeader     =   array();
    $httpHeader[]   =   'Transfer-Encoding: chunked';
    $httpHeader[]   =   'Content-Type: application/xml';
    $httpHeader[]   =   'Content-MD5: ' . base64_encode(md5($amazon_feed, true));
    //$httpHeader[]   =   'x-amazon-user-agent: MyScriptName/1.0';
    $httpHeader[]   =   'Expect:';
    $httpHeader[]   =   'Accept:';

    $signature      = urlencode(base64_encode($signature));

    $link  = "https://mws.amazonservices.it/Feeds/".$param['Version']."?";
    $link .= $arr . "&Signature=" . $signature;

    echo $link;

    $ch = curl_init($link);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $amazon_feed);
    $response = curl_exec($ch);
    $info = curl_getinfo($ch);
    $errors=curl_error($ch);
    curl_close($ch);

    echo '<pre>';
    print_r($response); //xml response

回复是

50691017150_POST_INVENTORY_AVAILABILITY_DATA_2016-12-15T10:00:09 + 00:00_SUBMITTED_47843855-c5fb-4db9-bc3c-1ccd0aff4169

但是当我进入亚马逊库存时,我看不到任何变化。我也试过等了几天但没有任何变化。 我做错了什么?

提前感谢您的帮助!

使用MWS Scratchpad我遇到的错误如下

<?xml version="1.0" encoding="UTF-8"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
    <DocumentVersion>1.02</DocumentVersion>
    <MerchantIdentifier>A2PDC8GCZHAL2D</MerchantIdentifier>
</Header>
<MessageType>ProcessingReport</MessageType>
<Message>
    <MessageID>1</MessageID>
    <ProcessingReport>
        <DocumentTransactionID>50691017150</DocumentTransactionID>
        <StatusCode>Complete</StatusCode>
        <ProcessingSummary>
            <MessagesProcessed>1</MessagesProcessed>
            <MessagesSuccessful>0</MessagesSuccessful>
            <MessagesWithError>1</MessagesWithError>
            <MessagesWithWarning>0</MessagesWithWarning>
        </ProcessingSummary>
        <Result>
            <MessageID>1</MessageID>
            <ResultCode>Error</ResultCode>
            <ResultMessageCode>13013</ResultMessageCode>
            <ResultDescription>This SKU does not exist in the Amazon.com catalog. Your inventory data was not processed. For reasons why, and help fixing this, see http://sellercentral.amazon.it/gp/errorcode/13013</ResultDescription>
            <AdditionalInfo>
                <SKU>887235757035</SKU>
            </AdditionalInfo>
        </Result>
    </ProcessingReport>
</Message>

但是这个sku存在于我的目录

1 个答案:

答案 0 :(得分:4)

实际响应是来自MWS的XML,您可以看到 50691017150 实际上是您的 FeedSubmissionId

当您提交Feed时,MWS会返回FeedSubmissionId,您可以通过FeedSubmissionId跟踪Feed的内容。

如果您转到MWS Scratchpad,则可以获得Feed结果 在身份验证表单中输入所需的凭据,然后选择提要作为Sezione AP, GetFeedSubmissionResult 作为操作。

您将被要求提供您已有的FeedSubmissionId。 看看那个饲料发生了什么,为什么它会失败?

您正在通过市场ID,这意味着sku在那里可用。但在你的情况下,它们中的任何一个都可能无法使用。并且 MarketplaceId 是可选的,您可以删除

$params['MarketplaceId.Id.1'] = $this->MARCKETPLACE_IT;
$params['MarketplaceId.Id.2'] = $this->MARCKETPLACE_UK;
$params['MarketplaceId.Id.3'] = $this->MARCKETPLACE_ES;
$params['MarketplaceId.Id.4'] = $this->MARCKETPLACE_DE;
$params['MarketplaceId.Id.5'] = $this->MARCKETPLACE_FR;