首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为每个ASIN获取单独的XML?Amazon-MWS

如何为每个ASIN获取单独的XML?Amazon-MWS
EN

Stack Overflow用户
提问于 2017-08-19 17:18:27
回答 1查看 231关注 0票数 0

我有一个带有20个ASIN的请求。我得到了具有20个ASIN的XML形式的响应。我想得到一个XML响应一个ASIN和保存在txt文件(20个XML)。但我只能在请求1ASIN而不是20的情况下这样做,但这样做的过程非常慢。我该怎么办?问题2.如何创建1个全为ASIN (例如1000)的XML?

代码语言:javascript
复制
    $arr = file('asin.txt',FILE_IGNORE_NEW_LINES);
$arr_chunks = array_chunk($arr, 20);

$request->setMarketplaceId(MARKETPLACE_ID);
$asin_list = new MarketplaceWebServiceProducts_Model_ASINListType();

//$request->setASINList($asin_list);
$request->setItemCondition('used');

foreach ($arr_chunks as $value){
    $value = $value;
    $asin_list->setASIN($value);
$request->setASINList($asin_list);
 sleep(1);
 invokeGetLowestOfferListingsForASIN($service, $request);
}

  function invokeGetLowestOfferListingsForASIN(MarketplaceWebServiceProducts_Interface $service, $request)
  {
      try {
        $response = $service->GetLowestOfferListingsForASIN();


 file_put_contents('asin2.xml', $response->toXML());
        echo ("Service Response\n");
        echo ("=============================================================================\n");

        $dom = new DOMDocument();
        $dom->loadXML($response->toXML());
        $dom->preserveWhiteSpace = FALSE;
        $dom->formatOutput = true;
        echo $dom->saveXML();
       // echo("ResponseHeaderMetadata: " . $response->getResponseHeaderMetadata() . "\n");

下面是一个XML示例(asin2.xml)

代码语言:javascript
复制
<GetLowestOfferListingsForASINResponse
    xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
    <GetLowestOfferListingsForASINResult
        ASIN='3944660110' status='Success'>
        <AllOfferListingsConsidered>true</AllOfferListingsConsidered>
        <Product>
            <Identifiers>
                <MarketplaceASIN>
                    <MarketplaceId>A1PA6795UKMFR9</MarketplaceId>
                    <ASIN>3944660110</ASIN>
                </MarketplaceASIN>
            </Identifiers>
            <LowestOfferListings></LowestOfferListings>
        </Product>
    </GetLowestOfferListingsForASINResult>
    <GetLowestOfferListingsForASINResult
        ASIN='3000383964' status='Success'>
        <AllOfferListingsConsidered>true</AllOfferListingsConsidered>
        <Product>
            <Identifiers>
                <MarketplaceASIN>
                    <MarketplaceId>A1PA6795UKMFR9</MarketplaceId>
                    <ASIN>3000383964</ASIN>
                </MarketplaceASIN>
            </Identifiers>
            <LowestOfferListings>
                <LowestOfferListing>
                    <Qualifiers>
                        <ItemCondition>Used</ItemCondition>
                        <ItemSubcondition>Good</ItemSubcondition>
                        <FulfillmentChannel>Merchant</FulfillmentChannel>
                        <ShipsDomestically>True</ShipsDomestically>
                        <ShippingTime>
                            <Max>0-2 days</Max>
                        </ShippingTime>
                        <SellerPositiveFeedbackRating>98-100%
                        </SellerPositiveFeedbackRating>
                    </Qualifiers>
                    <NumberOfOfferListingsConsidered>1
                    </NumberOfOfferListingsConsidered>
                    <SellerFeedbackCount>1388</SellerFeedbackCount>
                    <Price>
                        <LandedPrice>
                            <CurrencyCode>EUR</CurrencyCode>
                            <Amount>31.80</Amount>
                        </LandedPrice>
                        <ListingPrice>
                            <CurrencyCode>EUR</CurrencyCode>
                            <Amount>28.80</Amount>
                        </ListingPrice>
                        <Shipping>
                            <CurrencyCode>EUR</CurrencyCode>
                            <Amount>3.00</Amount>
                        </Shipping>
                    </Price>
                    <MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
                </LowestOfferListing>
            </LowestOfferListings>
        </Product>
    </GetLowestOfferListingsForASINResult>
    <GetLowestOfferListingsForASINResult
        ASIN='3000556575' status='Success'>
        <AllOfferListingsConsidered>true</AllOfferListingsConsidered>
        <Product>
            <Identifiers>
                <MarketplaceASIN>
                    <MarketplaceId>A1PA6795UKMFR9</MarketplaceId>
                    <ASIN>3000556575</ASIN>
                </MarketplaceASIN>
            </Identifiers>
            <LowestOfferListings></LowestOfferListings>
        </Product>
    </GetLowestOfferListingsForASINResult>
    <ResponseMetadata>
        <RequestId>3caac4dc-ca52-446f-9f4b-a8da2d685ce7</RequestId>
    </ResponseMetadata>
</GetLowestOfferListingsForASINResponse>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-20 02:09:32

一旦有了XML文档,就可以使用XPath列出每个ASIN,然后可以输出为XML,并且可以将每个段保存到适当的文件中……

代码语言:javascript
复制
   $dom = new DOMDocument ();
   $dom->load ( 't1.xml' );
   $xp = new DOMXPath($dom);
   $xp->registerNamespace("default", 
                "http://mws.amazonservices.com/schema/Products/2011-10-01");
    foreach ( $xp->query("//default:GetLowestOfferListingsForASINResult") as $asin ) {
        echo "For ASIN:".$asin->getAttribute('ASIN').PHP_EOL;
        echo "ASIN=".$xp->evaluate ("string(descendant::default:MarketplaceASIN/default:ASIN/text())", $asin) .PHP_EOL;

        echo $dom->saveXML($asin).PHP_EOL;
        $prices = $xp->query("descendant::default:Price", $asin );
        foreach ( $prices as $price )   {
            echo "LandedPrice=".$xp->evaluate("string(descendant::default:LandedPrice/default:Amount/text())",$price).PHP_EOL;
            echo "ListingPrice=".$xp->evaluate("string(descendant::default:ListingPrice/default:Amount/text())",$price).PHP_EOL;
            echo "Shipping=".$xp->evaluate("string(descendant::default:Shipping/default:Amount/text())",$price).PHP_EOL;

        }

    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45769819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档