WebService-php-2(17)

WBOY
發布: 2016-08-08 09:25:10
原創
972 人瀏覽過

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>
登入後複製

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><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>
登入後複製

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       〟不限        HTTP
資料類型     可嚴格定義    不可嚴格定義
自說明        性自合   〜
以上就介紹了WebService-php- 2(17),包含了面向的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!