当前位置:Gxlcms > PHP教程 > WebService-php-2(17)

WebService-php-2(17)

时间:2021-07-01 10:21:17 帮助过:9人阅读

wsdl实例

'1.0' encoding ='UTF-8' ?>
<definitions
targetNamespace='http://localhost/00/'
xmlns:tns='http://localhost/00/'
xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
xmlns='http://schemas.xmlsoap.org/wsdl/'>


"http://www.w3.org/2001/XMLSchema"
targetNamespace="http://localhost/00/">



'testRequest'>
"term" type="xsd:string"/>

'testResponse'>
"value" type="xsd:string"/>


'oplist'>
'test'>
'tns:testRequest'/>
'tns:testResponse'/>



'cartSoap' type='tns:oplist'>

'rpc'
transport='http://schemas.xmlsoap.org/soap/http'/>

'test'>
'http://www.cwtservice.cn/newOperation/'/>

'encoded' namespace='urn:xmethods-delayed-quotes'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>


'encoded' namespace='urn:xmethods-delayed-quotes'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>




'shopWS'>
'cartSoap' binding='tns:cartSoap'>
'http://localhost/00/wss.php'/>


Server端示例:

function test($x) {
return $x;
}
$ss = new SoapServer('http://localhost/00/wsdl.xml');
$ss->addFunction('test');
$ss->handle();

Client调用:

$soap = new soapClient('http://localhost/00/wsdl.xml',array('trace'=>true));
var_dump($soap->test('10086'));

传递和返回数组参数
如果传递或返回的参数为数组,可以在message标签中做说明.

'testRequest'>
"term" type="xsd:ArrayOfString"/>

'testResponse'>
"value" type="xsd:ArrayOfString"/>

XML-RPC调用

XML-RPC可以理解为简化版的soap,对数据的包装相对简洁.
php.ini中,要打开extension=php_xmlrpc.dll

/*
求和函数
注意,rpc服务器在调用函数时,传的参数是这样的:
array(0=>'函数名' , 1=>array(实参1,实参2,...实参N) , 2=>NULL)
*/
function hello() {
return 'hello';
}
function sum($method , $args , $extra) {
return array_sum($args);
}
// 创建RPC Server
$server = xmlrpc_server_create ();
xmlrpc_server_register_method ($server , 'hello' , 'hello');
xmlrpc_server_register_method ($server , 'sum' , 'sum');
// 收取请求
$request = $HTTP_RAW_POST_DATA;
//执行调用客户端的XML请求后获取执行结果
$xmlrpc_response = xmlrpc_server_call_method($server, $request , null);
//把函数处理后的结果XML进行
输出 header('Content-Type: text/xml'); echo $xmlrpc_response; //销毁XML-RPC服务器端资源 xmlrpc_server_destroy($server);

客户端:

class rpcclient {
protected $url;
public function __construct($url='' ) {
$this->url = $url;
}
protected function query($request) {
$context = stream_context_create(array('http' => array(
'method' => "POST",
'header' => "Content-Type: text/xml",
'content' => $request
)));
$xml = file_get_contents($this->url, false, $context);
return xmlrpc_decode($xml);
}
public function __call($method , $args) {
$request = xmlrpc_encode_request($method , $args);
return $this->query($request);
}
}
$rpc = new rpcclient('http://localhost/00/rpcs.php');
var_dump($rpc->hello());
var_dump($rpc->sum(4,5,6));

WebService与json Api的区别

      WebService   json API
数据封装  XML       json
复杂度   高        低
底层协议 不限       HTTP
数据类型 可严格定义    不可严格定义
自说明 性自说明    需额外API文档

以上就介绍了WebService-php- 2(17),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行