- 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');- 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
class Nusoap_lib
{
function Nusoap_lib()
{
require_once(realpath(APPPATH. '../application').'/libraries/nusoap/nusoap'.EXT);
}
}
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'));
}
}
<?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>';
}
}
}
}





No comments:
Post a Comment