Using Web Services in PHP5 to access Java-EE applications (2)

Source: Internet
Author: User
Tags date array constructor datetime soap web services xmlns wsdl
J2ee|php5|web|web Services | programs | accessing PHP Weather Client

This section will build our own PHP Weather client. Here are some snippets of code that suggest downloading the full client and WSDL file.

The Ext/soap class used to represent the Weather Service is soapclient. As we talked about the Weather Forecast application, we know that the application server is in http://host:port/ItsoWebServer2RouterWeb/wsdl/itso/session/ WSDL is provided in the WEATHERFORECASTEJB.WSDL. We use the default port and work on the computer that is the server so that we can create the first soapclient by looking for the WSDL file:

<?php
$soapClient = new SoapClient ("http://localhost:9080/").
"ITSOWEBSERVICE2ROUTERWEB/WSDL/ITSO/SESSION/WEATHERFORECASTEJB.WSDL");
? >
Note that because Ext/soap is built in, you do not need any include or require statements before referencing soapclient.

The client is now instantiated, and the Weather service is contacted, and its getforecast operation is invoked. When using SoapClient in WSDL mode, Ext/soap has a good feature that it can refer directly to a remote operation as if it were a function of soapclient itself. But it takes a bit of skill to create input parameters. Ext/soap can provide an array of actions and parameters found from the WSDL:

$functions = $soapClient->__getfunctions ();
Print_r ($functions);
$types = $soapClient->__gettypes ();
Print_r ($types);
Just show the results related to getforecast and reformat the results for easy reading, so we see the following code:

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 actually does not define the Getforecast class for us, we must create an array of input parameters that the operation requires:

$getForecastParam = Array (' StartDate ' =>time (), ' Days ' => 3);
The operation is then invoked like the SoapClient method:

$forecastResponse = $soapClient->getforecast ($getForecastParam);
Finally we get the returned Getforecastresponse object, which itself is an array of Weather objects, and then display the results in the table:

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>";
The PHP client has the same output as the Java client, so we know that San Jose will not snow during Christmas ...

Figure 3. PHP weatherclient


Observe the SOAP stream

We have successfully contacted the Weather service and shown the results. But what if there is an error and you don't get the results you expect? Ext/soap can show the SOAP messages exchanged between the client and the server to help us identify the problem.

You use tracing only when you create soapclient using the trace option. We set the trace option in the options array parameter to pass the parameter to the SoapClient constructor. We change the constructor usage to:

$soapClient = new SoapClient ("http://localhost:9080/").
"ITSOWEBSERVICE2ROUTERWEB/WSDL/ITSO/SESSION/WEATHERFORECASTEJB.WSDL",
Array (' Trace ' => 1));
and call the trace method after calling Goforecast:

echo "Request: <br>", Htmlspecialchars ($soapClient->__getlastrequest ()), "<br>";
echo "Response: <br>", Htmlspecialchars ($soapClient->__getlastresponse ()), "<br>";
Be sure to encode the trace output with the Htmlspecialchars built-in function because it converts the SOAP XML specifier into a special character, such as, so that the browser can be prevented from interpreting it as a marker.

The following is the trace output of a request:

<?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>
The corresponding answer is:

<?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> </ns-239399687:windSpeed>
<ns-239399687:temperatureCelsius> 6 </ns-239399687:temperatureCelsius>
<ns-239399687:dbflag> 1 </ns-239399687:dbflag>
</getForecastReturn>
</getForecastResponse>
</soapenv:Body>
</soapenv:Envelope>
If you run the client to collect these outputs when tracing is turned on, you need to set the days parameter to 0, and the SOAP answer will only output fewer rows. But we have encountered unexpected behavior. We had expected Getforecastresponse to be an array of Weather objects as before, but it should have only one element, not 4 elements. However, it has been converted to a simple Weather object, and we have to encode it based on this behavior, as you can see in the final sample PHP client code. This differs from the behavior of the Java client, in which Getforecast always returns an array of Weather objects, regardless of how many Weather objects are in the server response. The soapclient::_gettypes () output does not provide enough detail for us to understand this discrepancy, so we need to turn to the WSDL document to understand the complete interface specification.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.