user8457287 user8457287 - 30 days ago 6
PHP Question

How to get a separate XML for each ASIN ? Amazon-MWS

I have 1 request with 20 ASINs. I get the response in the form of XML with 20 ASINs. I want to get one XML - Respons for one ASIN and Save in txt-File (20 XML).
But I can do this only if in request 1 ASIN and not 20. But then the process is very slow. What should I do?
Question number 2.
How can I create 1 XML with all ASIN (eg 1000) ?

$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");


Here is an example of 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>

Answer Source

Once you have your XML document, you can use XPath to list each ASIN, this can then be output as XML and you can save each segment to an appropriate file...

$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 $dom->saveXML($asin).PHP_EOL;
}