Use Web services in PHP5 to access J2EE applications (1)

Source: Internet
Author: User
Using Web services in PHP5 to access J2EE applications (1) many Web developers like the rich functions and ease of use of PHP, but sometimes they need to access the existing business logic in the J2EE application server. In this article, many Web developers like the rich functions and ease of use of PHP, but sometimes they need to access the existing business logic in the J2EE application server. This article uses some examples to illustrate how to use Web services to access J2EE applications through the new SOAP extension in PHP 5, without having to break away from the PHP environment or learning new programming models.

Introduction to PHP, Web services, and SOAP

This article describes how to access enterprise applications from PHP scripts. You may be a PHP programmer who needs to write code for the department's Web application to access services provided by the company's headquarters in the form of Web services. You may be an experienced J2EE developer and want to know more about PHP and its applications. The example in this article is a program running on IBM WebSphere? Enterprise JavaBean (EJB) on the application server, but this article does not discuss the deployment of Web services. Its main goal is to introduce how to use Web services from PHP, which can be applied to various Web services.

What is PHP?

PHP: Hypertext Preprocessor (Hypertext Preprocessor, PHP) is a popular server-side scripting language used to create dynamic Web content. The PHP interpreter provides source code or compiled binary files for mainstream platforms? Version, Windows? , Mac OS X, and iSeries ?.

Millions of Web servers are running PHP, most of which use PHP 4. PHP 5 launched in July 2004 is gradually being used. PHP 5 has improved the object model, and the underlying memory management has also been re-designed from the perspective of multithreading and performance. However, pay attention to a few changes that cannot be backward compatible. these are recorded in the PHP Manual.

What is Web service technology?

Web services refer to self-contained, modular applications in which clients and services are loosely coupled. For details about Web services, you only need to understand the main technologies in this article:

SOAP (Simple Object Access Protocol) defines the messages transmitted between the client and the server. Messages are in XML format. SOAP is independent of the platform, programming language, network, and transmission layer. This article will discuss SOAP over HTTP.

WSDL (Web service description language) is an XML-based language used to describe Web services. The description includes the service location, format, operation, parameters, and data type.

UDDI (unified description, discovery, and integration) is a method that uses APIs and UDDI Registry to store and retrieve Web service information on the network.

This article includes some examples of SOAP messages and WSDL documents, but does not provide examples of UDDI.

The XMethods website is a useful Web service tool, where you can find a list of publicly available Web services implemented on various server platforms. You can use the examples in this article to easily access the services selected from XMethods.

SOAP and PHP

There are multiple products that allow the use of SOAP in PHP 4 Scripts. The most common products are PEAR: SOAP and NuSOAP. At the time of writing this article, these products still have problems in compatibility with PHP 5, and it is estimated that they will be upgraded soon.

The built-in SOAP extension is added in PHP 5, which is called ext/soap. It is provided as part of PHP, so you do not need to download, install, and manage separate packages. This is the first SOAP implementation written in PHP instead of C, so the author claims that it is much faster.

Because the new extension is one of the complete components of PHP, the relevant documentation is included in the Function Reference section of the PHP Manual. The SOAP reference starts with an important disclaimer:

Warning the extension is EXPERIMENTAL ). The behavior of this extension, including the function name and other content of this extension, may be changed at any time in future PHP versions without notice. Use this extension at your own risk.

Warning seems a bit worrying, but in fact this extension seems to be well supported. Like any new code, this extension also has bugs, but the reported problems are usually fixed quickly. You can view the defect list on the PHP site. We estimate that in future PHP versions, this extension will switch from trial functionality to mainstream functionality.

Install php soap extension

Install and run PHP 5 on the Web server. Our experiment uses PHP 5.0.2, which is the latest version and fixes many errors in the initial PHP 5 version. As mentioned above, ext/soap is provided as part of PHP 5, so you do not need to download it separately, but you may need to make some modifications to it to enable it. What changes need to be made depends on whether you download the source code, compile PHP, or directly download the binary file.

If you download the PHP source code and compile it on your own platform, you may need to build it again because ext/soap is not enabled by default. Repeat the previous build process and add the -- enable-soap option in the configure command.

