title: PHP SoapClient: How to prefix SOAP parameter tag name with namespace?
body: <p>I'm using PHP's <code>SoapClient</code> to consume a SOAP service but am receiving an error that the SOAP service cannot see my parameters.</p>
  
  <pre><code>&lt;tns:GenericSearchResponse xmlns:tns="http://.../1.0"&gt;
    &lt;tns:Status&gt;
      &lt;tns:StatusCode&gt;1&lt;/tns:StatusCode&gt;
      &lt;tns:StatusMessage&gt;Invalid calling system&lt;/tns:StatusMessage&gt;
    &lt;/tns:Status&gt;
  &lt;/tns:GenericSearchResponse&gt;
  </code></pre>
  
  <p>The XML PHP's SoapClient sends for the SOAP call:</p>
  
  <pre><code>&lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
      xmlns:ns1="http://.../1.0"&gt;
    &lt;SOAP-ENV:Body&gt;
      &lt;ns1:GenericSearchRequest&gt;
        &lt;UniqueIdentifier&gt;12345678&lt;/UniqueIdentifier&gt;
        &lt;CallingSystem&gt;WEB&lt;/CallingSystem&gt;
      &lt;/ns1:GenericSearchRequest&gt;
    &lt;/SOAP-ENV:Body&gt;
  &lt;/SOAP-ENV:Envelope&gt;
  </code></pre>
  
  <p>I used soap-ui initially, that works successfully when consuming the same WSDL. The XML soap-ui sends for the call:</p>
  
  <pre><code>&lt;SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
      xmlns:ns1="http://.../1.0"&gt;
    &lt;SOAP-ENV:Body&gt;
      &lt;ns1:GenericSearchRequest&gt;
        &lt;ns1:UniqueIdentifier&gt;12345678&lt;/ns1:UniqueIdentifier&gt;
        &lt;ns1:CallingSystem&gt;WEB&lt;/ns1:CallingSystem&gt;
      &lt;/ns1:GenericSearchRequest&gt;
    &lt;/SOAP-ENV:Body&gt;
  &lt;/SOAP-ENV:Envelope&gt;
  </code></pre>
  
  <p>The difference being the <code>UniqueIdentifier</code> and <code>CallingSystem</code> parameters are prefixed with <code>ns1</code> in the soap-ui request.</p>
  
  <p>I've tried using passing <code>SoapVar</code> objects to the <code>SoapClient</code> call but this does not augment the parameter tags and prefix them with <code>ns1</code>.</p>
  
  <p>I know that <code>WEB</code> is a valid <code>CallingSystem</code> value as the XSD specifies it, and it works when using soap-ui.</p>
  
  <p>My current <code>SoapClient</code> code:</p>
  
  <pre><code>try {
    $client = new SoapClient($wsdl, array('trace' =&gt; 1));
    $query = new stdClass;
    $query-&gt;UniqueIdentifier = $id;
    $query-&gt;CallingSystem = 'WEB';
    $response = $client-&gt;GenericUniqueIdentifierSearch($query);
  } catch (SoapFault $ex) {
    $this-&gt;view-&gt;error = $ex-&gt;getMessage();
    ...
  }
  </code></pre>
  
  <p>I found <a href="http://eosrei.net/articles/2012/01/php-soap-xml-attributes-namespaces-xmlwriter" rel="nofollow noreferrer">this blog post</a> but I was hoping there might be a cleaner implementation.</p>
  
  <p><strong>Update:</strong>
  Used a <a href="https://stackoverflow.com/a/10932943/122075">solution from this question</a> but is pretty clunky:</p>
  
  <pre><code>$xml = "&lt;ns1:GenericSearchRequest&gt;"
       . "&lt;ns1:UniqueIdentifier&gt;$id&lt;/ns1:UniqueIdentifier&gt;"
       . "&lt;ns1:CallingSystem&gt;WEB&lt;/ns1:CallingSystem&gt;"
       . "&lt;/ns1:GenericSearchRequest&gt;";
  $query = new SoapVar($xml, XSD_ANYXML);
  
  $response = $this-&gt;client-&gt;__SoapCall(
      'GenericUniqueIdentifierSearch',
      array($query)
  );
  </code></pre>