亚马逊MWS GetLowestOfferListingsForASIN和SimpleXML不工作

时间:2015-05-29 17:45:14

标签: php simplexml amazon-mws

以下是我尝试使用的一些示例数据:

<?xml version="1.0"?>
<GetLowestOfferListingsForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetLowestOfferListingsForASINResult ASIN="B006DTXDMO" status="Success">
  <AllOfferListingsConsidered>true</AllOfferListingsConsidered>
  <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
<Identifiers>
  <MarketplaceASIN>
    <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
    <ASIN>B006DTXDMO</ASIN>
  </MarketplaceASIN>
</Identifiers>
<LowestOfferListings>
  <LowestOfferListing>
    <Qualifiers>
      <ItemCondition>New</ItemCondition>
      <ItemSubcondition>New</ItemSubcondition>
      <FulfillmentChannel>Amazon</FulfillmentChannel>
      <ShipsDomestically>True</ShipsDomestically>
      <ShippingTime>
        <Max>0-2 days</Max>
      </ShippingTime>
      <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
    </Qualifiers>
    <NumberOfOfferListingsConsidered>2</NumberOfOfferListingsConsidered>
    <SellerFeedbackCount>26486</SellerFeedbackCount>
    <Price>
      <LandedPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>24.99</Amount>
      </LandedPrice>
      <ListingPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>24.99</Amount>
      </ListingPrice>
      <Shipping>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>0.00</Amount>
      </Shipping>
    </Price>
    <MultipleOffersAtLowestPrice>True</MultipleOffersAtLowestPrice>
  </LowestOfferListing>
  <LowestOfferListing>
    <Qualifiers>
      <ItemCondition>New</ItemCondition>
      <ItemSubcondition>New</ItemSubcondition>
      <FulfillmentChannel>Merchant</FulfillmentChannel>
      <ShipsDomestically>True</ShipsDomestically>
      <ShippingTime>
        <Max>0-2 days</Max>
      </ShippingTime>
      <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
    </Qualifiers>
    <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
    <SellerFeedbackCount>26486</SellerFeedbackCount>
    <Price>
      <LandedPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>29.99</Amount>
      </LandedPrice>
      <ListingPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>29.99</Amount>
      </ListingPrice>
      <Shipping>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>0.00</Amount>
      </Shipping>
    </Price>
    <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
  </LowestOfferListing>
  <LowestOfferListing>
    <Qualifiers>
      <ItemCondition>New</ItemCondition>
      <ItemSubcondition>New</ItemSubcondition>
      <FulfillmentChannel>Merchant</FulfillmentChannel>
      <ShipsDomestically>Unknown</ShipsDomestically>
      <ShippingTime>
        <Max>0-2 days</Max>
      </ShippingTime>
      <SellerPositiveFeedbackRating>98-100%</SellerPositiveFeedbackRating>
    </Qualifiers>
    <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
    <SellerFeedbackCount>10398</SellerFeedbackCount>
    <Price>
      <LandedPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>198.99</Amount>
      </LandedPrice>
      <ListingPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>198.99</Amount>
      </ListingPrice>
      <Shipping>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>0.00</Amount>
      </Shipping>
    </Price>
    <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
  </LowestOfferListing>
  <LowestOfferListing>
    <Qualifiers>
      <ItemCondition>New</ItemCondition>
      <ItemSubcondition>New</ItemSubcondition>
      <FulfillmentChannel>Merchant</FulfillmentChannel>
      <ShipsDomestically>True</ShipsDomestically>
      <ShippingTime>
        <Max>3-7 days</Max>
      </ShippingTime>
      <SellerPositiveFeedbackRating>98-100%</SellerPositiveFeedbackRating>
    </Qualifiers>
    <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
    <SellerFeedbackCount>1</SellerFeedbackCount>
    <Price>
      <LandedPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>589.49</Amount>
      </LandedPrice>
      <ListingPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>589.49</Amount>
      </ListingPrice>
      <Shipping>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>0.00</Amount>
      </Shipping>
    </Price>
    <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
  </LowestOfferListing>
</LowestOfferListings>
  </Product>
