亚马逊MWS PHP标记订单已发货 - 使用XML变量提交订单履行

时间:2014-09-09 01:02:26

标签: php amazon-mws

如何使用XML Feed标记亚马逊MWS中的订单 - 使用正确的端点,服务,版本和标题?

这是一个问题,我花了3天的时间来管理它,因为亚马逊api非常混乱,文档是中世纪的,并且提供任何特定信息的正确端点地址等大多数关键信息都是一个难以找到的东西。我只能通过在暂存器上模仿请求来创建它,随机测试各个部分,将这些示例组合在一起以完成不同的部分和操作。提交XML Feed是目前管理订单的唯一方式。

所以,这可能有助于某人。

1 个答案:

答案 0 :(得分:5)

我能够在下面进行简单的代码工作:

                        $param = array();
                        $param['AWSAccessKeyId']   = 'YOURKEY'; 
                        $param['Action']           = 'SubmitFeed'; 
                        $param['Merchant']         = 'YOURMERCHANTID'; 
                        $param['FeedType']         = '_POST_ORDER_FULFILLMENT_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'; 
                        $param['MarketplaceId.Id.1']    = 'MARKETPLACEID';
                        $param['PurgeAndReplace']    = 'false';

                        $secret = 'YOURSECRETKEY';

                        $url = array();
                        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"?>
                            <AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
                            <Header>
                            <DocumentVersion>1.01</DocumentVersion>
                            <MerchantIdentifier>YOURMERCHANTID</MerchantIdentifier>
                            </Header>
                            <MessageType>OrderFulfillment</MessageType>
                            <Message>
                            <MessageID>1</MessageID>
                            <OrderFulfillment>
                            <AmazonOrderID>YOUROWNORDERIDHERE</AmazonOrderID>
                            <FulfillmentDate>'.$param['Timestamp'].'</FulfillmentDate>
                            <FulfillmentData>
                            <CarrierName>TRACKINGCONAMEHERE</CarrierName>
                            <ShippingMethod>TRACKINGCONAMEORMETHODHERE</ShippingMethod>
                            <ShipperTrackingNumber>TRACKINGNOHERE</ShipperTrackingNumber>
                            </FulfillmentData>
                            </OrderFulfillment>
                            </Message>
                            </AmazonEnvelope>';


                        sort($url);

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

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

                        $signature = hash_hmac("sha256", $sign, $secret, true);
                        $httpHeader     =   array();
                        $httpHeader[]   =   'Transfer-Encoding: chunked';
                        $httpHeader[]   =   'Content-Type: application/xml';
                        $httpHeader[]   =   'Content-MD5: ' . base64_encode(md5($amazon_feed, true));
                        $httpHeader[]   =   'Expect:';
                        $httpHeader[]   =   'Accept:';              

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

                        $link  = "https://mws.amazonservices.com/Feeds/".$param['Version']."?";
                        $link .= $arr . "&Signature=" . $signature;
                        echo '<br>';
                        echo($link); //for debugging - you can paste this into a browser and see if it loads.
                        echo '<br>';
                        $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);
                        curl_close($ch);

给出以下输出:

<?xml version="1.0"?><SubmitFeedResponse xmlns="http://mws.amazonaws.com/doc/2009-01-01/">SubmitFeedResult>FeedSubmissionInfo>FeedSubmissionId>XXXXXXXXXXXXXXXXXXX</FeedSubmissionId><FeedType>_POST_ORDER_FULFILLMENT_DATA_</FeedType><SubmittedDate>2014-09-09T00:36:29+00:00</SubmittedDate><FeedProcessingStatus>_SUBMITTED_</FeedProcessingStatus></FeedSubmissionInfo>/SubmitFeedResult>ResponseMetadata>RequestId>XXXXXXXXXXXXXXXXXXX</RequestId></ResponseMetadata></SubmitFeedResponse><?xml version="1.0"?><SubmitFeedResponse xmlns="http://mws.amazonaws.com/doc/2009-01-01/"><SubmitFeedResult><FeedSubmissionInfo><FeedSubmissionId>XXXXXXXXXXXXXXXXXXX</FeedSubmissionId>FeedType>_POST_ORDER_FULFILLMENT_DATA_</FeedType><SubmittedDate>2014-09-09T00:36:29+00:00</SubmittedDate>FeedProcessingStatus>_SUBMITTED_</FeedProcessingStatus></FeedSubmissionInfo>/SubmitFeedResult>ResponseMetadata>RequestId>XXXXXXXXXXXXXXXXXXX</RequestId></ResponseMetadata></SubmitFeedResponse>