title: How to pass single tag header in php soap client for wcf?
body: <p>I am trying to consume WCF service from PHP . I need to pass a header field in soap client of PHP in order to consume it. Soap header will be something like this:</p>
  
  <pre><code>&lt;header&gt;
  &lt;LisenseKey&gt;Lisense key goes here&lt;/LisenseKey&gt;
  &lt;/header&gt;
  </code></pre>
  
  <p>The namespace for LisenseKey element is "<a href="http://mylinsensekeynamespace" rel="nofollow noreferrer">http://mylinsensekeynamespace</a>".</p>
  
  <p>The method I want to consume in WCF is as follows:</p>
  
  <pre><code>public string function GetMessage(string name)
  {
      return "Hello , "+name;
  }
  </code></pre>
  
  <p>Before the service is configured to validate header, I was consuming the service from PHP as follows and it is working perfectly:</p>
  
  <pre><code>try{
      $client = new SoapClient("http://localhost:8181/?wsdl");
      $param = new stdClass();
      $param-&gt;name = "My Name";
      $webService = $client-&gt;GetMessage($param);
      print_r($webService);
  }
  catch(Exception $e)
  {
      echo $e-&gt;getMessage();  
  }
  </code></pre>
  
  <p>When after the service is configured to validate license key in header, I am trying to consume it like this and it is not working yet:</p>
  
  <pre><code>try{
      $client = new SoapClient("http://localhost:8181/?wsdl");
  
      $actionHeader = new SoapHeader("http://mycustomheader",'LisenseKey',"lisense key",true);
      $client-&gt;__setSoapHeaders($actionHeader);
      $param = new stdClass();
      $param-&gt;name = "My Name";
      $webService = $client-&gt;GetMessage($param);
      print_r($webService);
  }
  catch(Exception $e)
  {
      echo $e-&gt;getMessage();  
  }
  </code></pre>
  
  <p>I already tried in so many different ways from online articles. How can I consume it? WCF service is using BasicHttpBinding and SOAP version should be 1.1. How to pass the header information to consume the service?</p>
  
  <p>Following is the .NET WCF service code that validate for LicenseKey soap header for every request.</p>
  
  <pre><code>    public class MyServiceMessageInspector : System.ServiceModel.Dispatcher.IDispatchMessageInspector
      {
          public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel,
              System.ServiceModel.InstanceContext instanceContext)
          {
              if (request.Headers.FindHeader("LisenseKey", "") == -1)
              {
                  throw new FaultException("Lisense Key Was Not Provided");
              }
  
              var lisenseKey = request.Headers.GetHeader&lt;string&gt;("LisenseKey", "http://mycustomheader.com");
              if (string.IsNullOrEmpty(lisenseKey))
              {
                  throw new FaultException("Lisnse key should not be empty");
              }
  
              if (lisenseKey != "12345x")
              {
                  throw new FaultException("Lisense key is not valid");
              }
  
              return instanceContext;
          }
  
          public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
          {
  
          }
      }
  
      public class MyServiceMessageInspectorBehaviour : Attribute, System.ServiceModel.Description.IServiceBehavior
      {
          public void AddBindingParameters(System.ServiceModel.Description.ServiceDescription serviceDescription,
              System.ServiceModel.ServiceHostBase serviceHostBase,
              System.Collections.ObjectModel.Collection&lt;System.ServiceModel.Description.ServiceEndpoint&gt; endpoints,
              System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
          {
  
          }
  
          public void ApplyDispatchBehavior(System.ServiceModel.Description.ServiceDescription serviceDescription,
              System.ServiceModel.ServiceHostBase serviceHostBase)
          {
              foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
              {
                  foreach (var endpointDispatcher in channelDispatcher.Endpoints)
                  {
                      endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new MyServiceMessageInspector());
                  }
              }
          }
  
          public void Validate(System.ServiceModel.Description.ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
          {
  
          }
      }
  </code></pre>