</GetLowestOfferListingsForASINResult>
<GetLowestOfferListingsForASINResult ASIN="B00DL6IT5S" status="Success">
  <AllOfferListingsConsidered>false</AllOfferListingsConsidered>
  <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
<Identifiers>
  <MarketplaceASIN>
    <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
    <ASIN>B00DL6IT5S</ASIN>
  </MarketplaceASIN>
</Identifiers>
<LowestOfferListings/>
 </Product>
</GetLowestOfferListingsForASINResult>
<GetLowestOfferListingsForASINResult ASIN="B00DKKKP3Y" status="Success">
  <AllOfferListingsConsidered>true</AllOfferListingsConsidered>
 <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
<Identifiers>
  <MarketplaceASIN>
    <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
    <ASIN>B00DKKKP3Y</ASIN>
  </MarketplaceASIN>
</Identifiers>
<LowestOfferListings>
  <LowestOfferListing>
    <Qualifiers>
      <ItemCondition>New</ItemCondition>
      <ItemSubcondition>New</ItemSubcondition>
      <FulfillmentChannel>Amazon</FulfillmentChannel>
      <ShipsDomestically>True</ShipsDomestically>
      <ShippingTime>
        <Max>0-2 days</Max>
      </ShippingTime>
      <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
    </Qualifiers>
    <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
    <SellerFeedbackCount>4324</SellerFeedbackCount>
    <Price>
      <LandedPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>51.95</Amount>
      </LandedPrice>
      <ListingPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>51.95</Amount>
      </ListingPrice>
      <Shipping>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>0.00</Amount>
      </Shipping>
    </Price>
    <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
  </LowestOfferListing>
</LowestOfferListings>
  </Product>
</GetLowestOfferListingsForASINResult>
<GetLowestOfferListingsForASINResult ASIN="B00XAP7WL2" status="Success">
  <AllOfferListingsConsidered>false</AllOfferListingsConsidered>
 <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
<Identifiers>
  <MarketplaceASIN>
    <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
    <ASIN>B00XAP7WL2</ASIN>
  </MarketplaceASIN>
</Identifiers>
<LowestOfferListings/>
  </Product>
</GetLowestOfferListingsForASINResult>
<GetLowestOfferListingsForASINResult ASIN="B00WAF736M" status="Success">
  <AllOfferListingsConsidered>true</AllOfferListingsConsidered>
  <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
<Identifiers>
  <MarketplaceASIN>
    <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
    <ASIN>B00WAF736M</ASIN>
  </MarketplaceASIN>
</Identifiers>
<LowestOfferListings>
  <LowestOfferListing>
    <Qualifiers>
      <ItemCondition>New</ItemCondition>
      <ItemSubcondition>New</ItemSubcondition>
      <FulfillmentChannel>Merchant</FulfillmentChannel>
      <ShipsDomestically>True</ShipsDomestically>
      <ShippingTime>
        <Max>0-2 days</Max>
      </ShippingTime>
      <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
    </Qualifiers>
    <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
    <SellerFeedbackCount>14457</SellerFeedbackCount>
    <Price>
      <LandedPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>39.99</Amount>
      </LandedPrice>
      <ListingPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>35.51</Amount>
      </ListingPrice>
      <Shipping>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>4.48</Amount>
      </Shipping>
    </Price>
    <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
  </LowestOfferListing>
</LowestOfferListings>
  </Product>
</GetLowestOfferListingsForASINResult>
<GetLowestOfferListingsForASINResult ASIN="B0013UOSHC" status="Success">
  <AllOfferListingsConsidered>true</AllOfferListingsConsidered>
  <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
<Identifiers>
  <MarketplaceASIN>
    <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
    <ASIN>B0013UOSHC</ASIN>
  </MarketplaceASIN>
</Identifiers>
<LowestOfferListings/>
 </Product>
</GetLowestOfferListingsForASINResult>
<GetLowestOfferListingsForASINResult ASIN="B00SZS9W4O" status="Success">
  <AllOfferListingsConsidered>true</AllOfferListingsConsidered>
  <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
<Identifiers>
  <MarketplaceASIN>
    <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
    <ASIN>B00SZS9W4O</ASIN>
  </MarketplaceASIN>
