1,不使用wsdl
建立:soap文件夹后把nusoap库放如,nusoap下载地址:http://sourceforge.net/projects/nusoap/
服务端:helloworld2.php
<?php
//包函nusoap.php
require_once('./lib/nusoap.php');
//创建服务端
$server = new soap_server;
//定义客户端调用方法
$server->register('hello');
//调用方法以及参数
function hello($name) {
return 'Hello, ' . $name;
}
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
client端:hello.php:
<?php
//包函nusoap.php
require_once('./lib/nusoap.php');
//新建一个soap客户端,调用服务端提供的wsdl
//$client = new soapclient('http://localhost/soap/hellowsdl2.php?wsdl', true);
$client = new soapclient('http://localhost/soap/helloworld2.php');
//查看一下是不是报错
$err = $client->getError();
if ($err) {
//显示错误
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
}
//调用服务端的方法
$result = $client->call('hello', array('person' => "this is a soap"));
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
?>
2,使用wsld
建立目录:test,把nusoap库放如,nusoap下载地址:http://sourceforge.net/projects/nusoap/
服务端:helloworld2.php
<?php
//包函nusoap.php
require_once('./lib/nusoap.php');
//新建一个soap服务
$server = new soap_server();
//初始化支持wsdl
$server->configureWSDL('hellowsdl2', 'urn:hellowsdl2');
//定义数据结构来接收数据
$server->wsdl->addComplexType(
'Person',
'complexType',
'struct',
'all',
'',
array(
'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'),//后面的type定义数据的类型,这个是string
'age' => array('name' => 'age', 'type' => 'xsd:int'),//后面的type定义数据的类型,这个是int
'gender' => array('name' => 'gender', 'type' => 'xsd:string')//后面的type定义数据的类型,这个是string
)
);
$server->wsdl->addComplexType(
'SweepstakesGreeting',
'complexType',
'struct',
'all',
'',
array(
'greeting' => array('name' => 'greeting', 'type' => 'xsd:string'),
'winner' => array('name' => 'winner', 'type' => 'xsd:string')
)
);
//服务器定义的soap调用方法
$server->register('hello', // 方法名字hello,方法就在下面
array('person' => 'tns:Person'), // 客户端传来的变量
array('return' => 'tns:SweepstakesGreeting'), //返回参数
'urn:hellowsdl2', // soap名
'urn:hellowsdl2#hello', // soap的方法名
'rpc', // 使用的方式
'encoded', // 编码
'test' // 存档
);
//定义上面注册过的函数hello
function hello($person) {
$greeting = 'Hello, ' . $person['firstname'] .
'. It is nice to meet a ' . $person['age'] .
' year old ' . $person['gender'] . '.';
$winner = 'Scott';
//要返回的数据
return array(
'greeting' => $greeting,
'winner' => $winner
);
}
// 请求时(试图)调用服务
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
客户端:hello.php
<?php
//包函nusoap.php
require_once('./lib/nusoap.php');
//新建一个soap客户端,调用服务端提供的wsdl
//$client = new soapclient('http://localhost/test/hellowsdl2.php?wsdl', true);
$client = new soapclient('http://localhost/test/helloworld2.php');
//查看一下是不是报错
$err = $client->getError();
if ($err) {
//显示错误
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
}
//要向服务端要传的参数
$person = array('firstname' => 'Willi', 'age' => 22, 'gender' => 'male');
//调用服务端的方法
$result = $client->call('hello', array('person' => $person));
//错误审核
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
$err = $client->getError();
if ($err) {
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
//显示请求信息
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
//显示返回信息
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
//显示调试信息
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>
实践来源:http://www.jz123.cn/text/1234122.html
建立:soap文件夹后把nusoap库放如,nusoap下载地址:http://sourceforge.net/projects/nusoap/
服务端:helloworld2.php
<?php
//包函nusoap.php
require_once('./lib/nusoap.php');
//创建服务端
$server = new soap_server;
//定义客户端调用方法
$server->register('hello');
//调用方法以及参数
function hello($name) {
return 'Hello, ' . $name;
}
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
client端:hello.php:
<?php
//包函nusoap.php
require_once('./lib/nusoap.php');
//新建一个soap客户端,调用服务端提供的wsdl
//$client = new soapclient('http://localhost/soap/hellowsdl2.php?wsdl', true);
$client = new soapclient('http://localhost/soap/helloworld2.php');
//查看一下是不是报错
$err = $client->getError();
if ($err) {
//显示错误
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
}
//调用服务端的方法
$result = $client->call('hello', array('person' => "this is a soap"));
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
?>
2,使用wsld
建立目录:test,把nusoap库放如,nusoap下载地址:http://sourceforge.net/projects/nusoap/
服务端:helloworld2.php
<?php
//包函nusoap.php
require_once('./lib/nusoap.php');
//新建一个soap服务
$server = new soap_server();
//初始化支持wsdl
$server->configureWSDL('hellowsdl2', 'urn:hellowsdl2');
//定义数据结构来接收数据
$server->wsdl->addComplexType(
'Person',
'complexType',
'struct',
'all',
'',
array(
'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'),//后面的type定义数据的类型,这个是string
'age' => array('name' => 'age', 'type' => 'xsd:int'),//后面的type定义数据的类型,这个是int
'gender' => array('name' => 'gender', 'type' => 'xsd:string')//后面的type定义数据的类型,这个是string
)
);
$server->wsdl->addComplexType(
'SweepstakesGreeting',
'complexType',
'struct',
'all',
'',
array(
'greeting' => array('name' => 'greeting', 'type' => 'xsd:string'),
'winner' => array('name' => 'winner', 'type' => 'xsd:string')
)
);
//服务器定义的soap调用方法
$server->register('hello', // 方法名字hello,方法就在下面
array('person' => 'tns:Person'), // 客户端传来的变量
array('return' => 'tns:SweepstakesGreeting'), //返回参数
'urn:hellowsdl2', // soap名
'urn:hellowsdl2#hello', // soap的方法名
'rpc', // 使用的方式
'encoded', // 编码
'test' // 存档
);
//定义上面注册过的函数hello
function hello($person) {
$greeting = 'Hello, ' . $person['firstname'] .
'. It is nice to meet a ' . $person['age'] .
' year old ' . $person['gender'] . '.';
$winner = 'Scott';
//要返回的数据
return array(
'greeting' => $greeting,
'winner' => $winner
);
}
// 请求时(试图)调用服务
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
客户端:hello.php
<?php
//包函nusoap.php
require_once('./lib/nusoap.php');
//新建一个soap客户端,调用服务端提供的wsdl
//$client = new soapclient('http://localhost/test/hellowsdl2.php?wsdl', true);
$client = new soapclient('http://localhost/test/helloworld2.php');
//查看一下是不是报错
$err = $client->getError();
if ($err) {
//显示错误
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
}
//要向服务端要传的参数
$person = array('firstname' => 'Willi', 'age' => 22, 'gender' => 'male');
//调用服务端的方法
$result = $client->call('hello', array('person' => $person));
//错误审核
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
$err = $client->getError();
if ($err) {
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
//显示请求信息
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
//显示返回信息
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
//显示调试信息
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>
实践来源:http://www.jz123.cn/text/1234122.html
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/3956/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2011-8-30 10:39
评论列表