How to create SOAP Web-service on PHP?

SOAP is a type of web-service protocol. SOAP is a messaging protocol for transporting information between server and client in XML format over HTTP. Remember that SOAP is the application layer (7th layer of the OSI model) level protocol. WSDL describes the method(s), transport details and message format of SOAP web-service. We can get the SOAP web-services description by adding the “?wsdl” at the end of the web-service URL.

SOAP Server-side

Download nusoap libraray
“NuSOAP is a rewrite of SOAPx4, provided by NuSphere and Dietrich Ayala. It is a set of PHP classes – no PHP extensions required – that allow developers to create and consume web services based on SOAP 1.1, WSDL 1.1 and HTTP 1.0/1.1.”

nusoap library having the soap_server class which is using to create the server object.

This returns the WSDL. Here ‘getmessage’ represents the service name and ‘urn’ to get the server URL. It must be specified without it register method doesn’t work.

Here get_message is a function name, 2nd parameter is for input, the 3rd one is for the output parameter. Above code register the method/function on the server.

In next step create the PHP class/function and give the same name as mention in registration.

But above message is not displayed without adding HTTP listener.

ERROR: Request header field SOAPAction is not allowed by Access-Control-Allow-Headers in preflight response.
This error occurs when server-side and client-side are not having same origin and header(“Access-Control-Allow-Headers:*”); is not added in php server file.

ERROR: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘null’ is therefore not allowed access.
This error occurs when server-side and client-side are not having same origin and header(“Access-Control-Allow-Origin:*”); is not added in php server file.

Full server-side code

SOAP Client-side

Now, we are going to create SOAP request for cross-origin. Just open an HTML file and follow the following steps.

We’ll use $.soap library.

$.soap is using $.ajax to call the SOAP web-service and xml2json.js plugin is using to parse the response.

Here is full code

Now, we are able to call the SOAP web-service and parse the result.

Here are my post about SOAP VS REST API.