</Identifiers>
<LowestOfferListings>
  <LowestOfferListing>
    <Qualifiers>
      <ItemCondition>New</ItemCondition>
      <ItemSubcondition>New</ItemSubcondition>
      <FulfillmentChannel>Amazon</FulfillmentChannel>
      <ShipsDomestically>True</ShipsDomestically>
      <ShippingTime>
        <Max>0-2 days</Max>
      </ShippingTime>
      <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
    </Qualifiers>
    <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
    <SellerFeedbackCount>2748</SellerFeedbackCount>
    <Price>
      <LandedPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>19.99</Amount>
      </LandedPrice>
      <ListingPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>19.99</Amount>
      </ListingPrice>
      <Shipping>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>0.00</Amount>
      </Shipping>
    </Price>
    <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
  </LowestOfferListing>
  <LowestOfferListing>
    <Qualifiers>
      <ItemCondition>New</ItemCondition>
      <ItemSubcondition>New</ItemSubcondition>
      <FulfillmentChannel>Merchant</FulfillmentChannel>
      <ShipsDomestically>True</ShipsDomestically>
      <ShippingTime>
        <Max>0-2 days</Max>
      </ShippingTime>
      <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
    </Qualifiers>
    <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
    <SellerFeedbackCount>2748</SellerFeedbackCount>
    <Price>
      <LandedPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>31.61</Amount>
      </LandedPrice>
      <ListingPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>25.99</Amount>
      </ListingPrice>
      <Shipping>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>5.62</Amount>
      </Shipping>
    </Price>
    <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
  </LowestOfferListing>
</LowestOfferListings>
  </Product>
</GetLowestOfferListingsForASINResult>
   <GetLowestOfferListingsForASINResult ASIN="B00T0N0NNW" status="Success">
  <AllOfferListingsConsidered>true</AllOfferListingsConsidered>
  <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
<Identifiers>
  <MarketplaceASIN>
    <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
    <ASIN>B00T0N0NNW</ASIN>
  </MarketplaceASIN>
</Identifiers>
<LowestOfferListings>
  <LowestOfferListing>
    <Qualifiers>
      <ItemCondition>New</ItemCondition>
      <ItemSubcondition>New</ItemSubcondition>
      <FulfillmentChannel>Amazon</FulfillmentChannel>
      <ShipsDomestically>True</ShipsDomestically>
      <ShippingTime>
        <Max>0-2 days</Max>
      </ShippingTime>
      <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
    </Qualifiers>
    <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
    <SellerFeedbackCount>2748</SellerFeedbackCount>
    <Price>
      <LandedPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>19.99</Amount>
      </LandedPrice>
      <ListingPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>19.99</Amount>
      </ListingPrice>
      <Shipping>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>0.00</Amount>
      </Shipping>
    </Price>
    <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
  </LowestOfferListing>
</LowestOfferListings>
 </Product>
</GetLowestOfferListingsForASINResult>
<GetLowestOfferListingsForASINResult ASIN="B00F01V9RC" status="Success">
 <AllOfferListingsConsidered>true</AllOfferListingsConsidered>
  <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
<Identifiers>
  <MarketplaceASIN>
    <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
    <ASIN>B00F01V9RC</ASIN>
  </MarketplaceASIN>
</Identifiers>
<LowestOfferListings>
  <LowestOfferListing>
    <Qualifiers>
      <ItemCondition>New</ItemCondition>
      <ItemSubcondition>New</ItemSubcondition>
      <FulfillmentChannel>Merchant</FulfillmentChannel>
      <ShipsDomestically>True</ShipsDomestically>
      <ShippingTime>
        <Max>0-2 days</Max>
      </ShippingTime>
      <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
    </Qualifiers>
    <NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
    <SellerFeedbackCount>2191</SellerFeedbackCount>
    <Price>
      <LandedPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>19.50</Amount>
      </LandedPrice>
      <ListingPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>14.00</Amount>
      </ListingPrice>
      <Shipping>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>5.50</Amount>
      </Shipping>
    </Price>
    <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
  </LowestOfferListing>
   </LowestOfferListings>
 </Product>
