Showing posts with label SoapClient. Show all posts
Showing posts with label SoapClient. Show all posts

Monday, May 26, 2014

How To Add Header Message To PHP SOAP Client Request

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.

Request:


Response:


Friday, March 21, 2014

A Complete PHP SOAP Client Example

This is a complete PHP SOAP Client example where we consumed the web services we developed here. You can also take a look at how we generated the WSDL file for the web service.



The code below started by disabling WSDL cache and defined the wsdl location. We also specify the soap server location. This parameter is optional if you define the soap server location in the wsdl file. We then, instantiate the PHP SOAP Client object by passing it the relevant parameters. We call the boom() method from the soap server class with two parameters; first->"PHP SOAP", and last->"Tutorial". The WSDL Tutorial, explained how we created these parameters. We accessed the value of the returned complex type, boomResponse, by calling the variable, "return". Note that "return" is a parameter of type, boomResponse. Check the server example for more information. Optionally, you can call __getLastRequest() and __getLastResponse() on the soap client object to see the last SOAP request and response respectively.











Your comments and suggestions are welcome
Related Posts Plugin for WordPress, Blogger...