I am working with the Amazon API and have noticed that there is a problem when the item does not exist in the XML I am searching through. The XML that I get in response looks like this:
<GetMatchingProductForIdResult status="Success" IdType="UPC" Id="082686052344">
<Products>
<Product>
<Identifiers>
<MarketplaceASIN>
<MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
<ASIN>B000IDC1WO</ASIN>
</MarketplaceASIN>
</Identifiers>
<AttributeSets>
<ns2:ItemAttributes xml:lang="en-US" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
<ns2:Binding>Toy</ns2:Binding>
<ns2:Brand>Rubie's</ns2:Brand>
<ns2:Color>Black</ns2:Color>
<ns2:Department>unisex-child</ns2:Department>
<ns2:Feature>Rubie's Costume Children's Zorro Hat and Eye Mask Set</ns2:Feature>
<ns2:Feature>Child'S costume accessory</ns2:Feature>
<ns2:Feature>Black eye mask</ns2:Feature>
<ns2:ItemDimensions>
<ns2:Height Units="inches">8.00</ns2:Height>
<ns2:Length Units="inches">14.00</ns2:Length>
<ns2:Width Units="inches">8.00</ns2:Width>
</ns2:ItemDimensions>
<ns2:Label>Rubies - Domestic</ns2:Label>
<ns2:Languages>
<ns2:Language>
<ns2:Name>english</ns2:Name>
<ns2:Type>Unknown</ns2:Type>
</ns2:Language>
</ns2:Languages>
<ns2:ListPrice>
<ns2:Amount>16.99</ns2:Amount>
<ns2:CurrencyCode>USD</ns2:CurrencyCode>
</ns2:ListPrice>
<ns2:Manufacturer>Rubies - Domestic</ns2:Manufacturer>
<ns2:ManufacturerMaximumAge Units="months">180.0</ns2:ManufacturerMaximumAge>
<ns2:ManufacturerMinimumAge Units="months">48.0</ns2:ManufacturerMinimumAge>
<ns2:Model>F5234_NS</ns2:Model>
<ns2:PackageDimensions>
<ns2:Height Units="inches">0.90</ns2:Height>
<ns2:Length Units="inches">14.50</ns2:Length>
<ns2:Width Units="inches">13.20</ns2:Width>
<ns2:Weight Units="pounds">0.20</ns2:Weight>
</ns2:PackageDimensions>
<ns2:PackageQuantity>1</ns2:PackageQuantity>
<ns2:PartNumber>F5234_NS</ns2:PartNumber>
<ns2:ProductGroup>Toy</ns2:ProductGroup>
<ns2:ProductTypeName>TOYS_AND_GAMES</ns2:ProductTypeName>
<ns2:Publisher>Rubies - Domestic</ns2:Publisher>
<ns2:SmallImage>
<ns2:URL>http://ecx.images-amazon.com/images/I/51ZRwleH85L._SL75_.jpg</ns2:URL>
<ns2:Height Units="pixels">75</ns2:Height>
<ns2:Width Units="pixels">57</ns2:Width>
</ns2:SmallImage>
<ns2:Studio>Rubies - Domestic</ns2:Studio>
<ns2:Title>Rubie's Costume Children's Zorro Hat and Eye Mask Set</ns2:Title>
<ns2:Warranty>No Warranty</ns2:Warranty>
</ns2:ItemAttributes>
</AttributeSets>
<Relationships/>
<SalesRankings>
<SalesRank>
<ProductCategoryId>toy_display_on_website</ProductCategoryId>
<Rank>481818</Rank>
</SalesRank>
<SalesRank>
<ProductCategoryId>2229578011</ProductCategoryId>
<Rank>469</Rank>
</SalesRank>
</SalesRankings>
</Product>
</Products>
</GetMatchingProductForIdResult>
If there is a client error, then I receive this:
<GetMatchingProductForIdResult Id="082686035408" IdType="UPC" status="ClientError">
<Error>
<Type>Sender</Type>
<Code>InvalidParameterValue</Code>
<Message>Invalid UPC identifier 082686035408 for marketplace ATVPDKIKX0DER</Message>
</Error>
</GetMatchingProductForIdResult>
I am totally unaware of how I would deal with this since I am doing mass scans at a time. So far for PHP code, I have this:
$xmlFiles = glob("xml/*xml");
//Checking if multiple xml files exist.
if(is_array($xmlFiles)){
foreach($xmlFiles as $xmlFile){
$xml = simplexml_load_file($xmlFile);
//problem 'Status' is = 'Client Error'
foreach($xml->GetMatchingProductForIdResult as $items) {
print_r($items);
//Getting UPC
if(isset($items['Id'])){
$id = $items['Id'];
}else{
$id = 'No Id Found';
}
//Getting ASIN From XML
if(isset($items->Products->Product->Identifiers->MarketplaceASIN->ASIN)){
$asin = $items->Products->Product->Identifiers->MarketplaceASIN->ASIN;
}else{
$asin = 'No Asin Found';
}
if(isset($items->Products->Product->SalesRankings->SalesRank[0]->Rank)){
$salesRank = $items->Products->Product->SalesRankings->SalesRank[0]->Rank;
}else{
$salesRank = 'Sales Rank Not Found';
}
if(($items['Status']) != 'ClientError'){
if(isset($items->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->ListPrice->Amount)) {
$amount = $items->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->ListPrice->Amount;
}else{
$amount = '0.00';
}
if(isset($items->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->PackageDimensions->Height) !== False){
$height =$items->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->PackageDimensions->Height;
} else {
$height = '';
}
if(isset($items->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->PackageDimensions->Length) !== False){
$length = $items->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->PackageDimensions->Length;
} else {
$length = '';
}
if(isset($items->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->PackageDimensions->Width)){
$width = $items->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->PackageDimensions->Width;
} else {
$width = '';
}
if(($length+$width+$height) < 27.75){
$oversize = '';
} elseif (($length+$width+$height) > 27.75 && ($length+$width+$height) < 40) {
$oversize = 'Small Oversize';
} elseif(($length+$width+$height) > 40){
$oversize = 'Large oversize';
}
$sqlImport = "INSERT INTO " . $fileName . "(id, asin, salesRank, Amount, Oversize)
VALUES('$id', '$asin', '$salesRank', '$amount', '$oversize')";
if ($sqlConnection->query($sqlImport) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sqlImport . "<br>" . $sqlConnection->error;
}
} else {
die('Sorry for an unexpected error');
}
}
} } I am not sure how I would implement your answer into the following code. I am sorting through multiple XML files that have 5 of each tag in it.
I have this error when there is also a client error:
Fatal error: Call to a member function children() on null in J:\XAMPP\htdocs\Phillip\src\MarketplaceWebServiceProducts\Samples\csv_prep.php on line 110
Line 110 is:
if(isset($items->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->ListPrice->Amount) !== False) {
$amount = $items->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->ListPrice->Amount;
}else{
$amount = '0.00';
}
The current file that I am testing on does have 80 XML files that it goes through.If anyone has anyway to help me out, I would greatly appreciate it. Thanks in advance.