如何解析php中的soap xml?

时间:2015-07-10 21:28:03

标签: php xml soap xml-parsing

您好我在使用PHP的API调用中获得下面给出的soap xml响应,但我无法解析它,因此我可以正确地存储到数据库中。

<?xml version="1.0" encoding="UTF-8"?>
<ns2:processedSalesOrderMessage xmlns:ns2="api.supplieroasis.com" xmlns:ns3="api.supplieroasis.com/retailorder" xmlns:ns4="https://mpc.overstock.com/jms/sofs">
   <ns2:processedSalesOrder>
      <ns2:salesChannelOrderNumber>131415</ns2:salesChannelOrderNumber>
      <ns2:salesChannelName>OFF_NETWORK</ns2:salesChannelName>
      <ns2:orderDate>2015-06-05T09:19:18.000-06:00</ns2:orderDate>
      <ns2:sofsCreatedDate>2015-06-05T09:19:20.000-06:00</ns2:sofsCreatedDate>
      <ns2:warehouseName>
         <ns2:code>1969</ns2:code>
      </ns2:warehouseName>
      <ns2:shipToAddress>
         <ns2:contactName>Test Order1</ns2:contactName>
         <ns2:address1>1969 S. Los Angeles St</ns2:address1>
         <ns2:city>Los Angeles</ns2:city>
         <ns2:stateOrProvince>CA</ns2:stateOrProvince>
         <ns2:postalCode>90011</ns2:postalCode>
         <ns2:countryCode>US</ns2:countryCode>
         <ns2:phone>213-745-2023</ns2:phone>
      </ns2:shipToAddress>
      <ns2:returnAddress>
         <ns2:contactName>1969</ns2:contactName>
         <ns2:address1>1969 S. Los Angeles St</ns2:address1>
         <ns2:city>Los Angeles</ns2:city>
         <ns2:stateOrProvince>CA</ns2:stateOrProvince>
         <ns2:postalCode>90011</ns2:postalCode>
         <ns2:countryCode>US</ns2:countryCode>
         <ns2:phone>213-745-2023</ns2:phone>
      </ns2:returnAddress>
      <ns2:shippingSpecifications>
         <ns2:isThirdPartyBilling>false</ns2:isThirdPartyBilling>
         <ns2:isSignatureRequired>false</ns2:isSignatureRequired>
         <ns2:isDeclaredValueRequired>false</ns2:isDeclaredValueRequired>
         <ns2:smallParcelShipment>
            <ns2:shippingServiceLevel>
               <ns2:code>GROUND</ns2:code>
            </ns2:shippingServiceLevel>
         </ns2:smallParcelShipment>
         <ns2:isExport>false</ns2:isExport>
      </ns2:shippingSpecifications>
      <ns2:branding />
      <ns2:orderFulfillment>ALLOW_PARTIAL_FILL</ns2:orderFulfillment>
      <ns2:orderId>47511</ns2:orderId>
      <ns2:status>PROCESSING</ns2:status>
      <ns2:retailChannelCode>OFF_NETWORK_1501</ns2:retailChannelCode>
      <ns2:retailOrderNumber>131415</ns2:retailOrderNumber>
      <ns2:actionRequired>true</ns2:actionRequired>
      <ns2:processedSalesOrderLine>
         <ns2:salesChannelLineId>1</ns2:salesChannelLineId>
         <ns2:salesChannelLineNumber>1</ns2:salesChannelLineNumber>
         <ns2:partnerSKU>ALDO_5225_37_48R</ns2:partnerSKU>
         <ns2:barcode>ALDO_5225_37_48R</ns2:barcode>
         <ns2:salesChannelSKU>ALDO_5225_37_48R</ns2:salesChannelSKU>
         <ns2:quantity>1</ns2:quantity>
         <ns2:itemPrice>0.00</ns2:itemPrice>
         <ns2:lineId>54600</ns2:lineId>
         <ns2:itemId>211437</ns2:itemId>
         <ns2:itemName>Ferrecci Men's 'Zonettie' 2-piece Navy Suit</ns2:itemName>
         <ns2:lineStatus>PROCESSING</ns2:lineStatus>
         <ns2:unitCost>0.00</ns2:unitCost>
         <ns2:unitCostCurrencyCode>USD</ns2:unitCostCurrencyCode>
      </ns2:processedSalesOrderLine>
   </ns2:processedSalesOrder>
</ns2:processedSalesOrderMessage>

我在API响应中将其作为字符串获取?任何人都可以帮助我,如何解析PHP ???

2 个答案:

答案 0 :(得分:3)

您可以将其转换为数组:

<?php

$resp = '<?xml version="1.0" encoding="UTF-8"?>
<ns2:processedSalesOrderMessage xmlns:ns2="api.supplieroasis.com" xmlns:ns3="api.supplieroasis.com/retailorder" xmlns:ns4="https://mpc.overstock.com/jms/sofs">
   <ns2:processedSalesOrder>
      <ns2:salesChannelOrderNumber>131415</ns2:salesChannelOrderNumber>
      <ns2:salesChannelName>OFF_NETWORK</ns2:salesChannelName>
      <ns2:orderDate>2015-06-05T09:19:18.000-06:00</ns2:orderDate>
      <ns2:sofsCreatedDate>2015-06-05T09:19:20.000-06:00</ns2:sofsCreatedDate>
      <ns2:warehouseName>
         <ns2:code>1969</ns2:code>
      </ns2:warehouseName>
      <ns2:shipToAddress>
         <ns2:contactName>Test Order1</ns2:contactName>
         <ns2:address1>1969 S. Los Angeles St</ns2:address1>
         <ns2:city>Los Angeles</ns2:city>
         <ns2:stateOrProvince>CA</ns2:stateOrProvince>
         <ns2:postalCode>90011</ns2:postalCode>
         <ns2:countryCode>US</ns2:countryCode>
         <ns2:phone>213-745-2023</ns2:phone>
      </ns2:shipToAddress>
      <ns2:returnAddress>
         <ns2:contactName>1969</ns2:contactName>
         <ns2:address1>1969 S. Los Angeles St</ns2:address1>
         <ns2:city>Los Angeles</ns2:city>
         <ns2:stateOrProvince>CA</ns2:stateOrProvince>
         <ns2:postalCode>90011</ns2:postalCode>
         <ns2:countryCode>US</ns2:countryCode>
         <ns2:phone>213-745-2023</ns2:phone>
      </ns2:returnAddress>
      <ns2:shippingSpecifications>
         <ns2:isThirdPartyBilling>false</ns2:isThirdPartyBilling>
         <ns2:isSignatureRequired>false</ns2:isSignatureRequired>
         <ns2:isDeclaredValueRequired>false</ns2:isDeclaredValueRequired>
         <ns2:smallParcelShipment>
            <ns2:shippingServiceLevel>
               <ns2:code>GROUND</ns2:code>
            </ns2:shippingServiceLevel>
         </ns2:smallParcelShipment>
         <ns2:isExport>false</ns2:isExport>
      </ns2:shippingSpecifications>
      <ns2:branding />
      <ns2:orderFulfillment>ALLOW_PARTIAL_FILL</ns2:orderFulfillment>
      <ns2:orderId>47511</ns2:orderId>
      <ns2:status>PROCESSING</ns2:status>
      <ns2:retailChannelCode>OFF_NETWORK_1501</ns2:retailChannelCode>
      <ns2:retailOrderNumber>131415</ns2:retailOrderNumber>
      <ns2:actionRequired>true</ns2:actionRequired>
      <ns2:processedSalesOrderLine>
         <ns2:salesChannelLineId>1</ns2:salesChannelLineId>
         <ns2:salesChannelLineNumber>1</ns2:salesChannelLineNumber>
         <ns2:partnerSKU>ALDO_5225_37_48R</ns2:partnerSKU>
         <ns2:barcode>ALDO_5225_37_48R</ns2:barcode>
         <ns2:salesChannelSKU>ALDO_5225_37_48R</ns2:salesChannelSKU>
         <ns2:quantity>1</ns2:quantity>
         <ns2:itemPrice>0.00</ns2:itemPrice>
         <ns2:lineId>54600</ns2:lineId>
         <ns2:itemId>211437</ns2:itemId>
         <ns2:itemName>Ferrecci Men\'s \'Zonettie\' 2-piece Navy Suit</ns2:itemName>
         <ns2:lineStatus>PROCESSING</ns2:lineStatus>
         <ns2:unitCost>0.00</ns2:unitCost>
         <ns2:unitCostCurrencyCode>USD</ns2:unitCostCurrencyCode>
      </ns2:processedSalesOrderLine>
   </ns2:processedSalesOrder>
</ns2:processedSalesOrderMessage>';



$xml = simplexml_load_string($resp)->children('ns2',true);
$json = json_encode($xml);
$result = json_decode($json);
echo '<pre>'; print_r($result); echo '<pre>';
die;

答案 1 :(得分:1)

试试这个,

$obj = simplexml_load_string($xml);

foreach($obj->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('urn:wsTest')->ServiceNameRQ->GetRatesRS->Rates->Rate as $rate)
{
echo (string)$rate->RateName . "\n";
}