[英]How to get a separate XML for each ASIN ? Amazon-MWS
我有20個ASIN的1個請求。 我得到帶有20個ASIN的XML形式的響應。 我想獲取一種XML-響應一種ASIN並保存為txt文件(20種XML)。 但是,只有在請求1 ASIN中而不是20中時,我才能執行此操作。但是此過程非常緩慢。 我該怎么辦? 問題編號2。如何使用所有ASIN(例如1000)創建1個XML?
$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)的示例
<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>
擁有XML文檔后,您可以使用XPath列出每個ASIN,然后可以將其作為XML輸出,並且可以將每個段保存到適當的文件中...
$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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.