Trying to parse using SimpleXML function, which is working fine. But I've been stuck on how to extract the 'colour' & 'length data from the XML
Below is snippet of the 'ebay-response.xml' file referenced in the code:
Complete xml file can be downloaded from ebay-response.xml
<?xml version="1.0" encoding="UTF-8"?>
<GetItemResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2015-08-03T11:45:56.061Z</Timestamp>
<Ack>Success</Ack>
<Version>927</Version>
<Build>E927_INTL_API_17590342_R1</Build>
<Item>
<Quantity>25000</Quantity>
<ShippingDetails />
<Title>CLOSED END ZIPS 40 Colours 4 6 8 10 12in (10-30cm) for Skirt Trousers Craft</Title>
<Variations>
<Variation>
<SKU>1176:3448</SKU>
<Quantity>100</Quantity>
<VariationSpecifics>
<NameValueList>
<Name>Colour</Name>
<Value>White</Value>
</NameValueList>
<NameValueList>
<Name>Length</Name>
<Value>4" (10cm)</Value>
</NameValueList>
</VariationSpecifics>
</Variation>
<Variation>
<SKU>1176:3449</SKU>
<Quantity>100</Quantity>
<VariationSpecifics>
<NameValueList>
<Name>Colour</Name>
<Value>White</Value>
</NameValueList>
<NameValueList>
<Name>Length</Name>
<Value>5" (12cm)</Value>
</NameValueList>
</VariationSpecifics>
</Variation>
<Variation>
<SKU>1176:3450</SKU>
<Quantity>100</Quantity>
<VariationSpecifics>
<NameValueList>
<Name>Colour</Name>
<Value>White</Value>
</NameValueList>
<NameValueList>
<Name>Length</Name>
<Value>6" (15cm)</Value>
</NameValueList>
</VariationSpecifics>
</Variation>
<Variation>
<SKU>1176:3451</SKU>
<Quantity>100</Quantity>
<VariationSpecifics>
<NameValueList>
<Name>Colour</Name>
<Value>White</Value>
</NameValueList>
<NameValueList>
<Name>Length</Name>
<Value>7" (18cm)</Value>
</NameValueList>
</VariationSpecifics>
</Variation>
<Variation>
My current PHP script is:
if(!$resp = simplexml_load_file("ebay-response.xml"))
{
echo "Unable to load XML Stream from eBAY API, possible no response from eBay?<br />\n";
return;
}
if ($resp->Ack != "Success") {
echo 'eBay Response Status was: ' . $resp->Ack . " Unable to parse the XML <br />\n";
return;
}
echo 'eBay Response Status: ' . $resp->Ack . "<br />\n";
echo 'ebay Response Timestamp: ' . $resp->Timestamp . "<br />\n";
echo 'ebay API Version: ' . $resp->Version . "<br />\n";
echo 'ebay API Build: ' . $resp->Build . "<br />\n";
echo 'eBay Item Title: ' . $resp->Item->Title . "<br />\n";
echo 'Total Items (all variations): ' . $resp->Item->Quantity . "<br />\n<br />\n";
foreach( $resp->Item->Variations->children() as $SkuAndQuantity )
{
echo 'Title: ' . $resp->Item->Title . ' SKU: ' . $SkuAndQuantity->SKU .
' Qty: ' . $SkuAndQuantity->Quantity . "<br />\n";
foreach( $resp->Item->Variations->Variation->VariationSpecifics->NameValueList->children() as $options )
{
echo $options .'<br />';
}
}
echo "<br />\n";
What I get back when I run my code is shown below, as you can see I appear to be pulling back only colour white (and no length) and even with the colour I appear to be only getting the colour from the 1st 'Variation' element
eBay Response Status: Success
ebay Response Timestamp: 2015-08-03T11:45:56.061Z
ebay API Version: 927
ebay API Build: E927_INTL_API_17590342_R1
eBay Item Title: CLOSED END ZIPS 40 Colours 4 6 8 10 12in (10-30cm) for Skirt Trousers Craft
Total Items (all variations): 25000
Title: CLOSED END ZIPS 40 Colours 4 6 8 10 12in (10-30cm) for Skirt Trousers Craft SKU: 1176:3448 Qty: 100
Colour
White
Title: CLOSED END ZIPS 40 Colours 4 6 8 10 12in (10-30cm) for Skirt Trousers Craft SKU: 1176:3449 Qty: 100
Colour
White
Title: CLOSED END ZIPS 40 Colours 4 6 8 10 12in (10-30cm) for Skirt Trousers Craft SKU: 1176:3450 Qty: 100
Colour
White
Title: CLOSED END ZIPS 40 Colours 4 6 8 10 12in (10-30cm) for Skirt Trousers Craft SKU: 1176:3451 Qty: 100
Colour
White
Title: CLOSED END ZIPS 40 Colours 4 6 8 10 12in (10-30cm) for Skirt Trousers Craft SKU: 1176:3452 Qty: 100
Colour
White
Title: CLOSED END ZIPS 40 Colours 4 6 8 10 12in (10-30cm) for Skirt Trousers Craft SKU: 1176:3453 Qty: 100
Colour
White
Title: CLOSED END ZIPS 40 Colours 4 6 8 10 12in (10-30cm) for Skirt Trousers Craft SKU: 1176:3454 Qty: 100
Colour
This part of code will not iterate through Variation or NameValueList elements:
foreach($resp->Item->Variations->Variation->VariationSpecifics->NameValueList->children() as $options)
{
echo $options .'<br />';
}
It will always use the first Variation element and first NameValueList element inside. This causes your problem.
You need to change your code to something like:
foreach($resp->Item->Variations->children() as $Variation)
{
echo 'Title: ' . $resp->Item->Title . ' SKU: ' . $Variation->SKU .' Qty: ' . $Variation->Quantity .'<br />';
foreach($Variation->VariationSpecifics->children() as $NameValueList)
foreach($NameValueList->children() as $option)
echo $option .'<br />';
}