title: PHP/SOAP: How can UserCredentials be passed to SOAP request?
body: <pre><code>// Based on http://stackoverflow.com/questions/3934639/soap-authentication-with-php
  include 'auth.php';
  $soapURL = "http://traindata.njtransit.com:8090/NJTTrainData.asmx?WSDL" ;
  $soapParameters = array('UserCredentials'=&gt;array('userName' =&gt; $username, 'password' =&gt; $password));
  
  $soapFunction = "getTrainScheduleJSON" ;
  $soapFunctionParameters = Array('station' =&gt; "NY") ;
  
  $soapClient = new SoapClient($soapURL, $soapParameters);
  
  $soapResult = $soapClient-&gt;__soapCall($soapFunction, $soapFunctionParameters);
  </code></pre>
  
  <p>Here is the output (below). It complains about Missing required header 'UserCredentials', but I have UserCredentials included. Or am I missing something? Thanks!</p>
  
  <pre><code>PHP Fatal error:  Uncaught SoapFault exception: [soap:MustUnderstand] System.Web.Services.Protocols.SoapHeaderException: Missing required header 'UserCredentials'.
     at System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()
     at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest() in /home/www/html/njtransit/wed.php:13
  Stack trace:
  #0 /home/www/html/njtransit/wed.php(13): SoapClient-&gt;__soapCall('getTrainSchedul...', Array)
  #1 {main}
    thrown in /home/www/html/njtransit/wed.php on line 13
  </code></pre>
  
  <p>Here is another attempt adding SoapHeader before the __setSoapHeaders. I also added namespace ($ns):</p>
  
  <pre><code>include 'auth.php';
  $soapURL = "http://traindata.njtransit.com:8090/NJTTrainData.asmx?WSDL" ;
  $ns = 'http://microsoft.com/webservices/'; //Namespace of the WS.
  $soapParameters = array('UserCredentials'=&gt;array('userName' =&gt; $username, 'password' =&gt; $password));
  
  $soapFunction = "getTrainScheduleJSON" ;
  $soapFunctionParameters = Array('station' =&gt; "NY") ;
  
  // $soapClient = new SoapClient($soapURL, $soapParameters);
  $soapClient = new SoapClient($soapURL);
  
  $header = new SoapHeader($ns,'UserCredentials',$soapParameters,false);
  
  $soapClient-&gt;__setSoapHeaders($header);
  var_dump($soapClient);
  $soapResult = $soapClient-&gt;__soapCall($soapFunction, $soapFunctionParameters);
  </code></pre>
  
  <p>Here is the output and the new error message is, "Object reference not set to an instance of an object."</p>
  
  <pre><code>object(SoapClient)#1 (3) {
    ["_soap_version"]=&gt;
    int(1)
    ["sdl"]=&gt;
    resource(5) of type (Unknown)
    ["__default_headers"]=&gt;
    array(1) {
      [0]=&gt;
      object(SoapHeader)#2 (4) {
        ["namespace"]=&gt;
        string(33) "http://microsoft.com/webservices/"
        ["name"]=&gt;
        string(15) "UserCredentials"
        ["data"]=&gt;
        array(1) {
          ["UserCredentials"]=&gt;
          array(2) {
            ["userName"]=&gt;
            string(10) "myusername"
            ["password"]=&gt;
            string(17) "mypassword"
          }
        }
        ["mustUnderstand"]=&gt;
        bool(false)
      }
    }
  }
  PHP Fatal error:  Uncaught SoapFault exception: [soap:Server] System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&gt; System.NullReferenceException: Object reference not set to an instance of an object.
     at DepartureVisionServiceXMLExternal.ServiceExternal.Login(String username, String password) in C:\Users\vcaicxz\Desktop\CRYSTAL\PentaDepartureVisionServiceXMLExternaL\NJTTrainData.asmx.cs:line 55
     at DepartureVisionServiceXMLExternal.ServiceExternal.getTrainScheduleJSON(String station) in C:\Users\vcaicxz\Desktop\CRYSTAL\PentaDepartureVisionServiceXMLExternaL\NJTTrainData.asmx.cs:line 141
     --- End of inner exception stack trace --- in /home/www/html/njtransit/wed2.php:19
  Stack trace:
  #0 /home/www/html/njtransit/wed2.php(19): SoapClient-&gt;__soapCall('getTrainSchedul...', Array)
  #1 {main}
    thrown in /home/www/html/njtransit/wed2.php on line 19
  </code></pre>