PHP5中使用Web服務(wù)訪問J2EE應(yīng)用程序(3)
Jun 21, 2016 am 09:15 AMj2ee|php5|web|web服務(wù)|程序|訪問
解釋 WSDL
我們已經(jīng)成功地調(diào)用了 Weather 服務(wù),但是還沒有看過它的 WSDL 文檔。WSDL 中的細節(jié)要比 SoapClient 公開的多。我們?nèi)绾沃缿?yīng)該在 startDate 參數(shù)中放什么呢?我們應(yīng)該期望從返回的數(shù)據(jù)中實際得到什么?要回答這些問題,必須更深入地分析 WSDL。
可以從下載部分下載 Weather Forecast 應(yīng)用程序的 WSDL。如果使用不同的 Web 服務(wù),只需要在瀏覽器中打開相應(yīng)的 WSDL 文檔即可。
getForecast 操作的 WSDL 是:
<wsdl:operation name="getForecast">
<wsdl:input message="intf:getForecastRequest" name="getForecastRequest"/>
<wsdl:output message="intf:getForecastResponse" name="getForecastResponse"/>
</wsdl:operation>
其中的 getForecastRequest 消息被定義為:
<wsdl:message name="getForecastRequest">
<wsdl:part element="intf:getForecast" name="parameters"/>
</wsdl:message>
而 getForecast 結(jié)構(gòu)被定義為:
<element name="getForecast">
<complexType>
<sequence>
<element name="startDate" nillable="true" type="xsd:dateTime"/>
<element name="days" type="xsd:int"/>
</sequence>
</complexType>
</element>
于是我們知道該函數(shù)需要兩個參數(shù),xsd:dateTime 類型的 startDate 和整數(shù)類型的 days。這與我們所了解的 SoapClient::_getTypes 函數(shù)完全匹配,但是現(xiàn)在我們還知道 startDate 可以為空(nillable)。毫無疑問,如果我們簡化輸入?yún)?shù),那么該函數(shù)將如下所示:
$forecastResponse = $soapClient->getForecast(array('startDate'=>Null, 'days'=>3));
如果明確指定今天的日期,結(jié)果會與所指定的完全一致。
如果希望制定其他起始日期怎么辦呢?XML Schema將 dateTime 定義成一種基本類型,按照 ISO 8601 標準格式化,比如“2004-12-01T00:00:00”。假設(shè)希望了解三天之后的天氣預(yù)報,可以使用內(nèi)置函數(shù) strtotime("+3 days") 獲得需要的日期,該函數(shù)與 time() 函數(shù)相同,都返回標準 UNIX 格式的日期時間,即表示從公元紀年開始到現(xiàn)在的秒數(shù)的一個整數(shù)。我們知道 XML Schema 要求日期采用具有字符串字段的 ISO 8601 格式進行編碼,于是在示例客戶機中編寫了 timeToIso8601 函數(shù),將整數(shù)日期轉(zhuǎn)換成 SOAP 編碼定義的格式。但我們吃驚地發(fā)現(xiàn),其實并不需要這樣做,ext/soap 非常聰明地將整數(shù)日期轉(zhuǎn)化成了需要的字符串字段格式。無論傳遞的是整數(shù)還是預(yù)格式化的字符串,都沒有關(guān)系,最終傳送的 SOAP 消息都是一樣的。
響應(yīng)中的日期又如何呢?在回程中,ext/soap 從 SOAP 響應(yīng)獲得了 dateTime 字段,但是沒有做任何格式轉(zhuǎn)換。我們希望它返回一個整數(shù),以表示從公元紀年到現(xiàn)在的秒數(shù),但實際上得到的是按照 ISO 8601 格式化的字符串。于是我們使用 strtotime 函數(shù)將其轉(zhuǎn)化成整數(shù),然后使用 strftime 格式化該整數(shù),以便于表示。
Weather Service 按日期提供預(yù)報,但它忽略了 dateTime 編碼中的時間成分。所以我們沒有考慮這方面的調(diào)整,如果從運行在不同時區(qū)內(nèi)的服務(wù)中請求天氣預(yù)報,那么可能必須這樣做。如果希望進一步了解時區(qū)轉(zhuǎn)換,請參閱參考資料中給出的描述 ISO 8601 標準的文章。
現(xiàn)在再回到響應(yīng)格式上來。上一節(jié)中曾經(jīng)提到 getForecast 返回數(shù)據(jù)的不一致性。WSDL 描述告訴我們 getForecast 返回一個 getForecastResponse 對象,getForecastResponse 可以包含無限多個稱為 Weather 的復雜類型的列表:
<element name="getForecastResponse">
<complexType>
<sequence>
<element maxOccurs="unbounded" name="getForecastReturn" type="tns2:Weather"/>
</sequence>
</complexType>
</element>
<complexType name="Weather">
<sequence>
<element name="condition" nillable="true" type="xsd:string"/>
<element name="date" nillable="true" type="xsd:dateTime"/>
<element name="windDirection" nillable="true" type="xsd:string"/>
<element name="windSpeed" type="xsd:int"/>
<element name="temperatureCelsius" type="xsd:int"/>
<element name="dbflag" type="xsd:boolean"/>
</sequence>
</complexType>
WSDL 不允許出現(xiàn)單元素數(shù)組這種特例。不幸的是,當響應(yīng)只包含一個 Weather 對象時,ext/soap 沒有考慮 WSDL 中應(yīng)用于 getForecastResponse 的 <sequence> 標簽,因為這種行為在客戶機代碼中造成了不必要的復雜性。
最后,WSDL 文檔還告訴 SOAP 客戶機可以從網(wǎng)絡(luò)中的哪個地方找到該服務(wù):
<wsdl:service name="WeatherForecastEJBService">
<wsdl:port binding="intf:WeatherForecastEJBSoapBinding"
name="WeatherForecastEJB">
<wsdlsoap:address location=
"http://localhost:9080/ItsoWebService2RouterWeb/services/WeatherForecastEJB"/>
</wsdl:port>
</wsdl:service>

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)