WebService-php- 2(17),webservice-php-17_PHP教程

WBOY
Freigeben: 2016-07-13 09:55:07
Original
954 Leute haben es durchsucht

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

wsdl实例

<?xml version =<span>'</span><span>1.0</span><span>'</span> encoding =<span>'</span><span>UTF-8</span><span>'</span> ?>
<<span>definitions
targetNamespace</span>=<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>
<xsd:schema xmlns:xsd=<span>"</span><span>http://www.w3.org/2001/XMLSchema</span><span>"</span><span>
targetNamespace</span>=<span>"</span><span>http://localhost/00/</span><span>"</span>>
</xsd:schema>
</types>
<!--
<message><span> 元素可定义每个消息的部件,以及相关联的数据类型.
</span>-->
<message name=<span>'</span><span>testRequest</span><span>'</span>>
<part name=<span>"</span><span>term</span><span>"</span> type=<span>"</span><span>xsd:string</span><span>"</span>/>
</message>
<message name=<span>'</span><span>testResponse</span><span>'</span>>
<part name=<span>"</span><span>value</span><span>"</span> type=<span>"</span><span>xsd:string</span><span>"</span>/>
</message>
<!--
<portType><span> 元素是最重要的 WSDL 元素.它可描述一个 web service、可被执行的操作,以及相关的消息.
它告诉你去哪个WebService的连接点,扮演了一个控制者.
</span>-->
<portType name=<span>'</span><span>oplist</span><span>'</span>>
<operation name=<span>'</span><span>test</span><span>'</span>>
<input message=<span>'</span><span>tns:testRequest</span><span>'</span>/>
<output message=<span>'</span><span>tns:testResponse</span><span>'</span>/>
</operation>
</portType>
<!--<binding> 元素为每个端口定义消息格式和协议细节-->
<binding name=<span>'</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-->
<soap:binding style=<span>'</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><span>test</span><span>'</span>>
<soap:operation soapAction=<span>'</span><span>http://www.cwtservice.cn/newOperation/</span><span>'</span>/>
<input>
<soap:body use=<span>'</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>/>
</input>
<output>
<soap:body use=<span>'</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>
</operation>
</binding>
<!--<service>包含一个或者多个port元素,每个port元素表示一个不同的Web服务-->
<service name=<span>'</span><span>shopWS</span><span>'</span>>
<port name=<span>'</span><span>cartSoap</span><span>'</span> binding=<span>'</span><span>tns:cartSoap</span><span>'</span>>
<soap:address location=<span>'</span><span>http://localhost/00/wss.php</span><span>'</span>/>
</port>
</service>
</definitions>
Nach dem Login kopieren

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();
Nach dem Login kopieren

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>));
Nach dem Login kopieren

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

<message name=<span>'</span><span>testRequest</span><span>'</span>>
<part name=<span>"</span><span>term</span><span>"</span> type=<span>"</span><span>xsd:ArrayOfString</span><span>"</span>/>
</message>
<message name=<span>'</span><span>testResponse</span><span>'</span>>
<part name=<span>"</span><span>value</span><span>"</span> type=<span>"</span><span>xsd:ArrayOfString</span><span>"</span>/>
</message>
Nach dem Login kopieren

XML-RPC调用

XML-<span>RPC可以理解为简化版的soap,对数据的包装相对简洁.
php.ini中,要打开extension</span>=php_xmlrpc.dll
Nach dem Login kopieren
<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);
Nach dem Login kopieren

客户端:

<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>));
Nach dem Login kopieren

WebService与json Api的区别

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/993278.htmlTechArticleWebService-php- 2(17),webservice-php-17 wsdl实例 ?xml version = ' 1.0 ' encoding = ' UTF-8 ' ? definitionstargetNamespace = ' http://localhost/00/ ' xmlns:tns = ' http://lo...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage