Skip to content

Creating a SOAP server in CodeIgniter

Today I am going to talk about how to create a web services server with the library NuSOAP in CodeIgniter. This post is based on our previous post (Using NuSOAP in CodeIgniter), so I would recommend you to read that post before continuing with this one, since here we use the “pseudo” library nuSoap_lib that we have created earlier.

Creating our NuSOAP server

Our NuSOAP server will be a controller which does not need to have its own view file, since it will only be accessed to perform certain tasks (even interact with the models if it would be necessary) and return their results via web to its remote client, so at no time it will be necessary to show the data on the screen directly from the server.

Well, after clarifying this point, we have to create our server controller in CodeIgniter. For this, we have to create the file “nuSoapServer.php” into the folder “application / controller” in which we insert the following code:

class NuSoapServer extends Controller {
function __construct() {
$this->nusoap_server = new soap_server();
$this->nusoap_server->configureWSDL("MemberWSDL", "urn:MemberWSDL");
"id"=>array("name"=>"id", "type"=>"xsd:int"),
"first_name"=>array("name"=>"first_name", "type"=>"xsd:string"),
"surname"=>array("name"=>"surname", "type"=>"xsd:string")
"id" => "xsd:int",
"Returns the information of a certain member"
function index() {
if($this->uri->segment(3) == "wsdl") {
$_SERVER['QUERY_STRING'] = "wsdl";
} else {
function get_member() {
function getMember($idMember) {
$CI =& get_instance();
$row = $CI->Member->getMember($idMember);
return $row;

Ok, this is fine, but… what does it all mean? Well, I will explain step by step what is what in the previous code. In the function “$this-> nusoap_server-> wsdl-> addComplexType” we have several fields:

  1. Name. This is the name that we are giving to the type
  2. typeClass. It is the NuSOAP’s type which will make up our own type (the most used are complexType or simpleType)
  3. phpType. It is the PHP’s type that makes up the structure that our service is returning (currently you can use array or struct)
  4. composer. This field indicates how the sub-elements of our structure work, this means, all of them are optional items and are included in any order (value “all”), or all of them are optional but are included in the order set (value “sequence”), or only one of them is allowed each time (value “choice”).
  5. restrictionBase. This tells the system that our complex type is based on Array type defined in soap-encoding namespace.
  6. Finally, we define the fields that our complex type has and the type of each one of these fields.

Once we have defined the type that we will use in the “$this-> nusoap_server-> register”, we create the method that can be called from any external client. For that, we have to set:

  1. ย The name of the method
  2. The input parameters needed by our method
  3. The type of output parameters returned by our method
  4. Namespaces of the method
  5. Action to perform in the namespace (the same name of the method)
  6. Style
  7. Use
  8. Information about the method

With this, we have almost done our NuSOAP web services server. All we need now is a detail in the configuration. In the file “application / config / routes.php” we have to add the following path:

$route['nuSoapServer/getMember/wsdl'] = 'nuSoapServer/index/wsdl';

Since it is essential to establish the correct path to our NuSOAP service.

With this, we have created and ready to work our NuSOAP server and it is integrated into our development framework CodeIgniter.

12 thoughts on “Creating a SOAP server in CodeIgniter”

  1. hi bro,
    really nice blog but there is small problem in ereg function in lib/nusoap.php file.
    ereg not supporting in php 5.3+ version .
    instead of ereg use preg_match and eregi use preg_match_all ๐Ÿ™‚ ๐Ÿ™‚
    thanx a lot about your lib …

  2. Everything works but I have a question.
    Why getMember is defined inside get_member?
    Is it only to prevent it from being called in a browser via the url?

  3. hi i have seen your tutorial which is very productive for new comers, i have question regarding webservice security. can you let me know what steps follow to secure webservice. eg: password protection/encryption etc

        1. You can protect the folder by .htacces files

          Other way is to use basic http autentication:

          if(!$_SERVER[‘PHP_AUTH_USER’]) {
          header(‘WWW-Authenticate: Basic realm=”WebServer Auth”‘);
          header(‘HTTP/1.0 401 Unauthorized’);
          } else {
          // USE $_SERVER[‘PHP_AUTH_USER’] and $_SERVER[‘PHP_AUTH_PW’] to validate users

  4. Its not Working for me.

    While im in Codeigniter the return is:
    Response not of type text/xml: text/html; charset=UTF-8

    The same code out of codeigniter returns correctly.

    Any Ideas?


Leave a Reply

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