使用php解析XML文档时的奇怪结果

时间:2013-01-07 14:13:47

标签: php xml amazon

我正在尝试使用PHP来解析XML文档。它基本上工作,除了我得到一个非常奇怪的错误,我没有看到原因。第一个ASIN的XML是:

    <?xml version="1.0"?>
<GetLowestOfferListingsForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetLowestOfferListingsForASINResult ASIN="1580230032" status="Success">
<AllOfferListingsConsidered>true</AllOfferListingsConsidered>
<Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
<Identifiers>
  <MarketplaceASIN>
    <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
    <ASIN>1580230032</ASIN>
  </MarketplaceASIN>
</Identifiers>
<LowestOfferListings>
  <LowestOfferListing>
    <Qualifiers>
      <ItemCondition>New</ItemCondition>
      <ItemSubcondition>New</ItemSubcondition>
      <FulfillmentChannel>Merchant</FulfillmentChannel>
      <ShipsDomestically>True</ShipsDomestically>
      <ShippingTime>
        <Max>0-2 days</Max>
      </ShippingTime>
      <SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
    </Qualifiers>
    <NumberOfOfferListingsConsidered>2</NumberOfOfferListingsConsidered>
    <SellerFeedbackCount>221</SellerFeedbackCount>
    <Price>
      <LandedPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>10.12</Amount>
      </LandedPrice>
      <ListingPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>6.13</Amount>
      </ListingPrice>

,第二个ASIN的XML是:

    <GetLowestOfferListingsForASINResult ASIN="0870714376" status="Success">
  <AllOfferListingsConsidered>true</AllOfferListingsConsidered>
  <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
<Identifiers>
  <MarketplaceASIN>
    <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
    <ASIN>0870714376</ASIN>
  </MarketplaceASIN>
</Identifiers>
<LowestOfferListings>
  <LowestOfferListing>
    <Qualifiers>
      <ItemCondition>New</ItemCondition>
      <ItemSubcondition>New</ItemSubcondition>
      <FulfillmentChannel>Merchant</FulfillmentChannel>
      <ShipsDomestically>True</ShipsDomestically>
      <ShippingTime>
        <Max>0-2 days</Max>
      </ShippingTime>
      <SellerPositiveFeedbackRating>98-100%</SellerPositiveFeedbackRating>
    </Qualifiers>
    <NumberOfOfferListingsConsidered>2</NumberOfOfferListingsConsidered>
    <SellerFeedbackCount>1416</SellerFeedbackCount>
    <Price>
      <LandedPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>18.49</Amount>
      </LandedPrice>
      <ListingPrice>
        <CurrencyCode>USD</CurrencyCode>
        <Amount>14.50</Amount>
      </ListingPrice>

正如您所看到的,每个的数量都不同,但是当我运行程序时,我只从第一个ASIN获得金额。以下是获取结果的PHP代码:

    $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, $ItemCondition);
$currentPrice = $price_xml ->GetLowestOfferListingsForASINResult->Product->LowestOfferListings->LowestOfferListing;

// check to see if there are values
if(!empty($currentPrice))
{  

    foreach($currentPrice as $offer){
            $totalFeedback = $offer->SellerFeedbackCount;
        //if ($totalFeedback >99) {
            $condition = $offer->Qualifiers->ItemSubcondition;

                //amazon condition matching algorithm (so we can match our condition up against amazons conditions)
                switch ($condition) {
                case "New":
                    $amazonCondition = 5;
                    break;
                case "Mint":
                    $amazonCondition = 4;
                    break;
                case "VeryGood":
                    $amazonCondition = 3;
                    break;
                case "Good":
                    $amazonCondition = 2;
                    break;
                case "Acceptable":
                    $amazonCondition = 1;
                    break;
                default:
                    $amazonCondition = 0;
                }

            $lowestAmazonPrice = 0;

                            $currentPrice = $price_xml ->GetLowestOfferListingsForASINResult->Product->LowestOfferListings;
             foreach($currentPrice->LowestOfferListing as $offer){
                                if( ($ourCondition = $amazonCondition) /*&& ($totalFeedback >= 5000)*/) {
                    $offerArray[$y] = str_replace('$','',$offer->Price->ListingPrice->Amount);
                    $y++;
                                }   
            }
            $lowestAmazonPrice = min($offerArray);
    }

                    if ($fillzPrice > $lowestAmazonPrice ) {
                        $avgPrice = ($lowestAmazonPrice - ($lowestAmazonPrice * .08));
                         $source = "Adjusted Fillz Price";
                    } else {
                        $avgPrice = $fillzPrice;
                        $source = "Original Price";
                    }
    // check to make sure we are not going below established floor for prices
            if($avgPrice < ($follettPrice * 2.37)){
        $avgPrice = $follettPrice * 2.37;
        $source = "Follett Pricing";
    }
    if($avgPrice < ($row['cost'] * 2)){
        $avgPrice = $row['cost'] * 2;
        $source = "Double Cost";
    }
    /*if($avgPrice < 5.50){
        $avgPrice = 5.50;
        $source = "Lowest Base Cost";
    }*/
            //update fillzPrice
    $conn->query("UPDATE inventory SET ourPrice = $avgPrice, upload = '1' WHERE sku=" . $row['sku']);

    $pricedSKU[$n] = array('sku' => $row['sku'],
                            'new price' => number_format($avgPrice, 2, '.', ''),
                            'price source' => $source,
                                                            'Fillz' => $fillzPrice,
                                                            'Amazon'=> $lowestAmazonPrice,
                                                            'asin'=> $asin,
                                                            'condition'=>$ourCondition
                            );

    $n++;

}

}

正如我之前所说,除了这个问题,大部分输出都是正确的。我收到了一封带有ASIN的电子邮件和金额,所以我可以查看结果。有什么想法我在做错什么以及如何纠正它?

1 个答案:

答案 0 :(得分:0)

如果其他人遇到此问题,解决方法是添加以下代码:

 $offerArray = array();
在第一个while语句之后

。现在一切都很好。