2017-09-13 132 views
1

使用Walmart API的v3/orders部分來檢索給定日期的訂單。使用CURL和SimpleXML解析XML響應對象[對象(SimpleXMLElement)[x]使用CURL和SimpleXML

我成功接收XML格式的響應。

<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); 

做一個var_dump($result);輸出XML作爲字符串。

enter image description here

加載字符串轉換成可變$xml = simplexml_load_string($result);並做了var_dump($xml);輸出簡單地object(SimpleXMLElement)[2]

試圖進一步縮小節點路徑..

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

輸出object(SimpleXMLElement)[3]

並試圖以爲目標與$elements = $xml->{'ns3:list'}->{'ns3:elements'};

var_dumps只是一個null

我已驗證我成功接收使用$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

我還另外使用試圖編碼和解碼到一個數組和JSON 200的響應

$xml = simplexml_load_string($result, "SimpleXMLElement", LIBXML_NOCDATA); 
$json = json_encode($xml); 
$array = json_decode($json,TRUE); 

,但仍得到一個

array (size=0) 
    empty 

爲什麼XML會「卡在」object中,我該如何解析它的內容?

非常困惑,爲什麼我不能用這個字符串做任何事情。

以下是完整的腳本:

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); 

與樣本XML輸出

<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>[email protected]</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 and 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> 

什麼是分析任何單個節點在這種情況下適當的方式相處?

回答

2

您可以使用該命名空間聲明

<?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 to cast the value to get the text value of an element also you can traverse items like this 

foreach($data->elements as $el){ $el->order->purchaseOrderId } 
+0

真棒簡單地遍歷XML德!謝謝。不知道你可以在'simplexml_load_string'中包含名字空間。 - '$ data = simplexml_load_string($ result,'SimpleXMLElement',0,'http://walmart.com/mp/v3/orders');'輸出完美。 –

+1

很高興,這有助於你:) – OsDev