我使用if语句来检查XML对象是否存在,但是当我得到trying to get property of non-object error时,它不能很好地工作。
下面是代码
foreach($resp->GetLowestOfferListingsForASINResult as $product){
$isbn = "";
$price = "";
$condition = "";
if($product->Product->Identifiers->MarketplaceASIN->ASIN){
$isbn = (string) $product->Product->Identifiers->MarketplaceASIN->ASIN;
}
if($product->Product->LowestOfferListings->LowestOfferListing->Price->LandedPrice->Amount){
$price = (float) $product->Product->LowestOfferListings->LowestOfferListing->Price->LandedPrice->Amount;
}
if($product->Product->LowestOfferListings->LowestOfferListing->Qualifiers->ItemSubcondition){
$condition = (string) $product->Product->LowestOfferListings->LowestOfferListing->Qualifiers->ItemSubcondition;
}
}我试过用
if(!property_exists($product, 'Amount')){
$price = (float) $product->Product->LowestOfferListings->LowestOfferListing->Price->LandedPrice->Amount;
}但这不起作用,我应该用什么
xml返回的结果(如果存在价格)
<?xml version="1.0"?>
<GetLowestOfferListingsForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetLowestOfferListingsForASINResult ASIN="000726965X" 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>A1F83G8C2ARO7P</MarketplaceId>
<ASIN>000726965X</ASIN>
</MarketplaceASIN>
</Identifiers>
<LowestOfferListings>
<LowestOfferListing>
<Qualifiers>
<ItemCondition>Used</ItemCondition>
<ItemSubcondition>Mint</ItemSubcondition>
<FulfillmentChannel>Amazon</FulfillmentChannel>
<ShipsDomestically>True</ShipsDomestically>
<ShippingTime>
<Max>0-2 days</Max>
</ShippingTime>
<SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
</Qualifiers>
<NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
<SellerFeedbackCount>500720</SellerFeedbackCount>
<Price>
<LandedPrice>
<CurrencyCode>GBP</CurrencyCode>
<Amount>12.65</Amount>
</LandedPrice>
<ListingPrice>
<CurrencyCode>GBP</CurrencyCode>
<Amount>12.65</Amount>
</ListingPrice>
<Shipping>
<CurrencyCode>GBP</CurrencyCode>
<Amount>0.00</Amount>
</Shipping>
</Price>
<MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
</LowestOfferListing>
<LowestOfferListing>
<Qualifiers>
<ItemCondition>Used</ItemCondition>
<ItemSubcondition>Acceptable</ItemSubcondition>
<FulfillmentChannel>Merchant</FulfillmentChannel>
<ShipsDomestically>True</ShipsDomestically>
<ShippingTime>
<Max>0-2 days</Max>
</ShippingTime>
<SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
</Qualifiers>
<NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
<SellerFeedbackCount>73208</SellerFeedbackCount>
<Price>
<LandedPrice>
<CurrencyCode>GBP</CurrencyCode>
<Amount>15.41</Amount>
</LandedPrice>
<ListingPrice>
<CurrencyCode>GBP</CurrencyCode>
<Amount>12.61</Amount>
</ListingPrice>
<Shipping>
<CurrencyCode>GBP</CurrencyCode>
<Amount>2.80</Amount>
</Shipping>
</Price>
<MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
</LowestOfferListing>
<LowestOfferListing>
<Qualifiers>
<ItemCondition>Used</ItemCondition>
<ItemSubcondition>Mint</ItemSubcondition>
<FulfillmentChannel>Merchant</FulfillmentChannel>
<ShipsDomestically>True</ShipsDomestically>
<ShippingTime>
<Max>0-2 days</Max>
</ShippingTime>
<SellerPositiveFeedbackRating>98-100%</SellerPositiveFeedbackRating>
</Qualifiers>
<NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
<SellerFeedbackCount>2125</SellerFeedbackCount>
<Price>
<LandedPrice>
<CurrencyCode>GBP</CurrencyCode>
<Amount>22.80</Amount>
</LandedPrice>
<ListingPrice>
<CurrencyCode>GBP</CurrencyCode>
<Amount>20.00</Amount>
</ListingPrice>
<Shipping>
<CurrencyCode>GBP</CurrencyCode>
<Amount>2.80</Amount>
</Shipping>
</Price>
<MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
</LowestOfferListing>
<LowestOfferListing>
<Qualifiers>
<ItemCondition>Used</ItemCondition>
<ItemSubcondition>Mint</ItemSubcondition>
<FulfillmentChannel>Merchant</FulfillmentChannel>
<ShipsDomestically>True</ShipsDomestically>
<ShippingTime>
<Max>0-2 days</Max>
</ShippingTime>
<SellerPositiveFeedbackRating>90-94%</SellerPositiveFeedbackRating>
</Qualifiers>
<NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
<SellerFeedbackCount>68301</SellerFeedbackCount>
<Price>
<LandedPrice>
<CurrencyCode>GBP</CurrencyCode>
<Amount>23.14</Amount>
</LandedPrice>
<ListingPrice>
<CurrencyCode>GBP</CurrencyCode>
<Amount>20.34</Amount>
</ListingPrice>
<Shipping>
<CurrencyCode>GBP</CurrencyCode>
<Amount>2.80</Amount>
</Shipping>
</Price>
<MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
</LowestOfferListing>
<LowestOfferListing>
<Qualifiers>
<ItemCondition>Used</ItemCondition>
<ItemSubcondition>Mint</ItemSubcondition>
<FulfillmentChannel>Merchant</FulfillmentChannel>
<ShipsDomestically>False</ShipsDomestically>
<ShippingTime>
<Max>0-2 days</Max>
</ShippingTime>
<SellerPositiveFeedbackRating>90-94%</SellerPositiveFeedbackRating>
</Qualifiers>
<NumberOfOfferListingsConsidered>2</NumberOfOfferListingsConsidered>
<SellerFeedbackCount>4114</SellerFeedbackCount>
<Price>
<LandedPrice>
<CurrencyCode>GBP</CurrencyCode>
<Amount>25.39</Amount>
</LandedPrice>
<ListingPrice>
<CurrencyCode>GBP</CurrencyCode>
<Amount>22.59</Amount>
</ListingPrice>
<Shipping>
<CurrencyCode>GBP</CurrencyCode>
<Amount>2.80</Amount>
</Shipping>
</Price>
<MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
</LowestOfferListing>
<LowestOfferListing>
<Qualifiers>
<ItemCondition>Used</ItemCondition>
<ItemSubcondition>Mint</ItemSubcondition>
<FulfillmentChannel>Merchant</FulfillmentChannel>
<ShipsDomestically>True</ShipsDomestically>
<ShippingTime>
<Max>0-2 days</Max>
</ShippingTime>
<SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
</Qualifiers>
<NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
<SellerFeedbackCount>34661</SellerFeedbackCount>
<Price>
<LandedPrice>
<CurrencyCode>GBP</CurrencyCode>
<Amount>29.20</Amount>
</LandedPrice>
<ListingPrice>
<CurrencyCode>GBP</CurrencyCode>
<Amount>26.40</Amount>
</ListingPrice>
<Shipping>
<CurrencyCode>GBP</CurrencyCode>
<Amount>2.80</Amount>
</Shipping>
</Price>
<MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
</LowestOfferListing>
<LowestOfferListing>
<Qualifiers>
<ItemCondition>Used</ItemCondition>
<ItemSubcondition>VeryGood</ItemSubcondition>
<FulfillmentChannel>Merchant</FulfillmentChannel>
<ShipsDomestically>False</ShipsDomestically>
<ShippingTime>
<Max>0-2 days</Max>
</ShippingTime>
<SellerPositiveFeedbackRating>95-97%</SellerPositiveFeedbackRating>
</Qualifiers>
<NumberOfOfferListingsConsidered>1</NumberOfOfferListingsConsidered>
<SellerFeedbackCount>49504</SellerFeedbackCount>
<Price>
<LandedPrice>
<CurrencyCode>GBP</CurrencyCode>
<Amount>33.16</Amount>
</LandedPrice>
<ListingPrice>
<CurrencyCode>GBP</CurrencyCode>
<Amount>30.36</Amount>
</ListingPrice>
<Shipping>
<CurrencyCode>GBP</CurrencyCode>
<Amount>2.80</Amount>
</Shipping>
</Price>
<MultipleOffersAtLowestPrice>False</MultipleOffersAtLowestPrice>
</LowestOfferListing>
</LowestOfferListings>
</Product>
</GetLowestOfferListingsForASINResult>
<ResponseMetadata>
<RequestId>09a1deae-94ec-4661-95ae-49567ab00eb8</RequestId>
</ResponseMetadata>
</GetLowestOfferListingsForASINResponse>xml返回的结果(如果不存在价格)
<?xml version="1.0"?>
<GetLowestOfferListingsForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetLowestOfferListingsForASINResult ASIN="0957143826" 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>A1F83G8C2ARO7P</MarketplaceId>
<ASIN>0957143826</ASIN>
</MarketplaceASIN>
</Identifiers>
<LowestOfferListings/>
</Product>
</GetLowestOfferListingsForASINResult>
<ResponseMetadata>
<RequestId>13918cb9-69e5-4445-ad24-70f6fda06f19</RequestId>
</ResponseMetadata>
</GetLowestOfferListingsForASINResponse>发布于 2012-08-18 22:24:51
这里的问题是,您对->操作符链的测试太深了:
if ( $product->Product->LowestOfferListings->LowestOfferListing->Price->LandedPrice->Amount )为了测试这一点,PHP必须依次计算每个->运算符。
$product->Product正常$product->Product->LowestOfferListings正常$product->Product->LowestOfferListings->LowestOfferListing是一套空集$product->Product->LowestOfferListings->LowestOfferListing->Price未定义此外,即使SimpleXML对象是空列表,它也不会计算为false,因此您确实需要使用isset。因此,为了发现没有价格的情况,您需要这样做:
if ( isset($product->Product->LowestOfferListings->LowestOfferListing) )注意:我还没有对上面的内容进行实际测试,所以我可能在某些具体问题上出错了,但我很确定这是您的基本问题。
https://stackoverflow.com/questions/11939774
复制相似问题