Using NuSOAP Server & Client with CodeIgniter

Print pagePDF pageEmail page

One of the recent projects required integrating a 3rd party application. The backend was developed using CodeIgniter v3 running on PHP v5+. I emphasized on standards and chose SOAP as the integration technology. Although PHP v5 has native support for SOAP server and client, I wanted to prevent re-inventing the wheel. That’s when I bumped on to NuSOAP library, provided by NuSphere and Dietrich Ayala. There are two variations, old and new. I decided to go with the latest.

The web is full of “HowTo”s about integrating NuSOAP with CodeIgniter. Careful observation revealed that most of them are copied from one or two sources ! After reading few, I decided to make my hands dirty. This is my experience. Upon running the library as is, I encountered several errors. They are mostly to do with PHP v5.5 compatibility issues. Looks like the developers were too busy to upgrade it. I hope this post will be useful for someone trying to do a similar activity. You are free to use anything from this page, and don’t forget to post your experience in the comments section below.

The beginning

  1. Download NuSOAP new version and copy the /lib folder to codeigniter’s application\libraries folder (I used v1.114 2007/11/06 15:17:46)
  2. Create a custom library wrapper in the same application\libraries folder (make sure the filename is Nusoap_lib.php – CodeIgniter v3 requires file names to start with a capital letter)

Patches

These are the patches I made (my version available at the bottom of this page)

  1. Line 2673 : original : set_magic_quotes_runtime(0);
    replace with : ini_set(‘magic_quotes_runtime’, 0);
  2. Line 3314 : original : $data = split(‘;’, $cookie_str);
    replace with : $data = explode(‘;’, $cookie_str);
  3. Line 5328 : remove this line <br><br>
  4. After Line 5456 add this line: if (isset($attrs[‘operations’])) {
  5. After Line 5484 add this line: }
  6. After Line 7389 add this line: unset($this->wsdl);

Server

Although I wanted to use try-catch for error management I realized that I can’t use:

instead I used this:

Here’s the server controller, at: application\controllers\MySoapServer.php

Once the server is running you can test it by calling the controller class. Here’s the result.

Click on the wsdl link to see the auto-generated WSDL file; and click on the echoTest link to see the details of our first test function.

Client

Here’s the client controller, at: application\controllers\MySoapClient.php

Always use try-catch blocks to prevent bad error messages shown to users.

These are the function call prototypes (from nusoap.php)

Here’s the output from the browser:

 MySoapServer client

Testing

I used the SOAPui, to test the communication. There are many browser-plugins, but nothing can be compared to SOAPui’s functionality. It has test cases, asserts, authentication, headers etc…

First try with an input and then without. You should see soap_fault is generated.

What is NOT shown here

My intention is to show you the bare minimum to implement a successful SOAP activity. If you read the server code carefully, the action method lives inside the constructor. This is not good, you can easily move it out but that requires using $this-> to refer the instance. But it is NOT available inside the constructor, we need to use $CI = & get_instance();

I also have not used any authentication here, although you must at least use “HTTP Basic Auth”.

Links

Use following links to extend your journey.

Share

2 thoughts on “Using NuSOAP Server & Client with CodeIgniter

  1. Thanks man! Your code about the constructors helped me get nuSOAP going with PHP7. Great!

  2. Awesome! Pretty much all other code out there is copied from one source. Thank you for the clear breakdown.

Leave a Reply

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