Monday 22 August 2016

SOAP SERVER IN CODEIGNITER USING NUSOAP PHP TOOLKIT

To create SOAP server in CodeIgniter, it is better to use NuSOAP (SOAP Toolkit for PHP).
So in one of my requirement I tried to integrate NuSOAP in CodeIgniter and it worked for me, so thought of sharing it.
Download NuSOAP Toolkit from this URL: Download
Below are the steps to create SOAP server in CodeIgniter using NuSOAP PHP Toolkit.
Step 1: After downloading NuSOAP toolkit, copy “lib” and “nusoap” folder in “application/library/” folder.
Step 2: Create one library file say “nusoap_library.php” in “application/library” folder and copy and paste below given code in “application/library/nusoap_library.php” file. It just includes “nusoap.php” library file from “lib” folder.
    class Nusoap_library
    {
       function Nusoap_library()
       {
           require_once('lib/nusoap'.EXT);
       }
    }
Step 3: Create one controller in “application/controllers/” folder, give good name to the controller as it will be your webservice URL(wsdl). Assume “soapserver” as controller name and copy and paste constructor code in “soapserver” controller. In load NuSOAP library and create SOAP server object.
    class Soapserver extends CI_Controller
    {
         function Soapserver()
         {
              parent::__construct();
              $ns = 'http://'.$_SERVER['HTTP_HOST'].'/index.php/soapserver/';
              $this->load->library("Nusoap_library"); // load nusoap toolkit library in controller
              $this->nusoap_server = new soap_server(); // create soap server object
              $this->nusoap_server->configureWSDL("SOAP Server Using NuSOAP in CodeIgniter", $ns); // wsdl cinfiguration
              $this->nusoap_server->wsdl->schemaTargetNamespace = $ns; // server namespace
         }
    }
Step 4: Now define index() function in controller.
    function index()
    {
        $this->nusoap_server->service(file_get_contents("php://input")); // read raw data from request body
    }
Step 5:This is one of the important step where we define our actual web service methods. The web service methods are defined as nested function in index() function of controller. Consider, addition of two numbers “addnumbers” as example method. Now define “addnumbers” method in index() function as.
    function index()
    {
        function addnumbers($a,$b)
        {
            $c = $a + $b;
            return $c;
        }
        $this->nusoap_server->service(file_get_contents("php://input")); // read raw data from request body
    }
Step 6: Now we need to register this “addnumbers” method as web service method in constructor function of “soapserver” controller and which can be done by using following code.
    $input_array = array ('a' => "xsd:string", 'b' => "xsd:string"); // "addnumbers" method parameters
    $return_array = array ("return" => "xsd:string");
    $this->nusoap_server->register('addnumbers', $input_array, $return_array, "urn:SOAPServerWSDL", "urn:".$ns."/addnumbers", "rpc", "encoded", "Addition Of Two Numbers");
Above code will register “addnumbers” method in “soapserver” web service.
Now to check whether this web service is created or not, just browse the web service URL and which will be as.
Web Service URL: http://www.example.com/index.php/soapserver
WSDL URL: http://www.example.com/index.php/soapserver?wsdl
If you want to set any credential to use the web service, we can set using PHP_AUTH_USER and PHP_AUTH_PASS, you can use below given function to set credentials. Copy this function in our “soapserver” controller.
 function checkcredential()
 {
  $username = $_SERVER['PHP_AUTH_USER'];
  $password = $_SERVER['PHP_AUTH_PW'];
  if ($username == 'test' && $password == 'test')
  {
   return TRUE;
  }
  else
  {
   return FALSE;
  }
 }
So you can use PHP_AUTH_USER and PHP_AUTH_PASS variables and can be checked with any dynamic or static values. It can be stored in database as well. Here I have used static username and password as “test”. So whenever any request comes to this web service, the requester must use “test” as username and password as credentials.
Now above credential function can be used in all web service methods for authentication purpose. If you want to use credential function in “addnumbers” method, you can use it like this.
 function addnumbers($a,$b)
 {
  if (checkcredential() == FALSE)
  {
   return "Invalid Authentication";
  }
  else
  {
   $c = $a + $b;
   return $c;
  }
 }
The downloaded NuSOAP toolkit have number of example codes of SOAP client. Use one of the example code whichever fits your requirements to check SOAP server created in CodeIgniter from above post.
Hope this post find helpful!!!

No comments:

Post a Comment

Installation of Drop Box API on Codigniter

As with the YouTube API the first step in getting this sucker setup is getting a developer key by visiting  https://www.dropbox.com/develop...