php - Parsing XML Response Objects [object(SimpleXMLElement)[x] from Walmart API Using CURL and SimpleXML -


using v3/orders part of walmart api retrieve orders on given day.

i receiving response in xml format.

<ns3:list xmlns:ns2="http://walmart.com/mp/orders" xmlns:ns3="http://walmart.com/mp/v3/orders" xmlns:ns4="http://walmart.com/">   <ns3:meta>      <ns3:totalcount>1</ns3:totalcount>      <ns3:limit>10</ns3:limit>   </ns3:meta> <ns3:elements>    <ns3:order>...</ns3:order> </ns3:elements> 

... $result = curl_exec($ch); 

doing var_dump($result); outputs xml string.

enter image description here

loading string variable $xml = simplexml_load_string($result); , doing var_dump($xml); outputs object(simplexmlelement)[2]

trying further narrow down node path..

$list = $xml->{'ns3:list'}; 

outputs object(simplexmlelement)[3]

and trying target ns3:elements $elements = $xml->{'ns3:list'}->{'ns3:elements'};

var_dumps null

i've verified receiving response of 200 using $httpcode = curl_getinfo($ch, curlinfo_http_code);

i've additionally tried encoding , decoding array , json using

$xml = simplexml_load_string($result, "simplexmlelement", libxml_nocdata); $json = json_encode($xml); $array = json_decode($json,true); 

but still

array (size=0)   empty 

why xml "stuck" in object , how can parse it's contents?

very confused why can't string.

here full script:

function pkcs8_to_pem($der) {    static $begin_marker = "-----begin private key-----";   static $end_marker = "-----end private key-----";    $value = base64_encode($der);    $pem = $begin_marker . "\n";   $pem .= chunk_split($value, 64, "\n");   $pem .= $end_marker . "\n";    return $pem; }  function getclientsignature($url, $request_type, $timestamp) {   $walmart_secret = 'xxxxxxx';   $walmart_consumer_id = 'xxxxxxxxx';    $pem = pkcs8_to_pem(base64_decode($walmart_secret));   $private_key = openssl_pkey_get_private($pem);    $data = $walmart_consumer_id."\n";   $data .= $url."\n";   $data .= $request_type."\n";   $data .= $timestamp."\n";    $hash = defined("openssl_algo_sha256") ? openssl_algo_sha256 : "sha256";   if (!openssl_sign($data, $signature, $private_key, $hash)) {     return null;   }    return base64_encode($signature); }  $walmart_consumer_id = 'xxxxxxxxxxxx'; $walmart_channel_type = 'xxxxxxxxxxxxxxxxxxxx';  $request_type = "get";  $yesterday2 = new datetime(); $yesterday2->modify('-1 day'); $yesterday = $yesterday2->format('y-m-d');  $url = "https://marketplace.walmartapis.com/v3/orders?createdstartdate=" . $yesterday;  $timestamp = round(microtime(true) * 1000);  $signature = getclientsignature($url, $request_type, $timestamp);  $headers = array(); $headers[] = "accept: application/xml"; $headers[] = "wm_svc.name: walmart marketplace"; $headers[] = "wm_consumer.id: ".$walmart_consumer_id; $headers[] = "wm_sec.timestamp: ".$timestamp; $headers[] = "wm_sec.auth_signature: ".$signature; $headers[] = "wm_qos.correlation_id: ".mt_rand(); $headers[] = "wm_consumer.channel.type: " .$walmart_channel_type;  $ch = curl_init($url); curl_setopt($ch, curlopt_failonerror, true); curl_setopt($ch, curlopt_customrequest, $request_type); curl_setopt($ch, curlopt_autoreferer, true); curl_setopt($ch, curlopt_followlocation, true); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_httpheader, $headers);  $result = curl_exec($ch);  $xml = simplexml_load_string($result); 

along sample xml output

<ns3:list xmlns:ns2="http://walmart.com/mp/orders" xmlns:ns3="http://walmart.com/mp/v3/orders" xmlns:ns4="http://walmart.com/">    <ns3:meta>       <ns3:totalcount>1</ns3:totalcount>        <ns3:limit>10</ns3:limit>    </ns3:meta>   <ns3:elements>   <ns3:order>     <ns3:purchaseorderid>2575693098967</ns3:purchaseorderid>     <ns3:customerorderid>4021603941547</ns3:customerorderid>     <ns3:customeremailid>mgr@walmartlabs.com</ns3:customeremailid>     <ns3:orderdate>2016-05-11t23:16:10.000z</ns3:orderdate>     <ns3:shippinginfo>         <ns3:phone>6502248603</ns3:phone>         <ns3:estimateddeliverydate>2016-05-20t17:00:00.000z</ns3:estimateddeliverydate>         <ns3:estimatedshipdate>2016-05-16t17:00:00.000z</ns3:estimatedshipdate>         <ns3:methodcode>standard</ns3:methodcode>         <ns3:postaladdress>             <ns3:name>madhukara pgoms</ns3:name>             <ns3:address1>860 w cal ave</ns3:address1>             <ns3:address2>seat # 860c.2.176</ns3:address2>             <ns3:city>sunnyvale</ns3:city>             <ns3:state>ca</ns3:state>             <ns3:postalcode>94086</ns3:postalcode>             <ns3:country>usa</ns3:country>             <ns3:addresstype>residential</ns3:addresstype>         </ns3:postaladdress>     </ns3:shippinginfo>     <ns3:orderlines>         <ns3:orderline>             <ns3:linenumber>1</ns3:linenumber>             <ns3:item>                 <ns3:productname>garmin refurbished nuvi 2595lmt 5 gps w lifetime maps , traffic</ns3:productname>                 <ns3:sku>grmn100201</ns3:sku>             </ns3:item>             <ns3:charges>                 <ns3:charge>                     <ns3:chargetype>product</ns3:chargetype>                     <ns3:chargename>itemprice</ns3:chargename>                     <ns3:chargeamount>                         <ns3:currency>usd</ns3:currency>                         <ns3:amount>124.98</ns3:amount>                     </ns3:chargeamount>                     <ns3:tax>                         <ns3:taxname>tax1</ns3:taxname>                         <ns3:taxamount>                             <ns3:currency>usd</ns3:currency>                             <ns3:amount>10.94</ns3:amount>                         </ns3:taxamount>                     </ns3:tax>                 </ns3:charge>             </ns3:charges>             <ns3:orderlinequantity>                 <ns3:unitofmeasurement>each</ns3:unitofmeasurement>                 <ns3:amount>1</ns3:amount>             </ns3:orderlinequantity>             <ns3:statusdate>2016-05-11t23:43:50.000z</ns3:statusdate>             <ns3:orderlinestatuses>                 <ns3:orderlinestatus>                     <ns3:status>created</ns3:status>                     <ns3:statusquantity>                         <ns3:unitofmeasurement>each</ns3:unitofmeasurement>                         <ns3:amount>1</ns3:amount>                     </ns3:statusquantity>                 </ns3:orderlinestatus>             </ns3:orderlinestatuses>         </ns3:orderline>     </ns3:orderlines>   </ns3:order>  </ns3:elements> </ns3:list> 

what's proper way parse of individual nodes in scenario?

you can traverse de xml using namespace declaration

<?php  $xml = 'your_xml_string';  $data = simplexml_load_string($xml,'simplexmlelement',0,'http://walmart.com/mp/v3/orders');  echo (string) $data->meta->totalcount;   //you have cast value text value of element can traverse items  foreach($data->elements $el){ $el->order->purchaseorderid } 

Comments

  1. same issue from my side
    have you got any solution for that.

    ReplyDelete

Post a Comment