0

我正在尝试使用 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 和金额的电子邮件,以便我查看结果。关于我做错了什么以及如何纠正它的任何想法?

4

1 回答 1

0

如果其他人有这个问题,解决办法是添加这行代码:

 $offerArray = array();

在第一个 while 语句之后。现在一切正常。

于 2013-01-07T18:42:50.483 回答