欢迎光临
我们一直在努力

PHP5中使用Web服务访问J2EE应用程序(3)-PHP教程,PHP应用

建站超值云服务器,限时71元/月

解释 wsdl

  我们已经成功地调用了 weather 服务,但是还没有看过它的 wsdl 文档。wsdl 中的细节要比 soapclient 公开的多。我们如何知道应该在 startdate 参数中放什么呢?我们应该期望从返回的数据中实际得到什么?要回答这些问题,必须更深入地分析 wsdl。

  可以从下载部分下载 weather forecast 应用程序的 wsdl。如果使用不同的 web 服务,只需要在浏览器中打开相应的 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 结构被定义为:

<element name=”getforecast”>
<complextype>
<sequence>
<element name=”startdate” nillable=”true” type=”xsd:datetime”/>
<element name=”days” type=”xsd:int”/>
</sequence>
</complextype>
</element>

  于是我们知道该函数需要两个参数,xsd:datetime 类型的 startdate 和整数类型的 days。这与我们所了解的 soapclient::_gettypes 函数完全匹配,但是现在我们还知道 startdate 可以为空(nillable)。毫无疑问,如果我们简化输入参数,那么该函数将如下所示:

$forecastresponse = $soapclient->getforecast(array(startdate=>null, days=>3));

  如果明确指定今天的日期,结果会与所指定的完全一致。

  如果希望制定其他起始日期怎么办呢?xml schema将 datetime 定义成一种基本类型,按照 iso 8601 标准格式化,比如“2004-12-01t00:00:00”。假设希望了解三天之后的天气预报,可以使用内置函数 strtotime(“+3 days”) 获得需要的日期,该函数与 time() 函数相同,都返回标准 unix 格式的日期时间,即表示从公元纪年开始到现在的秒数的一个整数。我们知道 xml schema 要求日期采用具有字符串字段的 iso 8601 格式进行编码,于是在示例客户机中编写了 timetoiso8601 函数,将整数日期转换成 soap 编码定义的格式。但我们吃惊地发现,其实并不需要这样做,ext/soap 非常聪明地将整数日期转化成了需要的字符串字段格式。无论传递的是整数还是预格式化的字符串,都没有关系,最终传送的 soap 消息都是一样的。

  响应中的日期又如何呢?在回程中,ext/soap 从 soap 响应获得了 datetime 字段,但是没有做任何格式转换。我们希望它返回一个整数,以表示从公元纪年到现在的秒数,但实际上得到的是按照 iso 8601 格式化的字符串。于是我们使用 strtotime 函数将其转化成整数,然后使用 strftime 格式化该整数,以便于表示。

  weather service 按日期提供预报,但它忽略了 datetime 编码中的时间成分。所以我们没有考虑这方面的调整,如果从运行在不同时区内的服务中请求天气预报,那么可能必须这样做。如果希望进一步了解时区转换,请参阅参考资料中给出的描述 iso 8601 标准的文章。

  现在再回到响应格式上来。上一节中曾经提到 getforecast 返回数据的不一致性。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 不允许出现单元素数组这种特例。不幸的是,当响应只包含一个 weather 对象时,ext/soap 没有考虑 wsdl 中应用于 getforecastresponse 的 <sequence> 标签,因为这种行为在客户机代码中造成了不必要的复杂性。

  最后,wsdl 文档还告诉 soap 客户机可以从网络中的哪个地方找到该服务:

<wsdl:service name=”weatherforecastejbservice”>
<wsdl:port binding=”intf:weatherforecastejbsoapbinding”
name=”weatherforecastejb”>
<wsdlsoap:address location=
“http://localhost:9080/itsowebservice2routerweb/services/weatherforecastejb”/>
</wsdl:port>
</wsdl:service>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » PHP5中使用Web服务访问J2EE应用程序(3)-PHP教程,PHP应用
分享到: 更多 (0)