Last updated on July 31st, 2016 at 11:13 am
In this post i explain you how to create SOAP web service in PHP. Most of trinity follower request for this post, so here it is :). In my last post i explain how to create REST service in php.
What is SOAP?
SOAP (Simple Object Access Protocol) is a messaging protocol that allows programs that run on disparate operating systems (such as Windows and Linux) to communicate using Hypertext Transfer Protocol (HTTP) and its Extensible Markup Language (XML).
Now before start creating SOAP service we need NUSOAP Library. You can easily download that from here.
Live Preview: http://trinitytuts.com/d/soap/index.php
Create Your First SOAP service
I share some basics of SOAP service step by step.
Step 1. Download NUSOAP and extract into your server root directory or any folder inside your server or local server.
Step 2. Create new php file name it whatever you like i name my file index.php and include nusoap.php.
/* Add nusoap library */ include_once('nusoap-0.9.5/lib/nusoap.php');
Step 3. Now create your first method which we are going to use later in SOAP service.
/* Create your functions */ function myFirstRequest($msg){ return $msg; }
Step 4. Now we create instance of soap_server because we are creating a server file which handle request and response to user.
/* Configure your soap server */ $server = new soap_server();
Step 5. Now we configure or soap service by calling a method configWSDL as shown bellow
$server->configureWSDL("index", "urn:index");
WSDL: Web Services Description Language (WSDL) is an XML-based language used to describe the services a business offers and to provide a way for individuals and other businesses to access those services electronically.
Step 6. Now this is important step where i register my created method which i create in beginning.
$namespace = "http://trinitytuts.com/d/soap/index.php"; /* Register your function */ $server->register("myFirstRequest", // Register Method array('msg' => 'xsd:string'), // Method Parameter array('return' => 'xsd:string'), // Response $namespace, // Namespace false, // soapaction: (use default) "rpc", // style: rpc or document "encoded", // use: encoded or literal "Some Description" // description: documentation for the method );
You can register any number of function you want according to your need.
Step 7. This is last step in this we can return data to user
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : ''; @$server->service($POST_DATA);
All done now we can check this in our browser open your page in your browser or you can go to this link to preview.
Now to send data to server i add tool or extension in my browser. I am using SOA Client for Mozilla for this.
Paste your URL: http://trinitytuts.com/d/soap/index.php?wsdl and click on Parse WSDL. Select function using radio button you also use radio button to select different program, add your data inside field and click on invoke, if your data submit successfully you get your response as shown bellow image
Complete code
<?php /* Add nusoap library */ include_once('nusoap-0.9.5/lib/nusoap.php'); $namespace = "http://trinitytuts.com/d/soap/index.php"; /* Create your functions */ function myFirstRequest($msg){ return $msg; } function multiParmFunction($value1, $value2){ /* Add your functionality here what ever you want */ return $value1+$value2; } /* Configure your soap server */ $server = new soap_server(); $server->configureWSDL("index", "urn:index"); /* Register your function */ $server->register("myFirstRequest", array('msg' => 'xsd:string'), array('return' => 'xsd:string'), $namespace, false, "rpc", "encoded", "Some Description" ); $server->register("multiParmFunction", array('value1' => 'xsd:string', 'value2' => 'xsd:string'), array('return' => 'xsd:string'), $namespace, false, "rpc", "encoded", "Some Description" ); $POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : ''; @$server->service($POST_DATA);
That’s it. Now you can create your own SOAP service in PHP, You can easily add your functionality inside function or create class and call its function etc.
Hope this help you understand and create web service in SOAP using PHP.
Thank you 🙂