将子节点添加到XML变量 - PHP

时间:2017-12-19 05:19:51

标签: php arrays xml simplexml

我正在尝试填充需要使用php curl发送的SOAP请求的值。我已经使用soap.xml请求设置了一个变量,并获得了所有需要传递的数据。

$shipBody = '<?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:v1="http://servicescom/ws/merchantAPI/v1.0">
      <soap:Header>
        <wsse:Security xmlns:wsse="http://docs.oasis-1.xsd">
          <wsse:UsernameToken>
            <wsse:Username>'.$username.'</wsse:Username>
            <wsse:Password>'.$password.'</wsse:Password>
          </wsse:UsernameToken>
        </wsse:Security>
      </soap:Header>
      <soap:Body>
        <v1:parcelShipmentNotification>
          <parcel>
            <carrierName>'.$carrierName.'</carrierName>
            <carrierService>'.$carrierService.'</carrierService>
            '.$xml_data->children('itemdatastring')->asXML().'<!--Tried adding data as children but didn't return anything-->
            '.$xml_data->asXML().'<!--This worked but i couldn't remove the extra "itemdatastring"-->
            <orderId>'.$exId.'</orderId>
            <parcelId>'.$parcelId.'</parcelId>
            <parcelReference>'.$shipDate.'</parcelReference>
            <shippingDate>'.$shipDate.'</shippingDate>
            <trackingURL>https://tools.usps.com/go/TrackConfirmAction?tLabels='.$trackingUrl.'</trackingURL>
          </parcel>
          <requestPackingSlip>true</requestPackingSlip>
        </v1:parcelShipmentNotification>
      </soap:Body>
    </soap:Envelope>';

我能够创建一个函数来获取所有含有sku和qty的行项目,但是我很难将返回值传递给$ shipBody变量。这是一个示例订单项;

$myShipment = array ( 
array(
    'sku' => 'BA2222222',
    'qty' => '2'
    ),
array(
    'sku' => 'BA111111',
    'qty' => '4'
    ),
);

以下是我目前如何获取订单项的XML。

function array_to_xml( $dataToConvert, $xml_data ) {
    foreach( $dataToConvert as $key => $value ) {
        if( is_numeric($key) ){
            $key = 'items'; //dealing with <0/>..<n/> issues
        }
        if( is_array($value) ) {
            $subnode = $xml_data->addChild($key);
            array_to_xml($value, $subnode);
        } else {
            $xml_data->addChild("$key",htmlspecialchars("$value"));
        }
     }
}

$orderLineItems = array();
foreach ($myShipment as $orderItem) {
    $orderItemSku = $orderItem['sku'];
    $orderItemQty = $orderItem['qty'];

    $orderLineItems[] = array(
      'quantity' => $orderItemQty,
      'sku' => $orderItemSku
    );
}

$xml_data = new SimpleXMLElement('<itemdatastring></itemdatastring>');
array_to_xml($orderLineItems,$xml_data);

最后,这是完成文件的样子;

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:v1="http://servicescom/ws/merchantAPI/v1.0">
  <soap:Header>
    <wsse:Security xmlns:wsse="http://docs.oasis-1.xsd">
      <wsse:UsernameToken>
        <wsse:Username>'.$username.'</wsse:Username>
        <wsse:Password>'.$password.'</wsse:Password>
      </wsse:UsernameToken>
    </wsse:Security>
  </soap:Header>
  <soap:Body>
    <v1:parcelShipmentNotification>
      <parcel>
        <carrierName>'.$carrierName.'</carrierName>
        <carrierService>'.$carrierService.'</carrierService>
        <items>
            <quantity>2</quantity>
            <sku>BA2222222</sku>
        </items>
        <items>
            <quantity>4</quantity>
            <sku>BA111111</sku>
        </items>
        <orderId>'.$exId.'</orderId>
        <parcelId>'.$parcelId.'</parcelId>
        <parcelReference>'.$shipDate.'</parcelReference>
        <shippingDate>'.$shipDate.'</shippingDate>
        <trackingURL>https://tools.usps.com/go/TrackConfirmAction?tLabels='.$trackingUrl.'</trackingURL>
      </parcel>
      <requestPackingSlip>true</requestPackingSlip>
    </v1:parcelShipmentNotification>
  </soap:Body>
</soap:Envelope>

任何帮助/指针都会非常感激。 谢谢

1 个答案:

答案 0 :(得分:0)

当您致电->children()时,您会收到一个节点列表,因此尝试拨打$xml_data->children('itemdatastring')->asXML()将会失败。

你可以做点像......

$itemString = '';
foreach ( $xml_data->children() as $item ) {
    $itemString .= $item->asXML();
}

将从每个单独的元素构建一个字符串,然后在XML中使用$itemString