您好,我有一个 xml 提要,其中包含许多产品,每个产品都可以有多个 skus。我正在尝试遍历该 xml,然后如果找到 EAN 代码的匹配项,那么我将尝试将匹配的 skus 输出为单独的 xml 文件。
我到目前为止的代码如下,请你指出我哪里出错了,因为它还没有完全工作并且它输出一个空响应。
$ean = $_GET["ean"];
$trialXML = simplexml_load_file('trial.xml');
//function to lookup an EAN
function lookupEAN($ean,$xml) {
//loop over products
foreach($xml->products->product as $product) {
//loop over individual skus
foreach($product->sku as $sku) {
if ($sku->ean == $ean) {
$skuData .= $sku;
}
}
}
return $skuData;
}
//lookup EAN
$response = lookupEAN($ean,$trialXML);
Header('Content-type: text/xml');
print_r($response);
这是来自 xml 的示例产品:
<product>
<id>11077810</id>
<name>Cooke & Lewis Flow Soap Dispenser</name>
<categoryId>9372170</categoryId>
<features/>
<url>http://www.diy.com/diy/jsp/bq/nav.jsp?action=detail&fh_secondid=11077810&fh_location=//catalog01/en_GB/categories<{9372014}/categories<{9372035}/categories<{9372170}/specificationsProductType=bathroom_fittings/specificationsSpecificProductType=soap_dishes___dispensers</url>
<productHierarchy>
<productHierarchyElement>Rooms</productHierarchyElement>
<productHierarchyElement>Bathrooms</productHierarchyElement>
<productHierarchyElement>Bathroom Accessories</productHierarchyElement>
<productHierarchyElement>Bathroom Fittings</productHierarchyElement>
<productHierarchyElement>Soap Dishes & Dispensers</productHierarchyElement>
</productHierarchy>
<averageRating>0.0</averageRating>
<numberOfReviews>0</numberOfReviews>
<sku>
<id>11568516</id>
<name>Cooke & Lewis Flow Soap Dispenser</name>
<description>From the tradtional to the downright modern, we have accessories to suit the style of your home. Choose from the classic bronze effect through to the contemporary chrome to add a finishing touch to your bathroom. Glass and wood enhance some of our bathroom ranges and some have matching toilet seats.</description>
<ean>0000005286911</ean>
<price>29.98</price>
<wasPrice/>
<deliveryBand>Next day</deliveryBand>
<deliveryCost>5.0</deliveryCost>
<deliveryTime>Collect at Store</deliveryTime>
<deliveryLeadTime>0</deliveryLeadTime>
<stockAvailability>1</stockAvailability>
<channel>In Store Only</channel>
<buyerCats>
<catLevel0>BATHROOMS</catLevel0>
<catLevel1>WALL MOUNTED ACCESSORIES</catLevel1>
<catLevel2>DECO</catLevel2>
</buyerCats>
<affiliateCats>
<affiliateCat0>Home & Garden</affiliateCat0>
</affiliateCats>
<categoryHierarchy>Bathroom Fittings</categoryHierarchy>
<imageUrl>http://s7g1.scene7.com/is/image/BandQ/0000005286911_001c_v001_zp</imageUrl>
<thumbnailUrl>http://s7g1.scene7.com/is/image/BandQ/0000005286911_001c_v001_zp?$75x75_generic$=</thumbnailUrl>
<facets>
<facet name="styleStyle">Flow</facet>
<facet name="ecoRecycle">false</facet>
<facet name="specificationsSpecificProductType">Soap Dishes & Dispensers</facet>
<facet name="ecoGrowFoods">false</facet>
<facet name="ecoSavesEnergy">false</facet>
<facet name="ecoNurtureNature">false</facet>
<facet name="ecoDLME">false</facet>
<facet name="specificationsProductType">Bathroom Fittings</facet>
<facet name="featuresBrand">Cooke & Lewis</facet>
<facet name="ecoHealthyHomes">false</facet>
<facet name="ecoSavesWater">false</facet>
<facet name="featuresColour">Chrome</facet>
</facets>
<relatedItems>
<relatedItem>0000005287512</relatedItem>
<relatedItem>0000005287673</relatedItem>
<relatedItem>0000005287673</relatedItem>
</relatedItems>
<optionalTags>
<optionalTag>ROOMS15</optionalTag>
</optionalTags>
</sku>
</product>