phpxmlebay-api

Looping XML with PHP


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!


Solution

  • You have to correct your code in this way:

    foreach ($responseDoc->getElementsByTagName('Order') as $order) {
        echo 'Order ID: ' . $order->getElementsByTagName('OrderID')->item(0)->nodeValue;
        echo '<br />Order Status: ' . $order->getElementsByTagName('OrderStatus')->item(0)->nodeValue;
        echo '<br />Amount Paid: ' . $order->getElementsByTagName('AmountPaid')->item(0)->nodeValue . ' ' . $order->getElementsByTagName('AmountPaid')->item(0)->getAttribute('currencyID');
    }
    

    That if you want print the data.

    Instead, if you want capture data in an array:

    $orders = [];
    foreach ($responseDoc->getElementsByTagName('Order') as $order) {
        $row = array();
        $row['id'] = $order->getElementsByTagName('OrderID')->item(0)->nodeValue;
        $row['status'] = $order->getElementsByTagName('OrderStatus')->item(0)->nodeValue;
        $row['amount'] = $order->getElementsByTagName('AmountPaid')->item(0)->nodeValue . ' ' . $order->getElementsByTagName('AmountPaid')->item(0)->getAttribute('currencyID');
        $orders[] = $row;
    }
    print_r( $orders );
    

    sandbox demo

    Annotations:

    Because `` nodes are univocals in this context, I use
    $responseDoc->getElementsByTagName('Order')
    

    but you can use also

    $responseDoc->getElementsByTagName('OrderArray')->item(0)->getElementsByTagName('Order')
    

    or

    $responseDoc->getElementsByTagName('OrderArray')->item(0)->childNodes