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

#1
[eluser]SkiOne[/eluser]
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:
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:
Code:
public function index() {
        $this->load->helper('url');
        
        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);
      
        $server->setClass('api');
        $server->handle();
    }

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:

Code:
/**
     * @param  string is something
     * @param  string is something
     * @param  string is something
     * @return boolean
     */
    public function someMethod($var1, $var2, $var3)
    {
        $this->checkAuth();
        //dostuff
        
        return true;
    }


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

Code:
/**
     * @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:

Code:
protected function checkAuth()
    {
        if(!$this->authenticated){
            log_info('User not valid denying access');
            $this->output->set_status_header('403');
            die();
        }

    }

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

http://phpsblog.agustinvillalba.com/crea...odeigniter

Cheers,

#3
[eluser]PravinS[/eluser]
You can refer this script
Quote:http://www.php-guru.in/2013/soap-server-...p-library/


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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