Using NuSOAP in CodeIgniter

Today I am going to talk about the inclusion of the PHP library NuSOAP in the development framework CodeIgniter, since a few days ago I saw the need for this integration. In this article I will focus on the integration of the library to consume Web services offered by an external site to us.

1. What is NuSOAP?

But first, let’s take a look what it is and what is used for the library NuSOAP. This library, which is very useful and widely used, is a toolkit to develop Web Services under the PHP language. It consists of a series of classes that will make us much easier to develop Web Services. Provides support for the development of clients (those consuming Web Services) and servers (those that provide the Web Services). NuSOAP is based on SOAP 1.1, WSDL 1.1 and HTTP 1.0/1.1. Although this is not the only one tool, as there are more tools of this type, this is arguably the best and therefore, the most commonly used in web development.

2. Integrating NuSOAP in CodeIgniter

Once we know what NuSOAP is, we move on to integrate it into our development framework. The first thing that we need to do is to download the library, unzip it and include it in the directory application/libraries of our CodeIgniter framework.With this, we have everything ready to move on to the next step:performing the integration.

The easiest way to integrate this library in CodeIgniter is by creating our own “pseudo” CodeIgniter’s library, which at the same time allows the use of the real NuSOAP library, that we have previously downloaded. I say “pseudo” because really the only thing that we do is a communication interface between the controllers of our application and the library NuSOAP itself. We move on to develop our own library CodeIgniter, for that, we have to create a new file called nuSoap_lib.php (or whatever you want to call it, the name depends only on your imagination) in the application/libraries, the same level as the directory that contains the NuSOAP library.

In the file nuSoap_lib.php we have to put the following code:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
      class nuSoap_lib{
          function Nusoap_lib(){
               require_once(str_replace("\","/",APPPATH).'libraries/NuSOAP/lib/nusoap'.EXT); //If we are executing this script on a Windows server
          }
      }
?>

With this we have created our “pseudo” NuSOAP library, from now on we can use it from our controllers. To do that, we have to introduce the following code in our controller to call the library NuSOAP:

class Client extends Controller {
    function __construct() {
        parent::Controller();
        $this->load->library("nuSoap_lib");
    }

    function index() {

        $this->nusoap_client = new nusoap_client("http://url.server");

        if($this->nusoap_client->fault)
        {
             $text = 'Error: '.$this->nusoap_client->fault;
        }
        else
        {
            if ($this->nusoap_client->getError())
            {
                 $text = 'Error: '.$this->nusoap_client->getError();
            }
            else
            {
                 $row = $this->nusoap_client->call(
                          'functionInServer',
                           array(params)
                        );
                ...
            }
     }
}

In this way, we can consume SOAP services from our application developed with CodeIgniter. As you can see, it is fairly simple and very comfortable. One more reason to continue using this simple and very powerful framework.

1 thought on “Using NuSOAP in CodeIgniter

Leave a Reply

Your email address will not be published. Required fields are marked *