Wednesday, February 11, 2015

Nusoap in Codeigniter Tutorial

Cài đặt: Download thư viện của Nusoap tại Nusoap Library. Phiên bản mình dùng cho blog này là 0.9.5
- Giải nén và copy toàn bộ thư viện trong thư mục lib vào application/libraries và đổi tên lại thành nusoap. 
- Bước tiếp theo tạo 1 file tên nusoap_lib.php trong thư mục application/libraries.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Nusoap_lib
{
   function Nusoap_lib()
   {
       require_once(realpath(APPPATH. '../application').'/libraries/nusoap/nusoap'.EXT);
   }
}
- Ok đã xong phần cài đặt, bây giờ mình sẽ tạo một vài thao tác đơn giản để test service

Bên Server: tạo một soap server mang tên soap_member.php (tránh tạo trùng tên soap_server.php hay soapserver.php vì có thể trùng với 1 vài class trong thư viện của nusoap :D)
Soap server
<?php
class Soap_member extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->library('Nusoap_lib');
        $this->nusoap_server = new soap_server();
        $this->nusoap_server->configureWSDL('soap_member', 'urn:soap_member');
        $this->nusoap_server->register('hello', // method name
                array('name' => 'xsd:string'), // input parameters
                array('return' => 'xsd:string'), // output parameters
                'urn:Soap_server', // namespace
                'urn:Soap_server#hello', // soapaction
                'rpc', // style
                'encoded', // use
                'Says hello to the caller' // documentation
        );
        $this->nusoap_server->register('addnumbers', // method name
                array('a' => "xsd:string", 'b' => "xsd:string"), // input parameters
                array('return' => 'xsd:string'), // output parameters
                'urn:Soap_server', // namespace
                'urn:Soap_server#addnumbers', // soapaction
                'rpc', // style
                'encoded', // use
                'Says hello to the caller' // documentation
        );
    }
    function index() {
        if ($this->uri->rsegment(3) == 'wsdl') {
            $_SERVER['QUERY_STRING'] = 'wsdl';
        } else {
            $_SERVER['QUERY_STRING'] = '';
        }
        function hello($name) {
            return 'Hello, ' . $name;
        }
        function addnumbers($a, $b) {
            return 'Sum: '. ($a + $b);
        }
        $this->nusoap_server->service(file_get_contents('php://input'));
    }
}

Bên Client: 
Soap client
<?php
class Soap_client extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->library('Nusoap_lib');
        $this->load->helper('url');
        try {
            $this->soapclient = new nusoap_client(site_url('gallery/soap_member/index/wsdl'));
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
            var_dump($exc);
        }


        $err = $this->soapclient->getError();
        if ($err) {
            echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
        }
    }
    function index() {
        $result = $this->soapclient->call('hello', array('name' => 'Scott'));
        // Check for a fault
        if ($this->soapclient->fault) {
            echo '<h2>Fault</h2><pre>';
            print_r($result);
            echo '</pre>';
        } else {
            // Check for errors
            $err = $this->soapclient->getError();
            if ($err) {
                // Display the error
                echo '<h2>Error</h2><pre>' . $err . '</pre>';
            } else {
                // Display the result
                echo '<h2>Result</h2><pre>';
                print_r($result);
                echo '</pre>';
            }
        }
    }
    function test() {
        $params = array(
            'a' => 2,
            'b' => 3,
        );
        $result = $this->soapclient->call('addnumbers', $params);
        // Check for a fault
        if ($this->soapclient->fault) {
            echo '<h2>Fault</h2><pre>';
            print_r($result);
            echo '</pre>';
        } else {
            // Check for errors
            $err = $this->soapclient->getError();
            if ($err) {
                // Display the error
                echo '<h2>Error</h2><pre>' . $err . '</pre>';
            } else {
                // Display the result
                echo '<h2>Result</h2><pre>';
                print_r($result);
                echo '</pre>';
            }
        }
    }
}

  • OK bây giờ mình sẽ chạy trên host để test thử http://Yourdomain/gallery/soap_client/


  •   http://Yourdomain/gallery/soap_client/test

     
 Chúc các bạn thành công!
P/S: nguồn tham khảo https://makeitaasaan.wordpress.com/2014/02/23/how-to-develop-soap-server-and-soap-client-in-codeignitor-using-nusoap-library/

No comments:

Post a Comment