Home php教程 php手册 PHP5中使用Web服务访问J2EE应用程序(2)

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

Jun 21, 2016 am 09:08 AM
soap soapclient weather

j2ee|php5|web|web服务|程序|访问

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


  观察 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 文档来了解完整的接口规范。



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP and SOAP to implement Web service invocation and development How to use PHP and SOAP to implement Web service invocation and development Jun 25, 2023 am 09:59 AM

In the field of Web development, Web services are a very important technology that enable different applications to communicate with each other to build more complex and powerful systems. In this article, we will take an in-depth look at how to use PHP and SOAP to implement web service invocation and development. SOAP (SimpleObjectAccessProtocol) is an XML-based protocol used for information exchange between different applications. SOAP is an important Web service standard

PHP and SOAP: How to achieve synchronous and asynchronous processing of data PHP and SOAP: How to achieve synchronous and asynchronous processing of data Jul 28, 2023 pm 03:29 PM

PHP and SOAP: How to implement synchronous and asynchronous processing of data Introduction: In modern web applications, synchronous and asynchronous processing of data are becoming more and more important. Synchronous processing refers to processing only one request at a time and waiting for the completion of the request before processing the next request; asynchronous processing refers to processing multiple requests at the same time without waiting for the completion of a certain request. In this article, we will introduce how to use PHP and SOAP to achieve synchronous and asynchronous processing of data. 1. Introduction to SOAP SOAP (SimpleObject

PHP and SOAP: How to Implement Remote Procedure Calls (RPC) PHP and SOAP: How to Implement Remote Procedure Calls (RPC) Jul 29, 2023 pm 02:45 PM

PHP and SOAP: How to implement Remote Procedure Call (RPC) Introduction: In recent years, with the rise of distributed systems, Remote Procedure Call (RPC) has been widely adopted in Web development. This article will introduce how to implement RPC using PHP and SOAP, and demonstrate its usage through code examples. 1. What is remote procedure call (RPC)? Remote procedure call (RemoteProcedureCall, RPC) is a communication

SOAP Protocol Guide in PHP SOAP Protocol Guide in PHP May 20, 2023 pm 07:10 PM

With the continuous development of Internet technology, more and more enterprise-level applications need to provide interfaces to other applications to realize the interaction of data and business. In this case, we need a reliable protocol to transmit data and ensure data integrity and security. SOAP (Simple Object Access Protocol) is an XML-based protocol that can be used to implement communication between applications in a Web environment. As a popular web programming language, PHP

Parsing SOAP messages using Python Parsing SOAP messages using Python Aug 08, 2023 am 09:27 AM

Parsing SOAP messages using Python SOAP (Simple Object Access Protocol) is an XML-based remote procedure call (RPC) protocol used to communicate between different applications on the network. Python provides many libraries and tools to process SOAP messages, the most commonly used of which is the suds library. suds is a SOAP client library for Python that can be used to parse and generate SOAP messages. It provides a simple and

How to compress and decompress data using PHP and SOAP How to compress and decompress data using PHP and SOAP Jul 29, 2023 pm 12:28 PM

How to use PHP and SOAP to compress and decompress data Introduction: In modern Internet applications, data transmission is a very common operation. However, with the continuous development of Internet applications, the increase in data volume and the requirements for transmission speed, reasonable The use of data compression and decompression techniques has become a very important topic. In PHP development, we can use the SOAP (SimpleObjectAccessProtocol) protocol to achieve data compression and decompression. This article will show you how to

How to use PHP and SOAP to deploy and publish web services How to use PHP and SOAP to deploy and publish web services Jul 28, 2023 pm 01:57 PM

How to use PHP and SOAP to deploy and publish Web services Introduction: In today's Internet era, the deployment and publishing of Web services has become a very important topic. PHP is a popular server-side programming language, while SOAP (Simple Object Access Protocol) is an XML protocol used for communication between web services. This article will introduce you to how to use PHP and SOAP to deploy and publish web services, and provide some code examples.

A complete guide to building web-based applications with PHP and SOAP A complete guide to building web-based applications with PHP and SOAP Jul 30, 2023 am 10:25 AM

A complete guide to building web-based applications using PHP and SOAP In today's Internet era, web-based applications have become an important tool for managing and interacting with data. As a powerful development language, PHP can be seamlessly integrated with other technologies, while SOAP (Simple Object Access Protocol), as an XML-based communication protocol, provides us with a simple, standard and extensible method to build Web services. . This article will provide you with

See all articles