天天看點

(二)基于PHP——簡單的WSDL的建立(WSDL篇)

轉載請注明出處:http://blog.csdn.net/cwt0408/article/details/6952936

(謝謝合作!)

1、建立WSDL檔案

     建立WSDL的工具很多,eclipse、zendstudio、vs都可以,我個人建議自己寫,熟悉結構,另外自動工具對xml schame類型支援在類型中可能會報錯。

下面是我自己寫的模闆:

<?xml version ='1.0' encoding ='UTF-8' ?> 
<definitions name='自定義名稱' 
  targetNamespace='目标命名空間(WSDL所在位址)' 
	<!--tns自定義目标空間,下面會用到-->
  xmlns:tns='目标命名空間(WSDL所在位址)'
  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="目标命名空間(WSDL所在位址)"> 
 	</xsd:schema>
 </types> 

<!--
<message> 元素可定義每個消息的部件,以及相關聯的資料類型。
請求-響應是最普通的操作類型,不過 WSDL 定義了四種類型:
One-way	此操作可接受消息,但不會傳回響應。
Request-response	此操作可接受一個請求并會傳回一個響應。(常用)
Solicit-response	此操作可發送一個請求,并會等待一個響應。
Notification	此操作可發送一條消息,但不會等待響應。
-->  
<message name='請求消息名稱(方法名+Request)'> 
	<part name="term" type="xsd:string"/>
</message> 

<message name='響應消息名稱(方法名+Response)'> 
	<part name="value" type="xsd:string"/>
</message>

<!--
<portType> 元素是最重要的 WSDL 元素。它可描述一個 web service、可被執行的操作,以及相關的消息。
它告訴你去哪個WebService的連接配接點,扮演了一個控制者。
-->
<portType name='執行的操作名稱(binding的type與其對應)'> 
  <operation name='執行操作的方法'> 
    <input message='tns:*Request'/> 
    <output message='tns:*response'/> 
  </operation> 
</portType>

<!--<binding> 元素為每個端口定義消息格式和協定細節-->
<binding name='Binding的名稱,與service的port名稱對應' type='指向用于Binding的端口(tns(字首):PortType名稱)'> 
<!--style:屬性可取值 "rpc" 或 "document",ransport:屬性定義了要使用的 SOAP 協定。在這個例子中我們使用 HTTP-->
  <soap:binding style='rpc' 
    transport='http://schemas.xmlsoap.org/soap/http'/> 
	<!--operation 元素定義了每個端口提供的操作符,對于每個操作,相應的 SOAP 行為都需要被定義-->
  <operation name='GetCallDetailRecords'> 
    <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='服務名稱'> 
  <port name='Binding名稱' binding='tns:Binding名稱'> 
    <soap:address location='http://目标命名空間(WSDL所在位址)/service.php'/> 
  </port> 
</service> 
</definitions>
           

2、在service目錄下建立myphone.wsdl

<?xml version ='1.0' encoding ='UTF-8' ?> 
<definitions name='phonebook' 
  targetNamespace='http://www.mysoapservice.cn/' 
  xmlns:tns='http://www.mysoapservice.cn/'
  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>
 	<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 		targetNamespace="http://www.mysoapservice.cn/"> 
 	</xsd:schema>
 </types> 

<message name='GetPhoneBookRequest'> 
	<part name="name" type="xsd:string"/>
</message> 

<message name='GetPhoneBookResponse'> 
	<part name="phonebookInfo" type="xsd:string"/>
</message>

<portType name='PhoneBookToEveryOneProt'> 
  <operation name='GetPhoneBook'> 
    <input message='tns:GetPhoneBookRequest'/> 
    <output message='tns:GetPhoneBookResponse'/> 
  </operation>
</portType>

<binding name='PhoneBookSOAP' type='tns:PhoneBookToEveryOneProt'> 
  <soap:binding style='rpc' 
    transport='http://schemas.xmlsoap.org/soap/http'/> 
  <operation name='GetPhoneBook'> 
    <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 name='PhoneBookService'> 
  <port name='PhoneBookSOAP' binding='tns:PhoneBookSOAP'> 
    <soap:address location='http://www.mysoapservice.cn/service.php'/> 
  </port> 
</service> 
</definitions>
           

3、修改service.php

<?php
function GetPhoneBook($name){
	$pbook="Zhangsan,13888888888,friend,[email protected]";
	return $pbook;
}
$server = new SoapServer("myphone.wsdl");
$server->addFunction("GetPhoneBook");
$server->handle (); 
?>
           

4、修改client.php

<?php
header('Content-Type:text/html;charset=utf-8');
$client = new SoapClient("http://www.mysoapservice.cn/service.php?WSDL" , array('trace'=>true));
var_dump($client->__getTypes());
try {
 $response = $client->GetPhoneBook('zhang');
 var_dump($response);
}catch (SoapFault $sf){
 var_dump($sf);
 print ($client->__getLastRequest());
 print ($client->__getLastResponse());
}
?>
           

測試:

http://www.mysoapclient.cn/client.php

(二)基于PHP——簡單的WSDL的建立(WSDL篇)