• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SOAP Server in Codeigniter

I hear how doing SOAP in PHP is so hard and I also hear the doing SOAP in codeigniter is impossible. I am here to tell you that is not true. The problem is that it is not well documented. In addition PHP does not support auto WSDL generation, if you add ?wsdl to your SOAP endpoint you will get a message like "auto wsdl generation is not supported" NICE!

So what is SOAP, its many things but for the purposes of this conversation its helpful to think of SOAP as a way to expose methods of a class via a web service and have those methods be self defining. In order to accomplish this you need to document your class, instantiate a SOAP server (using PHP's soap class) and then add a 3rd party library to generate the WSDL.

I'll start with the last one first. This goes into the library folder:
http://pastebin.com/qmzZa3wd //sorry for the pastebin link

Next we need a method that will invoke and call this code:
public function wsdl()
        $wsdl = $this->load->library('wsdldocument','api');
        header('Content-Type: text/xml');
        echo $wsdl->saveXML();

So this is a hack, normally in SOAP you add ?wsdl to your endpoint to get the wsdl here you would do /controller/wsdl. I am sure you could write some code to mimic the normal behavior but so far this has worked for me. You also need to properly comment your methods for this to work (example later).

Next you will need to instantiate your soap server:
public function index() {
        ini_set('soap.wsdl_cache_limit', 0);
        ini_set('soap.wsdl_cache_ttl', 0);
        $base_url = base_url();
        $wsdl = $base_url.'/index.php/api/wsdl';
        $url['uri'] = $base_url.'/index.php/api'; //Take out the index.php if you are using rewrite to do ths same
        $server = new SOAPServer($wsdl, $url);

In the setClass method you specify the name of the controller that has your SOAP methods for me I went with the ever original 'api' name Smile

And thats pretty much it, here is an example of a method that is properly documented:

     * @param  string is something
     * @param  string is something
     * @param  string is something
     * @return boolean
    public function someMethod($var1, $var2, $var3)
        return true;

Also if you want to do authentication in your header it would look something like this:

     * @param  string with the properties username and password pass via header
     * @return boolean
    public function AuthHeader($Header)
        $this->authenticated = false;
        //You could get this from a database
        if($Header->username == 'user' && $Header->password == 'pass')
            $this->authenticated = true;
        return $this->authenticated;


Then you could make a function to check auth status:

protected function checkAuth()
            log_info('User not valid denying access');


[eluser]Agustín Villalba[/eluser]
You can also find information about SOAP servers in Codeigniter here:



You can refer this script

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.