I am working with the amazon API and I need to check the status of the XML. For example:
<GetMatchingProductForIdResult status="Success" IdType="UPC" Id="082686068055">
or
<GetMatchingProductForIdResult status="ClientError" IdType="UPC" Id="082686068055">
How would I go about writing a code that checks if the status is not "Success"? The XML looks like this:
<GetMatchingProductForIdResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetMatchingProductForIdResult status="Success" IdType="UPC" Id="082686068055">
<Products>
<Product>
<Identifiers>
...
</Identifiers>
<AttributeSets>
</AttributeSets>
</Product>
</Products>
</GetMatchingProductForIdResult>
Error:
<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>
PHP Code to retrieve the content:
if(isset($items->Products->Product->AttributeSets->children($namespace['ns2'])->ItemAttributes->ListPrice->Amount)) {
$amount = $items->Products->Product->AttributeSets->children($namespace['ns2'])->ItemAttributes->ListPrice->Amount;
}else{
$amount = '0.00';
}
I was able to create this code to get the ID of the product:
//$xml is an open XML file.
$items=$xml->GetMatchingProductForIdResult;
if(isset($items['Id'])){
$id = $items['Id'];
}else{
$id = 'No Id Found';
}
The first tag stays throughout the whole XML file. The tag closes at the end of the file. I am using SimpleXML to open, and get all of the other data needed from the file, but I always have an error when the tags in <AttributeSets>
is not valid. I need to find a way to avoid this problem. Thanks in advance.