CodeIgniter Forums

Full Version: Nusoap in Code Igniter 1.5.4
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]Leonardo Radoiu[/eluser]
Hello!

After some hard time trying to figure out how I can implement nusoap library in 1.5.4 version I came up with a solution I want to share with all of you who came across the very same problem.

First, the nusoap library file that you must put in the system/libraries folder (we assume we have already there the nusoap base class named nusoap.php). I named this Nusoap_lib.php:

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Nusoap_lib
{
   function Nusoap_lib()
   {
       require_once(BASEPATH.'libraries/nusoap'.EXT);
   }
}
?>

Let's say we have a nusoap webservice named MemberWSVC saved as a controller file named MemberWSVC.php:

Code:
<?php
class MemberWSVC 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(
            "MemberRecordset",
            "complexType",
            "array",
            "",
            "SOAP-ENC:Array",
            array(
                "id"=>array("name"=>"id", "type"=>"xsd:int"),
                "firstname"=>array("name"=>"firstname", "type"=>"xsd:string"),
                "lastname"=>array("name"=>"lastname", "type"=>"xsd:string")
            )
        );    
        
        $this->nusoap_server->register(
            "selectMemberInfo",
            array(
                "id" => "xsd:int",
            ),
            array("return"=>"tns:MemberRecordset"),
            "urn:MemberWSDL",
            "urn:MemberWSDL#selectMemberInfo",
            "rpc",
            "encoded",
            "Get member's info"
        );
    }
    
    function index() {
        // this just expose webservice's methods. if you put this in every method of the webservice to describe it you won't get it to work because of some post/get issues i guess


        // this is a workaround for not having get params enabled in ci
        // usually you get the nusoap webservice's wsdl by appending "?wsdl" at the end of its url

        if($this->uri->segment(3) == "wsdl") {
            $_SERVER['QUERY_STRING'] = "wsdl";
        } else {
            $_SERVER['QUERY_STRING'] = "";
        }
        
        $this->nusoap_server->service(file_get_contents("php://input"));
    }
    
    function select_member_info() {
        function selectMemberInfo($member_id) {
            $CI =& get_instance();
            $CI->load->model("Member");
            
            $CI->Member->_id = $member_id;
                        
            $row = $CI->Member->selectMemberInfo(); // the method we use to retrieve member's info as array
    
            return $row;
        }
        
        $this->nusoap_server->service(file_get_contents("php://input"));
    }
}
?>

Finally, we consume the webservice with a controller named Client.php:

Code:
<?php
class Client extends Controller {
    function __construct() {
        parent::Controller();

        $this->load->library("Nusoap_lib");
    }

