欢迎光临
我们一直在努力

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

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

php weather 客户机

  这一节将建立我们自己的 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 不会下雪……

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

  观察 soap 流

  我们成功地与 weather 服务取得了联系,并显示了结果。但是如果出现错误,得不到预期的结果,该怎么办?ext/soap 可以显示客户机与服务器之间交换的 soap 消息,能够帮助我们确定问题所在。

  只有使用 trace 选项创建 soapclient 时,才要使用跟踪功能。我们在 options 数组参数中设置 trace 选项,将该参数传递给 soapclient 构造函数。我们将构造函数的使用改为:

$soapclient = new soapclient(“http://localhost:9080/” .
“itsowebservice2routerweb/wsdl/itso/session/weatherforecastejb.wsdl”,
array(trace => 1));

  并在调用 goforecast 之后调用 trace 方法:

echo “request :<br>”, htmlspecialchars($soapclient->__getlastrequest()), “<br>”;
echo “response :<br>”, htmlspecialchars($soapclient->__getlastresponse()), “<br>”;

  一定要使用 htmlspecialchars 内置函数对 trace 输出进行编码,因为它将 soap xml 分界符转换成特殊字符,如 <,这样可以避免浏览器将其解释成标记。

  下面是某个请求的 trace 输出:

<?xml version=”1.0″ encoding=”utf-8″?>
<soap-env:envelope xmlns:soap-env=”http://schemas.xmlsoap.org/soap/envelope/”
xmlns:ns1=”http://session.itso”>
<soap-env:body>
<ns1:getforecast>
<ns1:startdate>2004-11-30t13:41:59</ns1:startdate>
<ns1:days>0</ns1:days>
</ns1:getforecast>
</soap-env:body>
</soap-env:envelope>

  对应的应答是:

<?xml version=”1.0″ encoding=”utf-8″?>
<soapenv:envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/”
xmlns:soapenc=”http://schemas.xmlsoap.org/soap/encoding/”
xmlns:xsd=”http://www.w3.org/2001/xmlschema”
xmlns:xsi=”http://www.w3.org/2001/xmlschema-instance”>
<soapenv:body>
<getforecastresponse xmlns=”http://session.itso”>
<getforecastreturn xmlns:ns-239399687=”http://mapping.itso”>
<ns-239399687:condition>sunny</ns-239399687:condition>
<ns-239399687:date>2004-11-30t00:00:00.000z</ns-239399687:date>
<ns-239399687:winddirection>w</ns-239399687:winddirection>
<ns-239399687:windspeed>18</ns-239399687:windspeed>
<ns-239399687:temperaturecelsius>6</ns-239399687:temperaturecelsius>
<ns-239399687:dbflag>1</ns-239399687:dbflag>
</getforecastreturn>
</getforecastresponse>
</soapenv:body>
</soapenv:envelope>

  如果在开启跟踪功能的情况下运行客户机来收集这些输出,那么需要将 days 参数设置为 0,只有这样做,soap 应答才会输出较少的行。但是我们遇到了没有预料到的行为。我们本来期望 getforecastresponse 和以前一样是一个 weather 对象数组,但是它应该只有一个元素,而不是 4 个元素。然而,它被转换成了一个简单的 weather 对象,我们必须根据这种行为进行编码,就像您在最终的示例 php 客户机代码中看到的那样。这与 java 客户机的行为有所不同,在客户机行为中,getforecast 总是返回 weather 对象数组,无论服务器响应中有多少个 weather 对象。soapclient::_gettypes() 输出并没有为我们理解这种差异提供足够的细节,因此我们要求助于 wsdl 文档来了解完整的接口规范。

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