2017-09-13 8 views
1

Walmart API의 v3/orders 부분을 사용하여 특정 날짜의 주문을 검색합니다.XML 응답 객체 구문 분석 [CURL 및 SimpleXML을 사용하는 Walmart API의 객체 (SimpleXMLElement) [x]

응답을 XML 형식으로 수신하고 있습니다. var_dump($result);을하는

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

<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> 
문자열로 XML을 출력합니다.

enter image description here

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

출력 object(SimpleXMLElement)[3]

.. 변수 $xml = simplexml_load_string($result);에 문자열을로드하고, 상기 노드의 경로를 좁히하려고 단순히 object(SimpleXMLElement)[2]

var_dump($xml); 출력 고 그리고 목표로하는 중$elements = $xml->{'ns3:list'}->{'ns3:elements'};

와 19,361,195,263,210 제가 성공적으로 수신하고 단순히 I 검증 한 사항 null

var_dumps 상기 I는 별도로 사용하여 배열하고 JSON으로 인코딩 및 디코딩을 시도한 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

를 사용 200의 응답

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

하지만 여전히

를 얻을 수

왜 XML이 object에 "고정"되어 있는데 어떻게 내용을 파싱 할 수 있습니까?

왜 내가이 문자열로 아무 것도 할 수 없다는 이유로 혼란스러워합니다. 샘플 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> 

이 시나리오에서 개별 노드의 구문을 분석하는 적절한 방법은 무엇입니까과 함께

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

: 여기

전체 스크립트?

답변

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 ($ 결과, 'SimpleXMLElement', 0, 'http : //walmart.com/mp/v3/orders'); 완벽하게 출력됩니다. –

+1

다행, 그게 도움이되었습니다 :) – OsDev