PHP头条
热点:

WebService-php- 2(17),webservice-php-17


wsdl实例

<?xml version ='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/'>
<!--<types> 元素定义 web service 使用的数据类型,WSDL 使用 XML Schema 语法来定义数据类型,也可以自定义Schema不包含的类型-->
<types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://localhost/00/">
</xsd:schema>
</types>
<!--
<message> 元素可定义每个消息的部件,以及相关联的数据类型.
-->
<message name='testRequest'>
<part name="term" type="xsd:string"/>
</message>
<message name='testResponse'>
<part name="value" type="xsd:string"/>
</message>
<!--
<portType> 元素是最重要的 WSDL 元素.它可描述一个 web service、可被执行的操作,以及相关的消息.
它告诉你去哪个WebService的连接点,扮演了一个控制者.
-->
<portType name='oplist'>
<operation name='test'>
<input message='tns:testRequest'/>
<output message='tns:testResponse'/>
</operation>
</portType>
<!--<binding> 元素为每个端口定义消息格式和协议细节-->
<binding name='cartSoap' type='tns:oplist'>
<!--style:属性可取值 "rpc""document",ransport:属性定义了要使用的 SOAP 协议.在这个例子中我们使用 HTTP-->
<soap:binding style='rpc'
transport='http://schemas.xmlsoap.org/soap/http'/>
<!--operation 元素定义了每个端口提供的操作符,对于每个操作,相应的 SOAP 行为都需要被定义-->
<operation name='test'>
<soap:operation soapAction='http://www.cwtservice.cn/newOperation/'/>
<input>
<soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</input>
<output>
<soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
</output>
</operation>
</binding>
<!--<service>包含一个或者多个port元素,每个port元素表示一个不同的Web服务-->
<service name='shopWS'>
<port name='cartSoap' binding='tns:cartSoap'>
<soap:address location='http://localhost/00/wss.php'/>
</port>
</service>
</definitions>

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标签中做说明.

<message name='testRequest'>
<part name="term" type="xsd:ArrayOfString"/>
</message>
<message name='testResponse'>
<part name="value" type="xsd:ArrayOfString"/>
</message>

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文档

www.phpzy.comtrue/php/36546.htmlTechArticleWebService-php- 2(17),webservice-php-17 wsdl实例 ?xml version = ' 1.0 ' encoding = ' UTF-8 ' ? definitionstargetNamespace = ' http://localhost/00/ ' xmlns:tns = ' http://localhost/00/ ' xmlns:soap = ' http://schemas.xmlsoap.org/wsdl...

相关文章

    暂无相关文章

PHP之友评论

今天推荐