PHP5中使用Web服務訪問J2EE應用程式(2)

來源:互聯網
上載者:User
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 文檔來瞭解完整的介面規範。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.