这一节将建立我们自己的 PHP Weather 客户机。这里提供了一些代码片段,建议下载完整的客户机和 WSDL 文件。
用于表示 Weather Service 的 ext/soap 类是 SoapClient。正如我们介绍 Weather Forecast 应用程序时所讨论的,我们知道应用服务器在 http://host:port/ItsoWebServer2RouterWeb/wsdl/itso/session/WeatherForecastEJB.wsdl 中提供了 WSDL。我们使用的是默认端口,并且在作为服务器的计算机上工作,这样就可以通过查找 WSDL 文件创建第一个 SoapClient:
<?php $soapClient = new SoapClient("http://localhost:9080/" . "ItsoWebService2RouterWeb/wsdl/itso/session/WeatherForecastEJB.wsdl"); ?> |
注意,因为 ext/soap 是内置的,所以,在引用 SoapClient 之前,不需要任何 include 或 require 语句。
现在已经实例化了客户机,还要联系 Weather 服务,并调用它的 getForecast 操作。在 WSDL 模式下使用 SoapClient 时,ext/soap 有一种很好的特性,即可以直接引用远程操作,就像它是 SoapClient 自身的函数一样。但是在建立输入参数时需要一点技巧。ext/soap 可以提供从 WSDL 中发现的操作和参数的数组:
$functions = $soapClient->__getFunctions(); print_r($functions); $types = $soapClient->__getTypes(); print_r($types); |
只需要显示与 getForecast 相关的结果,并重新格式化这些结果,以方便阅读,于是我们看到以下代码:
getForecastResponse getForecast(getForecast $parameters) struct getForecast { dateTime startDate; int days; } struct getForecastResponse { Weather getForecastReturn; } struct Weather { string condition; dateTime date; string windDirection; int windSpeed; int temperatureCelsius; boolean dbflag; } |
ext/soap 实际上并没有为我们定义 getForecast 类,我们必须创建该操作所需要的输入参数数组:
$getForecastParam = array(@#startDate@# =>time(), @#days@# => 3); |
然后像 SoapClient 的方法那样调用该操作:
$forecastResponse = $soapClient->getForecast($getForecastParam); |
最后我们得到了返回的 getForecastResponse 对象,它本身是一个 Weather 对象数组,然后在表格中显示结果:
echo "<table border=1 cellpadding=5>"; echo "<tr><th>Date</th><th>Condition</th><th>Temperature</th><th>Wind</th></tr>"; $weatherArray = $forecastResponse->getForecastReturn; foreach ($weatherArray as $weather) { echo "<tr>", "<td>",strftime("%a. %b %d, %Y", strtotime($weather->date)),"</td>", "<td>$weather->condition</td>", "<td>$weather->temperatureCelsius</td>", "<td>$weather->windDirection $weather->windSpeed</td>", "</tr>"; } echo "</table>"; |
PHP 客户机与 Java 客户机的输出相同,于是我们知道圣诞节期间 San Jose 不会下雪……
领测软件测试网最新更新
关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved 北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5 技术支持和业务联系:info@testage.com.cn 电话:010-51297073 |