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() {
parent::Controller();
$this->load->library("nuSoap_lib");
$this->nusoap_server = new soap_server();
$this->nusoap_server->configureWSDL("MemberWSDL", "urn:MemberWSDL");
$this->nusoap_server->wsdl->addComplexType(
"Member",
"complexType",
"array",
"",
"SOAP-ENC:Array",
array(
"id"=>array("name"=>"id", "type"=>"xsd:int"),
"first_name"=>array("name"=>"first_name", "type"=>"xsd:string"),
"surname"=>array("name"=>"surname", "type"=>"xsd:string")
)
);
$this->nusoap_server->register(
"getMember",
array(
"id" => "xsd:int",
),
array("return"=>"tns:Member"),
"urn:MemberWSDL",
"urn:MemberWSDL#getMember",
"rpc",
"encoded",
"Returns the information of a certain member"
);
}
function index() {
if($this->uri->segment(3) == "wsdl") {
$_SERVER['QUERY_STRING'] = "wsdl";
} else {
$_SERVER['QUERY_STRING'] = "";
}
$this->nusoap_server->service(file_get_contents("php://input"));
}
function get_member() {
function getMember($idMember) {
$CI =& get_instance();
$CI->load->model("Member");
$row = $CI->Member->getMember($idMember);
return $row;
}
$this->nusoap_server->service(file_get_contents("php://input"));
}
}

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

Leave a Reply

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