skip to Main Content

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


  1. This is probably wrong:

    foreach ($responseDoc->OrderArray->Order as $order) {
                                      ^^^^^^
    

    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>

    foreach ($responseDoc->OrderArray as $order) {
                                     ^---note the difference
    
    Login or Signup to reply.
  2. You can do that the same way, you are accessing Ack or TotalNumberOfEntries.

    To get list of Order nodes, you can do this:

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

    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

    $order->OrderStatus
    

    You have to do:

    $order->getElementsByTagName('OrderStatus')->item(0)->textContent;
    

    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:

    $orders = [];
    foreach ($orderList as $order) {    
        $orders[] = array(
            'OrderID' => $order->getElementsByTagName('OrderID')->item(0)->textContent,
            'OrderStatus' => $order->getElementsByTagName('OrderStatus')->item(0)->textContent,
            'AmountPaid' => $order->getElementsByTagName('AmountPaid')->item(0)->textContent.
            'AmountPaidCurrency' => $order->getElementsByTagName('AmountPaid')->item(0)->getAttribute('currencyID'),
        );
    }
    

    Please also note that I had to separately get currencyID attribute of AmountPaid. That’s one of reasons why you can’t simply convert HTML to array.

    Login or Signup to reply.
  3. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search