title: SOAP PHP : Generating a complex array to pass to web service
body: <p>2 distinct problems.</p>
  
  <p>I'm using the PHP SOAP class to pass some XML to a webservice. I can generate simple arrays just fine, and these work perfectly with the webservice. </p>
  
  <p>1.When I try to generate a complex (arrays within arrays) query I'm getting errors.</p>
  
  <p>2.Also I don't understand how to insert a parameter(???) into an XML tag.</p>
  
  <p>Below is a copy of the sample XML query from the webservice documentation</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;soap:Body&gt;
      &lt;InsertInvoice xmlns="KashFlow"&gt;
        &lt;UserName&gt;string&lt;/UserName&gt;
        &lt;Password&gt;string&lt;/Password&gt;
        &lt;Inv&gt;
          &lt;InvoiceDBID&gt;int&lt;/InvoiceDBID&gt;
          &lt;InvoiceNumber&gt;int&lt;/InvoiceNumber&gt;
          &lt;InvoiceDate&gt;dateTime&lt;/InvoiceDate&gt;
          &lt;DueDate&gt;dateTime&lt;/DueDate&gt;
          &lt;Customer&gt;string&lt;/Customer&gt;
          &lt;CustomerID&gt;int&lt;/CustomerID&gt;
          &lt;Paid&gt;int&lt;/Paid&gt;
          &lt;CustomerReference&gt;string&lt;/CustomerReference&gt;
          &lt;EstimateCategory&gt;string&lt;/EstimateCategory&gt;
          &lt;SuppressTotal&gt;int&lt;/SuppressTotal&gt;
          &lt;ProjectID&gt;int&lt;/ProjectID&gt;
          &lt;CurrencyCode&gt;string&lt;/CurrencyCode&gt;
          &lt;ExchangeRate&gt;decimal&lt;/ExchangeRate&gt;
          &lt;ReadableString&gt;string&lt;/ReadableString&gt;
          &lt;Lines&gt;
            &lt;anyType /&gt;
            &lt;anyType /&gt;
          &lt;/Lines&gt;
          &lt;NetAmount&gt;decimal&lt;/NetAmount&gt;
          &lt;VATAmount&gt;decimal&lt;/VATAmount&gt;
          &lt;AmountPaid&gt;decimal&lt;/AmountPaid&gt;
          &lt;CustomerName&gt;string&lt;/CustomerName&gt;
        &lt;/Inv&gt;
      &lt;/InsertInvoice&gt;
    &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>The problem is occuring in the <code>&lt;Lines&gt;&lt;/Lines&gt;</code> section of the code.</p>
  
  <p>With regards to the XML tag parameter... I need to produce this :</p>
  
  <pre><code>&lt;Lines&gt;
  &lt;anyType xsi:type="InvoiceLine"&gt;
   &lt;Quantity&gt;1&lt;/Quantity&gt;
   &lt;Description&gt;Description here&lt;/Description&gt;
   &lt;Rate&gt;99&lt;/Rate&gt;
   &lt;ChargeType&gt;0&lt;/ChargeType&gt;
   &lt;VatRate&gt;0&lt;/VatRate&gt;
   &lt;VatAmount&gt;0&lt;/VatAmount&gt;
   &lt;ProductID&gt;0&lt;/ProductID&gt;
   &lt;LineID&gt;0&lt;/LineID&gt;
   &lt;/anyType&gt;
  &lt;/Lines&gt;
  </code></pre>
  
  <p>The bit that I am having problems with is</p>
  
  <pre><code>&lt;anyType xsi:type="InvoiceLine"&gt;
  </code></pre>
  
  <p>namely inserting the xsi:type="InvoiceLine" .....</p>
  
  <p>Here's the php code I'm currently using.</p>
  
  <pre><code>    $invoice_array = array 
  (
  'InvoiceDBID' =&gt; "",
  'InvoiceNumber' =&gt; 1,
  'InvoiceDate' =&gt; date("c",strtotime($invoice_data['date_purchased'])),
  'DueDate' =&gt; date("c",strtotime($invoice_data['date_purchased'])),
  'Customer' =&gt; $invoice_data['customers_name'],
  'CustomerID' =&gt; $KFcust-&gt;CustomerID,
  'Paid' =&gt; 0,
  'CustomerReference' =&gt; "",
  'EstimateCategory' =&gt; "",
  'SuppressTotal' =&gt; 0,
  'ProjectID' =&gt; 0,
  'CurrencyCode' =&gt; 0,
  'ExchangeRate' =&gt; 1,
  'NetAmount'=&gt; 0,
  'ReadableString' =&gt;"this invoice created by osc for order ".$invoice_data['orders_id']);
  
  foreach($line_data as $line)
  {
  $line_array[] = array(
  'Quantity'=&gt;$line['products_quantity'],
  'Description'=&gt;$line['products_name'],
  'Rate'=&gt;$line['products_tax'],
  'ChargeType'=&gt;0,
  'VatAmount'=&gt;$totals_data['ot_tax']['value'],
  'VatRate'=&gt;$line['products_tax'],
  'Sort'=&gt;1,
  'ProductID'=&gt;$line['products_id']);
  }
  
  $invoice_array['Lines']=array('anyType'=&gt; array('_'=&gt;$line_array, 'xsi:type'=&gt;"InvoiceLine"));
  $invoice_array['Netamount']=$totals_data['ot_subtotal']['value']+$totals_data['ot_shipping']['value'];
  $invoice_array['VATAmount']=$totals_data['ot_tax']['value'];
  $invoice_array['AmountPaid']=$totals_data['ot_total']['value'];
  $invoice_array['CustomerName']=$invoice_data['customers_name'];
  
  $invID = $client-&gt;createInvoice($invoice_array);
  </code></pre>
  
  <p>Sorry this inquiry is a little muddled - but as you can see, both of these problems go hand in hand.</p>
  
  <p><em>*</em>*The actual error message received is </p>
  
  <pre><code>Unable to cast object of type 'System.Xml.XmlNode[]' to type 'KashFlow.InvoiceLine'.
  </code></pre>
  
  <p>But this is just an indication that I am submitting XML that is not in the expected format.</p>
  
  <p>Can anyone help please?</p>