The main advantage of using NuSOAP library is it allow developers to create and consume web services based on SOAP without any SOAP related PHP extensions.
If we were not able to predict production server system configuration(Like PHP version) then using of NuSOAP based web service is better.
It’s very easy to create and test webservice using NuSOAP library.
First we created the NuSOAP server which process web service request
Note : The “<webservice_url>” is absolute path of your webservice script(nusoap_api.php).
Ex. <webservice_url> = http://www.ilovephp.net/nusoap_api.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
<?php require_once("lib/nusoap.php"); // WSDL configuration $server = new soap_server(); $server->configureWSDL('SOAP webservice API', "<webservice_url>"); // SOAP Webservice Url ex. http://www.domain_name.com/nusoap_api.php $server->wsdl->schemaTargetNamespace = "<webservice_url>"; // SOAP Webservice Url // eof WSDL configuration // Web service methode initialization $server->register('add_user', array('json'=>'xsd:string'),array('return' => 'xsd:string'),$ns); /** * Add user in database * @param string * @return string */ function add_user($json) { $result = array(); $errors = array(); if (validate_user()) { // Input params $data = convert_json_to_array($json); // Webservice bussness logic here $result = array('message' => "Record added successfully"); } else { $errors[] = 'User authentication failed!'; } $result_array = array(); $result_array['error'] = $errors; $result_array['result'] = $result; return json_encode($result_array); } /** * User request validation */ function validate_user() { if($_SERVER['PHP_AUTH_USER'] == "WS_USERNAME" && $_SERVER['PHP_AUTH_PW'] == "WS_PASSWORD") // Web service username and password validation { return true; } else { return false; } } /** * Convert json to array */ function convert_json_to_array($json) { return json_decode($json,true); } if (!isset($HTTP_RAW_POST_DATA)) $HTTP_RAW_POST_DATA = file_get_contents( 'php://input' ); $server->service($HTTP_RAW_POST_DATA); ?> |
To consume and test above webservice we use below webservice client script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<?php require_once('lib/nusoap.php'); $wsdl = "<webservice_url>?wsdl"; // SOAP Webservice Url ex. http://www.domain_name.com/nusoap_api.php?wsdl $client = new nusoap_client($wsdl, 'wsdl'); // Input params if (isset($_POST['submit'])) { $username = $_POST['username']; $password = $_POST['password']; $json = $_POST['json']; } else { $username = "WS_USERNAME"; $password = "WS_PASSWORD"; $json = '{"name":"Ajay","dept":"sales","address":"Nashik","birthdate":"24/04/1991"}'; } // EOF Input params $client->setCredentials($username, $password); // Webservice credential $error = $client->getError(); if ($error) { echo $error; die(); } $action = "add_user"; // Webservice methode name $result = array(); if (isset($action)) { $data["json"] = $json; $result['response'] = $client->call($action, $data); } // Display webservice details echo "<h1>SOAP Web Service Client</h1>"; echo "<h3>Web service URL : ".str_replace("?wsdl","",$wsdl)."</h3>"; echo "<h3>Operation Name : </h3>".$action; echo "<form action=\"\" method=\"post\" >"; echo "<h3>Webservice Credentials : </h3>"; echo "Username : "; echo "<input type=\"text\" style=\"width:200px;\" name=\"username\" value=\"WS_USERNAME\">"; echo "<br>"; echo "Password : "; echo "<input type=\"text\" style=\"width:200px;\" name=\"password\" value=\"WS_PASSWORD\">"; echo "<h3>Input Parameter : </h3>"; echo "<table> <tr><td>json = </td><td><textarea name=\"json\" rows='6' cols='100'>".$json."</textarea></td></tr></table>"; echo "<input type=\"submit\" name=\"submit\" value=\"Invoke\">"; echo "<br>"; echo "<h3>Output : </h3>"; echo $result['response']; echo "<br><br>"; echo "<form>"; echo "-------------------------------------------------------------------------"; echo "-------------------------------------------------------------------------"; echo "<h2>Request</h2>"; echo "<pre>" . htmlspecialchars($client->request, ENT_QUOTES) . "</pre>"; echo "<h2>Response</h2>"; echo "<pre>" . htmlspecialchars($client->response, ENT_QUOTES) . "</pre>"; ?> |
That’s it about NuSOAP webservice.
Happy Coding 🙂