I’ve read various things that I’ve followed but I can’t seem to get this working. I’m returning XML from the eBay Trading API, which is working, but I can’t get some nodes to echo out… (i’m newish to php / xml)
Here’s a sample of the XML (stripped out as it contains order data):
<?xml version="1.0" encoding="UTF-8"?>
<GetOrdersResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2016-01-28T10:48:19.262Z</Timestamp>
<Ack>Success</Ack>
<Version>949</Version>
<Build>E949_INTL_APIXO_17770994_R1</Build>
<PaginationResult>
<TotalNumberOfEntries>3</TotalNumberOfEntries>
</PaginationResult>
<HasMoreOrders>false</HasMoreOrders>
<OrderArray>
<Order>
<OrderID>400930822745-519268006027</OrderID>
<OrderStatus>Completed</OrderStatus>
<AmountPaid currencyID="GBP">213.0</AmountPaid>
</Order>
<Order>
<OrderID>191593173254-1228482367009</OrderID>
<OrderStatus>Completed</OrderStatus>
<AmountPaid currencyID="GBP">289.0</AmountPaid>
</Order>
<Order>
<OrderID>400930822765-514681551027</OrderID>
<OrderStatus>Completed</OrderStatus>
<AmountPaid currencyID="GBP">238.0</AmountPaid>
</Order>
</OrderArray>
</GetOrdersResponse>
Here’s the PHP i’m using:
$responseDoc = new DomDocument();
$responseDoc->loadXML($responseXml);
$ackNode = $responseDoc->getElementsByTagName('Ack');
$paginationCategoryNode = $responseDoc->getElementsByTagName('PaginationResult');
$totalNode = $paginationCategoryNode->item(0)->getElementsByTagName('TotalNumberOfEntries');
$orders = [];
foreach ($responseDoc->OrderArray->Order as $order) {
$orders[] = $order->toArray();
}
echo 'Order Ack: ', $ackNode->item(0)->nodeValue;
echo '<br />Sales Quantity: ', $totalNode->item(0)->nodeValue;
print_r(array_values($orders));
/* also tried this also but didn't work:
foreach ($responseDoc->Order as $Order) {
echo '<br />' . $Order->OrderStatus;
}
*/
I know the API call is working as I can echo out the Ack:Success, and TotalNumberOfEntries:3 but not sure how to get at the Order -> OrderID, or order -> AmountPaid.
I want to loop each order to get the values for each…
Many thanks!
3
Answers
This is probably wrong:
You want to loop on the
Order
component, but since there’s MULTIPLE of those<order>
tags, you need to loop on its parent container, e.g. the<OrderArray>
You can do that the same way, you are accessing
Ack
orTotalNumberOfEntries
.To get list of
Order
nodes, you can do this:Then you can iterate it, but inside your loop you are still dealing with
DOMElement
objects so you can’t use it like simple object.So in order to get for example OrderStatus value, instead of
You have to do:
Also please keep in mind that there’s no such function like
DOMElement::toArray()
, therefore you should access every child node separately in your loop, to create$orders
array.Final loop could look like this:
Please also note that I had to separately get
currencyID
attribute ofAmountPaid
. That’s one of reasons why you can’t simply convert HTML to array.You have to correct your code in this way:
That if you want print the data.
Instead, if you want capture data in an array:
sandbox demo
Annotations:
Because “ nodes are univocals in this context, I use
but you can use also
or