    function index() {
        $id = $this->uri->segment(3);

        $this->nusoap_client = new soapclient($this->config->item('base_url')."/webservice.php/MemberWSVC/select_member_info/wsdl");
        
        if($this->nusoap_client->fault)
        {
            $text = 'Error: '.$this->nusoap_client->fault;
        }
        else
        {
            if ($this->nusoap_client->getError())
            {
                $text = 'Error: '.$this->nusoap_client->getError();
            }
            else
            {
                $row = $this->nusoap_client->call(
                    'selectMemberInfo',
                    array($id),
                    'urn:MemberWSDL',
                    'urn:MemberWSDL#selectMemberInfo'
                );
                
                if(count($row) > 0 && is_array($row)) {
                    $text = "The member's name is ".$row['firstname']." ".$row['lastname'];
                } else {
                    $text = "There is no such member with ID ".$id;
                }
            }
        }

        etc.
    }
?>

Let's say this client will be accesible at http://localhost/webservice.php/client/12345

That's it! I hope it works for you.

El Forum

[eluser]Avatar[/eluser]
Having some trouble, It's not running the function selectMemberInfo($member_id) within select_member_info function. I know this because I put echo 'here';die; statement. Below never shows here when access WS directly, hence it doesn't even get to load the model:

Code:
function select_member_info() {
    
        function selectMemberInfo($member_id) {
            $CI =& get_instance();
echo 'here';die;
            $CI->load->model("Member");
            
            $CI->Member->_id = $member_id;
                        
            $row = $CI->Member->selectMemberInfo(); // the method we use to retrieve member's info as array
    
            return $row;
        }
        
        $this->nusoap_server->service(file_get_contents("php://input"));
    }

but this does

Code:
function select_member_info() {
echo 'here';die;
        function selectMemberInfo($member_id) {
            $CI =& get_instance();
            
            $CI->load->model("Member");
            
            $CI->Member->_id = $member_id;
                        
            $row = $CI->Member->selectMemberInfo(); // the method we use to retrieve member's info as array
    
            return $row;
        }
        
        $this->nusoap_server->service(file_get_contents("php://input"));
    }

not sure why. Any help would be greatly appreciated. Thanks in advance. This is good stuff by the way. Thanks.

El Forum

[eluser]Leonardo Radoiu[/eluser]
Hi there!

That first "echo" will never show up anything because that "the inside" of the selectMemberInfo() function is never accessible by WS directly (like http://localhost/MemberWSVC or http://localhost/MemberWSVC/index/wsdl) but when you consume the WS with a client.
For instance, if you write a nusoap client script like this:

Code:
class Client extends Controller {

    function __construct() {
        parent::Controller();
    }

    function index() {
        $this->load->library('Nusoap_lib');
    
        $this->soapclient = new nusoapclient('http://localhost/MemberWSVC/select_member_info/wsdl');
                
        if(!$this->soapclient->fault)
        {
            if(!$this->soapclient->getError())
            {
                $member = $this->soapclient->call(
                    'selectMemberInfo', // <- just when we get here the function becomes visible and can be called
                    array(1),
                    'urn:MemberWSVC',
                    'urn:MemberWSVC#selectMemberInfo'
                );

                print_r($member); // see what is coming out of the WS
            }
        }
    }
}

And we access this controller, let's say, at http://localhost/client

If something happens and the webservice doesn't return a thing and you don't know what is going on out there your can debug the nusoap client because all the error will be printed clearly in the [response] key of the object:

Code:
print_r($this->soapclient);

*** What I forgot to mention earlier in my presentation *** is that you have to insert a route in your routes.php file in order to access the proper path of the wsdl:

$route['MemberWSVC/select_member_info/wsdl'] = "MemberWSVC/index/wsdl";

All you have to keep in you mind (and that is what makes the trouble with Nusoap) is that the proper output of the WSDL and the proper path of the WSDL keeps your nusoap client working.
That is, if you have an error along the way in your includes file (a white space or a debug output or an error of some kind) crashes the WSDL output.

For any other questions on this subject I will answer you gladly because I work intensely with Nusoap in CI.

El Forum

[eluser]Avatar[/eluser]
SWEET, it works. Thank you so much. This is a great code for starters in nusoap. Smile

El Forum

[eluser]Wades[/eluser]
Thanks Leonardo Radoiu
but I have some question about your script here, could you explain it when you have timeļ¼Ÿ
following are my test code according to your's

server.php
Code:
&lt;?php
class Server extends Controller {

    function Server()
    {
        parent::Controller();    
                
        $this->load->library("Nusoaplib");
        
        $this->nusoap_server = new soap_server();
        $this->nusoap_server->configureWSDL("Server", "urn:Server");
        
        $this->nusoap_server->wsdl->addComplexType(
            "MemberRecordset",
            "complexType",
            "array",
            "",
            "SOAP-ENC:Array",
            array(
                "id"=>array("name"=>"id", "type"=>"xsd:int"),
                "firstname"=>array("name"=>"firstname", "type"=>"xsd:string"),
                "lastname"=>array("name"=>"lastname", "type"=>"xsd:string")
            )
        );    
        
        $this->nusoap_server->register(
            "test",
            array(
                "id" => "xsd:int",
            ),
            array("return"=>"tns:MemberRecordset"),
            "urn:Server",
            "urn:Server#test",
            "rpc",
            "encoded",
            "Get member's info"
        );
    }
    
    function server_test()
    {
        function test($member_id)
        {
            $row['id'] = '1';
            $row['firstname'] = 'fn';
            $row['lastname'] = 'ln';
            return $row;
        }
        
        $this->nusoap_server->service(file_get_contents("php://input"));
    }
}
?&gt;

client.php
Code:
&lt;?php
class Client extends Controller {

    function Client()
    {
        parent::Controller();    
        $this->load->library("Nusoaplib");
    }
    
    function index()
    {
        $id = $this->uri->segment(3);

        $this->nusoap_client = new nusoap_client(base_url()."/server/server_test");
        
        if($this->nusoap_client->fault)
        {
            $text = 'Error: '.$this->nusoap_client->fault;
        }
        else
        {
            if ($this->nusoap_client->getError())
            {
                $text = 'Error: '.$this->nusoap_client->getError();
            }
            else
            {
                $row = $this->nusoap_client->call(
                    'test',
                    array($id)
                );
                
                if(count($row) > 0 && is_array($row)) {
                    $text = "The member's name is ".$row['firstname']." ".$row['lastname'];
                } else {
                    $text = "There is no such member with ID ".$id;
                }
            }
        }
        
        echo $text;
    }
}
?&gt;

the first question is that the return value in the client,
as you can see, it should return
Code:
$row['id'] = '1';
$row['firstname'] = 'fn';
$row['lastname'] = 'ln';
however, in the client class, i get this error
Code:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: firstname
Filename: controllers/client.php
when I
Code:
var_dump($row)
, the value are
Code:
array(3) { [0]=> string(1) "1" [1]=> string(2) "fn" [2]=> string(2) "ln" }
so I hope you can explain the code and how could I make it correct so that
Code:
$row['firstname']
can work in the client class
Code:
$this->nusoap_server->wsdl->addComplexType(
            "MemberRecordset",
            "complexType",
            "array",
            "",
            "SOAP-ENC:Array",
            array(
                "id"=>array("name"=>"id", "type"=>"xsd:int"),
                "firstname"=>array("name"=>"firstname", "type"=>"xsd:string"),
                "lastname"=>array("name"=>"lastname", "type"=>"xsd:string")
            )
        );

the second question I have is that when I change
Code:
$this->nusoap_server->configureWSDL("Server", "urn:Server");
to
Code:
$this->nusoap_server->configureWSDL("ss", "urn:ss");
it still work, no error, so I'm curiously about this

Greatly appreciated!!!

El Forum

[eluser]bigg-media[/eluser]
NuSoap Library for CI
http://codeigniter.com/wiki/CI_Nusoap_Library

El Forum

[eluser]esra[/eluser]
[quote author="bigg-media" date="1196985590"]NuSoap Library for CI
http://codeigniter.com/wiki/CI_Nusoap_Library[/quote]

A couple of recommendations if you don't mind. Start a new thread in the Ignited Code forum and add a line to your wiki article pointing them to that thread. That way users have a place to post questions and you get much better recognition for your work. In your new thread, point them to the wiki article's url.

You might also want to post your code on the wiki between opening and closing code block tags, so the file can be copied in the exact format you wrote it in. There is an article on the wiki with an explanation of the codes.

I have not had a chance to try this yet, but certainly will this weekend. Thanks for your contribution.

El Forum

[eluser]herrucules[/eluser]
Hi, i'm new to this nusoap, so please be patient Smile

i've tried to use the code above for serve n fetch the webservice.. but it came out as an error like this

Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing Schema: unexpected <element> in restriction in D:\ci\apps\system\application\controllers\client.php:12 Stack trace: #0 D:\ci\apps\system\application\controllers\client.php(12): SoapClient->SoapClient('http://heppy/ci...') #1 [internal function]: Client->index('12') #2 D:\ci\apps\system\codeigniter\CodeIgniter.php(224): call_user_func_array(Array, Array) #3 D:\ci\apps\index.php(115): require_once('D:\ci\apps\syst...') #4 {main} thrown in D:\ci\apps\system\application\controllers\client.php on line 12


first thing i tried is create MemberWSVC.php and Client.php controller.. then copy paste the code above. since i didnt have Member model i just hardcode the $row values just like wades did. and then added the routes. and as a result i get message above.

could anybody give me advice n direction
thx alot.

El Forum

[eluser]herrucules[/eluser]
i've correct the error, my friend told me that i must unload the soap module from php extension first.. now the no error but the $row returned is empty

A PHP Error was encountered
Severity: Notice

Message: Undefined index:

Filename: libraries/Nusoap.php

Line Number: 6497

i've tried to var_dump the values and it came out as an empty string: string(0) ""

any help ?

El Forum

[eluser]heriniaina[/eluser]
I have the same error

Quote:A PHP Error was encountered
Severity: Notice

Message: Undefined index:

Filename: libraries/Nusoap.php

Line Number: 6497


I use an external SOAP service (not in the same server)

Code:
$this->nusoap_client = new soapclient("http://radiovazogasy.com/wsdl/?wsdl");
    
    if($this->nusoap_client->fault)
    {
        $text = 'Error: '.$this->nusoap_client->fault;
    }
    else
    {
        if ($this->nusoap_client->getError())
        {
            $text = 'Error: '. $this->nusoap_client->getError();
        }
        else
        {
            $param = array('wsid' => "test");
            $row = $this->nusoap_client->call('rvgHandeha', $param);        
            
            var_dump($row);
        }
    }

Anybody can help?
Thanks
Pages: 1 2