Some web services
require header message to be passed along with the body of the soap request. This
is often needed for various reasons such as authentication, payment etc. In
order to consume such web services successfully, we must define and pass the
header element along with the body of the soap request message.
In this tutorial, we will demonstrates
how to define and add a SOAP header message to PHP SOAP Client request. You can
take a look at the web services we are trying to consume here. A sample request
for the method “boom” from the web service is shown below.
From the sample soap request, we noticed that the soap header element, “requestHeader”
has two inputs - username and password. We defined these inputs with an associative
array. Eg
Next, we used the PHP
SoapHeader class to create the soap header by passing it the namespace of the
web service (e.g “http://wsdl.example.org/”), the name of the soap header we
trying to create (e.g “requestHeader”) and the the soap header inputs defined
above (e.g $headerbody).
Finally, we called the __setSoapHeaders($header) method on the soap client object to set the soap header.
See the complete codes
below.
A sample request and response obtained from the above script.
No comments:
Post a Comment