title: Adding child node to XML variable - PHP
body: <p>I am trying to populate the values of a SOAP request that needs to be sent using php curl. I have setup a variable with the soap.xml request and have gotten all the data that's needed to be passed.</p>
  
  <pre><code>$shipBody = '&lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:v1="http://servicescom/ws/merchantAPI/v1.0"&gt;
        &lt;soap:Header&gt;
          &lt;wsse:Security xmlns:wsse="http://docs.oasis-1.xsd"&gt;
            &lt;wsse:UsernameToken&gt;
              &lt;wsse:Username&gt;'.$username.'&lt;/wsse:Username&gt;
              &lt;wsse:Password&gt;'.$password.'&lt;/wsse:Password&gt;
            &lt;/wsse:UsernameToken&gt;
          &lt;/wsse:Security&gt;
        &lt;/soap:Header&gt;
        &lt;soap:Body&gt;
          &lt;v1:parcelShipmentNotification&gt;
            &lt;parcel&gt;
              &lt;carrierName&gt;'.$carrierName.'&lt;/carrierName&gt;
              &lt;carrierService&gt;'.$carrierService.'&lt;/carrierService&gt;
              '.$xml_data-&gt;children('itemdatastring')-&gt;asXML().'&lt;!--Tried adding data as children but didn't return anything--&gt;
              '.$xml_data-&gt;asXML().'&lt;!--This worked but i couldn't remove the extra "itemdatastring"--&gt;
              &lt;orderId&gt;'.$exId.'&lt;/orderId&gt;
              &lt;parcelId&gt;'.$parcelId.'&lt;/parcelId&gt;
              &lt;parcelReference&gt;'.$shipDate.'&lt;/parcelReference&gt;
              &lt;shippingDate&gt;'.$shipDate.'&lt;/shippingDate&gt;
              &lt;trackingURL&gt;https://tools.usps.com/go/TrackConfirmAction?tLabels='.$trackingUrl.'&lt;/trackingURL&gt;
            &lt;/parcel&gt;
            &lt;requestPackingSlip&gt;true&lt;/requestPackingSlip&gt;
          &lt;/v1:parcelShipmentNotification&gt;
        &lt;/soap:Body&gt;
      &lt;/soap:Envelope&gt;';
  </code></pre>
  
  <p>I am able to create a function to get all line items with sku and qty but i am having difficulties passing the return value into the $shipBody variable. Here is a sample line items;</p>
  
  <pre><code>$myShipment = array ( 
  array(
      'sku' =&gt; 'BA2222222',
      'qty' =&gt; '2'
      ),
  array(
      'sku' =&gt; 'BA111111',
      'qty' =&gt; '4'
      ),
  );
  </code></pre>
  
  <p>Here is how i'm am currently getting the XML for line items.</p>
  
  <pre><code>function array_to_xml( $dataToConvert, $xml_data ) {
      foreach( $dataToConvert as $key =&gt; $value ) {
          if( is_numeric($key) ){
              $key = 'items'; //dealing with &lt;0/&gt;..&lt;n/&gt; issues
          }
          if( is_array($value) ) {
              $subnode = $xml_data-&gt;addChild($key);
              array_to_xml($value, $subnode);
          } else {
              $xml_data-&gt;addChild("$key",htmlspecialchars("$value"));
          }
       }
  }
  
  $orderLineItems = array();
  foreach ($myShipment as $orderItem) {
      $orderItemSku = $orderItem['sku'];
      $orderItemQty = $orderItem['qty'];
  
      $orderLineItems[] = array(
        'quantity' =&gt; $orderItemQty,
        'sku' =&gt; $orderItemSku
      );
  }
  
  $xml_data = new SimpleXMLElement('&lt;itemdatastring&gt;&lt;/itemdatastring&gt;');
  array_to_xml($orderLineItems,$xml_data);
  </code></pre>
  
  <p>And Finally, Here is what the finished file should look like;</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:v1="http://servicescom/ws/merchantAPI/v1.0"&gt;
    &lt;soap:Header&gt;
      &lt;wsse:Security xmlns:wsse="http://docs.oasis-1.xsd"&gt;
        &lt;wsse:UsernameToken&gt;
          &lt;wsse:Username&gt;'.$username.'&lt;/wsse:Username&gt;
          &lt;wsse:Password&gt;'.$password.'&lt;/wsse:Password&gt;
        &lt;/wsse:UsernameToken&gt;
      &lt;/wsse:Security&gt;
    &lt;/soap:Header&gt;
    &lt;soap:Body&gt;
      &lt;v1:parcelShipmentNotification&gt;
        &lt;parcel&gt;
          &lt;carrierName&gt;'.$carrierName.'&lt;/carrierName&gt;
          &lt;carrierService&gt;'.$carrierService.'&lt;/carrierService&gt;
          &lt;items&gt;
              &lt;quantity&gt;2&lt;/quantity&gt;
              &lt;sku&gt;BA2222222&lt;/sku&gt;
          &lt;/items&gt;
          &lt;items&gt;
              &lt;quantity&gt;4&lt;/quantity&gt;
              &lt;sku&gt;BA111111&lt;/sku&gt;
          &lt;/items&gt;
          &lt;orderId&gt;'.$exId.'&lt;/orderId&gt;
          &lt;parcelId&gt;'.$parcelId.'&lt;/parcelId&gt;
          &lt;parcelReference&gt;'.$shipDate.'&lt;/parcelReference&gt;
          &lt;shippingDate&gt;'.$shipDate.'&lt;/shippingDate&gt;
          &lt;trackingURL&gt;https://tools.usps.com/go/TrackConfirmAction?tLabels='.$trackingUrl.'&lt;/trackingURL&gt;
        &lt;/parcel&gt;
        &lt;requestPackingSlip&gt;true&lt;/requestPackingSlip&gt;
      &lt;/v1:parcelShipmentNotification&gt;
    &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>Any help/pointers will be really appreciated.
  Thank you</p>