wsdl实例
<?xml version =<span>'<span>1.0</span><span>'</span> encoding =<span>'</span><span>UTF-8</span><span>'</span> ?> definitions targetNamespace=<span>'</span><span>http://localhost/00/</span><span>'</span><span> xmlns:tns</span>=<span>'</span><span>http://localhost/00/</span><span>'</span><span> xmlns:soap</span>=<span>'</span><span>http://schemas.xmlsoap.org/wsdl/soap/</span><span>'</span><span> xmlns:xsd</span>=<span>'</span><span>http://www.w3.org/2001/XMLSchema</span><span>'</span><span> xmlns:soapenc</span>=<span>'</span><span>http://schemas.xmlsoap.org/soap/encoding/</span><span>'</span><span> xmlns:wsdl</span>=<span>'</span><span>http://schemas.xmlsoap.org/wsdl/</span><span>'</span><span> xmlns</span>=<span>'</span><span>http://schemas.xmlsoap.org/wsdl/</span><span>'</span>> <!--<types> 元素定义 web service 使用的数据类型,WSDL 使用 XML Schema 语法来定义数据类型,也可以自定义Schema不包含的类型--> <types> <schema xmlns:xsd="<span">"<span>http://www.w3.org/2001/XMLSchema</span><span>"</span><span> targetNamespace</span>=<span>"</span><span>http://localhost/00/</span><span>"</span>> </schema> </types> <!-- <message><span> 元素可定义每个消息的部件,以及相关联的数据类型. </span>--> <message name="<span">'<span>testRequest</span><span>'</span>> <part name="<span">"<span>term</span><span>"</span> type=<span>"</span><span>xsd:string</span><span>"</span>/> </part></message> <message name="<span">'<span>testResponse</span><span>'</span>> <part name="<span">"<span>value</span><span>"</span> type=<span>"</span><span>xsd:string</span><span>"</span>/> </part></message> <!-- <portType><span> 元素是最重要的 WSDL 元素.它可描述一个 web service、可被执行的操作,以及相关的消息. 它告诉你去哪个WebService的连接点,扮演了一个控制者. </span>--> <porttype name="<span">'<span>oplist</span><span>'</span>> <operation name="<span">'<span>test</span><span>'</span>> <input message="<span">'<span>tns:testRequest</span><span>'</span>/> <output message="<span">'<span>tns:testResponse</span><span>'</span>/> </output></operation> </porttype> <!--<binding> 元素为每个端口定义消息格式和协议细节--> <binding name="<span">'<span>cartSoap</span><span>'</span> type=<span>'</span><span>tns:oplist</span><span>'</span>> <!--style:属性可取值 <span>"</span><span>rpc</span><span>"</span> 或 <span>"</span><span>document</span><span>"</span>,ransport:属性定义了要使用的 SOAP 协议.在这个例子中我们使用 HTTP--> <binding style="<span">'<span>rpc</span><span>'</span><span> transport</span>=<span>'</span><span>http://schemas.xmlsoap.org/soap/http</span><span>'</span>/> <!--operation 元素定义了每个端口提供的操作符,对于每个操作,相应的 SOAP 行为都需要被定义--> <operation name="<span">'<span>test</span><span>'</span>> <operation soapaction="<span">'<span>http://www.cwtservice.cn/newOperation/</span><span>'</span>/> <input> <body use="<span">'<span>encoded</span><span>'</span> <span>namespace</span>=<span>'</span><span>urn:xmethods-delayed-quotes</span><span>'</span><span> encodingStyle</span>=<span>'</span><span>http://schemas.xmlsoap.org/soap/encoding/</span><span>'</span>/> <output> <body use="<span">'<span>encoded</span><span>'</span> <span>namespace</span>=<span>'</span><span>urn:xmethods-delayed-quotes</span><span>'</span><span> encodingStyle</span>=<span>'</span><span>http://schemas.xmlsoap.org/soap/encoding/</span><span>'</span>/> </body></output> </body></operation></operation> </binding></binding> <!--<service>包含一个或者多个port元素,每个port元素表示一个不同的Web服务--> <service name="<span">'<span>shopWS</span><span>'</span>> <port name="<span">'<span>cartSoap</span><span>'</span> binding=<span>'</span><span>tns:cartSoap</span><span>'</span>> <address location="<span">'<span>http://localhost/00/wss.php</span><span>'</span>/> </address></port> </service>
Server端示例:
<span>function test($x) { </span><span>return</span><span> $x; } $ss </span>= <span>new</span> SoapServer(<span>'</span><span>http://localhost/00/wsdl.xml</span><span>'</span><span>); $ss</span>->addFunction(<span>'</span><span>test</span><span>'</span><span>); $ss</span>->handle();
Client调用:
$soap = <span>new</span> soapClient(<span>'</span><span>http://localhost/00/wsdl.xml</span><span>'</span>,array(<span>'</span><span>trace</span><span>'</span>=><span>true</span><span>)); var_dump($soap</span>->test(<span>'</span><span>10086</span><span>'</span>));
传递和返回数组参数
如果传递或返回的参数为数组,可以在message标签中做说明.
<message name="<span">'<span>testRequest</span><span>'</span>> <part name="<span">"<span>term</span><span>"</span> type=<span>"</span><span>xsd:ArrayOfString</span><span>"</span>/> </part></message> <message name="<span">'<span>testResponse</span><span>'</span>> <part name="<span">"<span>value</span><span>"</span> type=<span>"</span><span>xsd:ArrayOfString</span><span>"</span>/> </part></message>
XML-RPC调用
XML-<span>RPC可以理解为简化版的soap,对数据的包装相对简洁. php.ini中,要打开extension</span>=php_xmlrpc.dll
<span>/*</span><span> 求和函数 注意,rpc服务器在调用函数时,传的参数是这样的: array(0=>'函数名' , 1=>array(实参1,实参2,...实参N) , 2=>NULL) </span><span>*/</span><span> function hello() { </span><span>return</span> <span>'</span><span>hello</span><span>'</span><span>; } function sum($method , $args , $extra) { </span><span>return</span><span> array_sum($args); } </span><span>//</span><span> 创建RPC Server</span> $server =<span> xmlrpc_server_create (); xmlrpc_server_register_method ($server , </span><span>'</span><span>hello</span><span>'</span> , <span>'</span><span>hello</span><span>'</span><span>); xmlrpc_server_register_method ($server , </span><span>'</span><span>sum</span><span>'</span> , <span>'</span><span>sum</span><span>'</span><span>); </span><span>//</span><span> 收取请求</span> $request =<span> $HTTP_RAW_POST_DATA; </span><span>//</span><span>执行调用客户端的XML请求后获取执行结果</span> $xmlrpc_response = xmlrpc_server_call_method($server, $request , <span>null</span><span>); </span><span>//</span><span>把函数处理后的结果XML进行输出</span> header(<span>'</span><span>Content-Type: text/xml</span><span>'</span><span>); echo $xmlrpc_response; </span><span>//</span><span>销毁XML-RPC服务器端资源</span> xmlrpc_server_destroy($server);
客户端:
<span>class</span><span> rpcclient { </span><span>protected</span><span> $url; </span><span>public</span> function __construct($url=<span>''</span><span> ) { $</span><span>this</span>->url =<span> $url; } </span><span>protected</span><span> function query($request) { $context </span>= stream_context_create(array(<span>'</span><span>http</span><span>'</span> =><span> array( </span><span>'</span><span>method</span><span>'</span> => <span>"</span><span>POST</span><span>"</span><span>, </span><span>'</span><span>header</span><span>'</span> => <span>"</span><span>Content-Type: text/xml</span><span>"</span><span>, </span><span>'</span><span>content</span><span>'</span> =><span> $request ))); $xml </span>= file_get_contents($<span>this</span>->url, <span>false</span><span>, $context); </span><span>return</span><span> xmlrpc_decode($xml); } </span><span>public</span><span> function __call($method , $args) { $request </span>=<span> xmlrpc_encode_request($method , $args); </span><span>return</span> $<span>this</span>-><span>query($request); } } $rpc </span>= <span>new</span> rpcclient(<span>'</span><span>http://localhost/00/rpcs.php</span><span>'</span><span>); var_dump($rpc</span>-><span>hello()); var_dump($rpc</span>->sum(<span>4</span>,<span>5</span>,<span>6</span>));
WebService与json Api的区别
WebService json API
数据封装 XML json
复杂度 高 低
底层协议 不限 HTTP
数据类型 可严格定义 不可严格定义
自说明 性自说明 需额外API文档
以上就介绍了WebService-php- 2(17),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。