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!!!
Thanks so much for this, for some reason I have just not been able to get my head around it but now I am good to go. Life saver.
Just to point out you use $ns but do not define it, could confuse some people that it needs to be the url of the service.
Thanks! Very useful!
Thanks! Very useful!
Hi, Thanks for your help.
How can I add function ?
If I have just one function in my principal index function, there is no problem but if I have more, soap return an error…
you can add your functions, what error you are getting
Thanks,Very good article, It was very helpful
thnx bro its really helpful
u have the code example for download?
nope, just follow the steps it will just take few minutes
PHP Fatal error: Cannot redeclare class Soapserver in /www/application/controllers/soapserver.php on line 34
How should I fix it?
i think you already have Soapserver class
Dude, that’s just great, Thanks for sharing that, That was really helpful
Hello Sir,
I want to consume web services of goglobal api but i m not be able to configure request format. Kindly help me.
Thank you so much
i have not worked on glglobal api, if i get the time will check and let you know.
Thanks! That’s really helpful.
But I can’t load a model inside the method web service.
I get this error message, when I load a model inside the method:
“Fatal error: Using $this when not in object context in [webaddress]\application\controllers\soapserver_controller.php on line xx”. How can I resolve this?
Thank you so much.
you need to create CI instance
$CI = & get_instance();
to use model functions, use$CI
instead of$this
How to call this in Java
I don’t have much idea regarding Java, but you can check online for Java SOAP client
Thanks pravin got it working
hey i am calling model functions from the registered functions but its not working
it should work, issue may something else, I have emailed you the script(without model)
The page you requested was not found.help me ?
i didn’t get you, which page?
its not working on server…please can u suggest me something on this
whats the issue? r u getting any errors? also check the error logs
Hey, very nice tutorial,
But I have some noobs problem here about calling that function from another controller. I’ve looking for solutions on internet neither of them are working in my case.
So I have created this inside index() function in my main controller
$this->load->library(“Nusoap_library”);
$param = array(“3”, “2”);
$webserviceUrl = base_url().’index.php/webservice?wsdl’;
$client = new nusoap_client($webserviceUrl, ‘wsdl’);
$err = $client->getError();
if($err) echo $err;
$response = $client->call(“soapAddNumber”, $param[0], $param[1]);
print_r($response);
it return blank page, how do I should call the soapAddNumber function in soapserver controller? What do I miss?
Thank you,
check my latest post http://www.php-guru.in/2015/soap-client-in-codeigniter-using-nusoap-php-toolkit/
somehow didnt work for me, getting the error Cannot redeclare class Soapserver
there may be one more class with name “Soapserver” in your application