If you download the binary file of the pre-compiled platform, ext/soap may have been compiled but not loaded. Therefore, you need to update the PHP configuration to load ext/soap. Edit php. ini and find the Dynamic Extensions section. here, add a line of code to automatically load the extension.

On Windows, the code line is:

Extension = php_soap.dll
On UNIX:

Extension = php_soap.so
If you have not loaded any optional extensions before, you may also need to set the extension_dir command to point to the directory containing the extension Library (including php_soap), for example:

Extension_dir = "C:/php/ext/" (use a forward slash on Windows)
Do not place the directory information in the extension command. you can use extension_dir if necessary.

For Windows, you can download the other two binary packages. The Windows installation package does not contain any extensions. Therefore, you must use the Windows zip package, which contains ext/soap.

Note that ext/soap depends on the GNOME xml Library, which must be 2.5.4 or later. If the version is not high enough, you can install libxml2 from xmlsoft.

Finally, ext/soap has its own configuration in php. ini. after the configuration is complete, ext/soap is shown as follows:

[Soap]
; Enables or disables WSDL caching feature.
Soap. wsdl_cache_enabled = 1
; Sets the directory name where SOAP extension will put cache files.
Soap. wsdl_cache_dir = "/tmp"
; (Time to live) Sets the number of second while cached file will be used
; Instead of original one.
Soap. wsdl_cache_ttl = 86400
This configuration controls the WSDL cache feature of the SOAP extension. By default, the WSDL description file is cached in the/tmp directory within 24 hours (86400 seconds. We will discuss this later. now we need to set soap. wsdl_cache_enabled = 0. Otherwise, you will encounter some inexplicable behavior during code development. After the development, remember to open the WSDL cache to make the code run faster.

For reference, we will use ext/soap in two environments:

Linux Centos 3.3 (free rebuilding of Red Hat EL 3), Apache 2.0.47, PHP 5.0.2, and libxml2 to 2.6.12 need to be upgraded.
Windows XP SP1, Apache 2.0.46, PHP 5.0.2 Binary compressed package, libxml2 2.6.11.

These descriptions also apply to other configurations.

Weather Forecast application

The Web service we want to access from PHP is a weather forecast application. This is a sample Application developed in WebSphere Version 5.1 Application Developer 5.1.1 Web Services Handbook. For more information about downloading the Weather Forecast application, see the download section later in this article. This book has designed several different scenarios, but we only consider one. in this book, this scenario is called "bottom-up development, use HTTP transmission and SOAP messages to generate Web services from the session EJB ". From the bottom up, Web services are packaged around existing enterprise applications.

Figure 1. weather forecast application

The main components of the Weather Forecast application shown in Figure 1 include:

Background WEATHER database for WEATHER prediction. The weather forecast information includes:

Wind direction, eight orientations
Wind speed, km/hour
Temperature, degree Celsius
Weather conditions: clear, sometimes overcast, overcast, rainy, or rainstorm
Date

The WeatherPredictor class is used to access the WEATHER database. If the database does not have a forecast that applies to the request date, WeatherPredictor randomly generates a weather forecast (different from the actual weather forecast) and saves it to the database.

The business logic is provided by WeatherForecastEJB session bean and published as a Web service. It provides three operations:

GetDayForecast returns the weather forecast for a certain day.

GetForecast returns the weather forecast for a certain period of time.

GetTemperatures returns the temperature forecast for a certain period of time.

All the elements required to deploy the session bean as a Web service are generated by the Web service wizard of WebSphere Studio Application Developer and generated as an ItsoWebService2RouterWeb project. Vroservlet servlet is a bridge between SOAP messages and EJB containers. you need to configure and deploy a vroservlet servlet to make Weather service available through URL ItsoWebService2EJBRouterWeb/services/WeatherServiceEJB. The WSDL document itso. session. WeatherForecastEJB. wsdl is in the ItsoWebService2EJBRouterWeb/wsdl directory.

Java client is one of the multiple Weather Service clients developed in this book. WeatherClientEJB in the ItsoWebService2EJBClient project is a simple Java servlet that calls getForecast Web service operations. The typical running result is as follows:


Figure 2. Java WeatherClient
The next step is to create equivalent client functions in PHP.

You do not need to run this example yourself. you can create a PHP client for the services selected from the XMethods website.

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.