</GetLowestOfferListingsForASINResult>
<ResponseMetadata>
  <RequestId>b5cb6d0f-14da-404b-af3d-f187877ea58e</RequestId>
</ResponseMetadata>
</GetLowestOfferListingsForASINResponse>';

这是我的代码:

            $param = array();
            $param['AWSAccessKeyId']    = 'ACCESS_KEY'; 
            $param['Action']            = 'GetLowestOfferListingsForASIN';
            $param['SellerId']          = 'SELLER_ID'; 
            $param['SignatureMethod']   = 'HmacSHA256';  
            $param['SignatureVersion']  = '2'; 
            $param['Timestamp']         = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
            $param['Version']           = '2011-10-01'; 
            $param['MarketplaceId']     = 'ATVPDKIKX0DER';
            $param['ItemCondition']     = 'new';

            //get all variations (limit 9)
            $inc=1;
            foreach($variations as $key=>$asin){
                if($inc <= 9){$param['ASINList.ASIN.' . $inc] = $asin;}
                $inc++;
            }

            $secret = 'SECRET';

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

            sort($url);

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

            $sign  = 'GET' . "\n";
            $sign .= 'mws.amazonservices.com' . "\n";
            $sign .= '/Products/2011-10-01' . "\n";
            $sign .= $arr;

            $signature = hash_hmac("sha256", $sign, $secret, true);
            $signature = urlencode(base64_encode($signature));

            $link  = "https://mws.amazonservices.com/Products/2011-10-01?";
            $link .= $arr . "&Signature=" . $signature;

            $ch = curl_init($link);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
            $response = curl_exec($ch);
            $info = curl_getinfo($ch);
            curl_close($ch);

$response = preg_replace('/&(?!#?[a-z0-9]+;)/', '&amp;', $response);
$price_data = simplexml_load_string($response);

$node = $price_data->GetLowestOfferListingsForASINResult->Product;
foreach($node as $product){
    $asin = $product->Identifiers->MarketplaceASIN->ASIN;

    $node = $product->LowestOfferListings->LowestOfferListing->Price->LandedPrice;
    foreach($node as $price){
        $amzn_price = $price->Amount;
    }

    echo "$asin $amzn_price <br />";
}

我成功地为ListMatchingProducts使用了一个非常相似的代码。我试图循环结果以匹配最低价格到他们的ASIN,但这只给我第一个结果而不是所有结果。我尝试过使用儿童和我能想到的其他大部分事情,但仍然没有运气。

1 个答案:

答案 0 :(得分:0)

如果有帮助,请查看下面的代码:

$response = $service->getLowestOfferListingsForASIN($request);

$dom = new DOMDocument();
$dom->loadXML($response->toXML());
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$xml_data = $dom->saveXML();
$dom->loadXML($xml_data);

$otherOfferXml = simplexml_load_string($xml_data);

foreach($otherOfferXml as $offers)
{
    // Skipping last RequestID section
    if(!isset($offers["status"]))
        continue;

    // Checking if the API returned any error then continue to next SKU
    if($offers["status"] != "Success")
        continue;

    $asin = (String) $offers->Product->Identifiers->MarketplaceASIN->ASIN;

    // Going through all ASIN's offers to get price
    $seller_counter = 0;
    $others_response_data[$asin] = "";
    foreach($offers->Product->LowestOfferListings->LowestOfferListing as $offers_list)
    {
        $others_response_data[$asin][$seller_counter]["LandedPrice"] = (String) $offers_list->Price->LandedPrice->Amount;
        $others_response_data[$asin][$seller_counter]["ListingPrice"] = (String) $offers_list->Price->ListingPrice->Amount;
        $others_response_data[$asin][$seller_counter]["Shipping"] = (String) $offers_list->Price->Shipping->Amount;
        $others_response_data[$asin][$seller_counter]["Fulfillment"] = $fulfillment_channel;
        $others_response_data[$asin][$seller_counter]["SKU"] = $asin_array[$asin]["sku"];
        $others_response_data[$asin][$seller_counter]["AZN_ASIN"] = $asin;
        $seller_counter++;  
    }
}