Looping XML with PHP -
i've read various things i've followed can't seem working. i'm returning xml ebay trading api, working, can't nodes echo out... (i'm newish php / xml)
here's sample of xml (stripped out 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 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 $order) { $orders[] = $order->toarray(); } echo 'order ack: ', $acknode->item(0)->nodevalue; echo '<br />sales quantity: ', $totalnode->item(0)->nodevalue; print_r(array_values($orders)); /* tried didn't work: foreach ($responsedoc->order $order) { echo '<br />' . $order->orderstatus; } */
i know api call working can echo out ack:success, , totalnumberofentries:3 not sure how @ order -> orderid, or order -> amountpaid.
i want loop each order values each...
many thanks!
you have correct code in way:
foreach ($responsedoc->getelementsbytagname('order') $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 want print data.
instead, if want capture data in array:
$orders = []; foreach ($responsedoc->getelementsbytagname('order') $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 );
annotations:
because <order>
nodes univocals in context, use
$responsedoc->getelementsbytagname('order')
but can use also
$responsedoc->getelementsbytagname('orderarray')->item(0)->getelementsbytagname('order')
or
$responsedoc->getelementsbytagname('orderarray')->item(0)->childnodes
Comments
Post a Comment