I have been researching this for a while and can’t seem to find the problem with my code. I am attempting to get the price of an item and this works great if there is a price, but if the price is missing it throws an error.
Here is the code:
/* Amazon Offers ALGORITHM */
$parsed_xml = amazon_xml($isbn);
$current = $parsed_xml->ListMatchingProductsResult->Products->Product;
$asin = $current->Identifiers->MarketplaceASIN->ASIN;
// get information based on the items ASIN
$price_xml = amazonPrice_xml($asin);
if($price_xml) {
while(count($lowestPrices) < 2)
{
// check to see if there are values
if(xml_child_exists($parsed_xml, $current->AttributeSets->children('ns2', true)->ItemAttributes->ListPrice->Amount))
{
$listPrice = $current->AttributeSets->children('ns2', true)->ItemAttributes->ListPrice->Amount;
} else {
$listPrice = 0;
}
$currentPrice = $price_xml ->GetLowestOfferListingsForASINResult->Product->LowestOfferListings->LowestOfferListing;
print_r($listPrice);
My function to check for child nodes is:
function xml_child_exists($xml, $childpath)
{
$result = $parsed_xml->xpath($childpath);
if (count($result)) {
return true;
} else {
return false;
}
}
try this to check if child node exists