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
2
Answers
This part of code will not iterate through Variation or NameValueList elements:
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:
XML allows to represent data in a hierarchical fashion, also as some kind of tree.
In the ebay answer you have one
<Item>
element.That
<Item>
element can have one<Variations>
element.That
<Variations>
element can have zero or more<Variation>
elements.The
<Variantion>
elements then again can have<Name>
/<Value>
pairs inside zero or more<VariationSpecifics>
<NameValueList>
elements.For the elements that can have more than one entry (1..n) you need to do iterations over.
The following is an example of such an iteration for plain text output (as it’s easier for demonstration purposes):
It shows that you need to stack multiple foreach’es into each other. Here those are two – because there are two elements marked with (1..n) in the schema above.
The inner foreach is a bit special because I wrapped the
<NameValueList>
elements inside a CachingIterator. That one adds a nice method you can ask if there will be a next element in the foreach loop ($nameValues->hasNext()
) which is used in the example to separate the name/value pairs with a semicolon (“;
“).The exemplary (shortened) output is:
Now this is not always the display you want here. For example you might want to list all colours and for each colour you want to show the available sizes. This will become overly complicated with foreach’ing over the SimpleXMLElements you have there. An alternative to foreach is running one or more xpath queries over which’s result you can foreach again.
Here is another example that lists alls lengths by their colors:
Exemplary output: