J2ee|php5|web|web Services | programs | Access to many Web developers like PHP's richness and ease of use, but sometimes they need access to the business logic that is already in the Java application Server. This article will show you how to use a WEB service to access a Java-EE application using a new SOAP extension in PHP 5 without having to detach from the PHP environment or learn a new programming model.
Introduction to PHP, Web services, and SOAP
This article describes how to access an enterprise application from a PHP script. You may be a PHP programmer who needs to write code for a departmental Web application to access services provided by the company's headquarters as a Web service. You may be an experienced Java developer who wants to know a little more about PHP and its applications. The example in this article is a run at 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 explain how to use Web services from PHP, which can be applied to a variety of Web service implementations.
What is PHP?
Php:hypertext Preprocessor (Hyper-text preprocessor, PHP) is a popular server-side scripting language for creating dynamic Web content. The PHP interpreter provides source code or compiled binaries for the mainstream platform, which includes most Linux? Versions, Windows?, Mac OS X, and iSeries?
There are indeed millions of WEB servers running PHP, most of them using PHP 4. PHP 5, launched in July 2004, is gradually being adopted. PHP 5 improves the object model, and the underlying memory management is redesigned from a multi-threaded and performance perspective. However, you need to be aware of a handful of changes that are not backwards compatible and are documented in the PHP manual.
What is WEB services technology?
WEB services refer to self-contained, modular applications in which clients and services are loosely coupled. For more information about WEB services, for this article, you only need to know the main technologies:
SOAP (Simple Object Access Protocol) defines the messages that are passed between the client and the server. The message is in XML format. SOAP is independent of the platform, programming language, network, and Transport layer. This article discusses SOAP on HTTP.
The WSDL (Web Service Description Language) is an xml-based language for describing Web services, which includes the location, format, operations, parameters, and data types of the service.
UDDI (Unified description, Discovery, and integration) is an API and UDDI Registry implementation that provides a way to store and retrieve Web service information on a network.
This article includes some examples of SOAP messages and WSDL documents, but does not provide examples of UDDI.
The XMethods Web site is a useful Web service tool where you can find a list of publicly available Web services implemented on a variety of server platforms. You can use the examples in this article to easily access the services that you select from XMethods.
SOAP and PHP
There are a variety of products that allow SOAP to be used in PHP 4 scripts, and the most common products are pear::soap and nusoap. In writing this article, these products have problems with the compatibility of PHP 5, which is expected to be upgraded soon.
The built-in SOAP extensions are added to PHP 5, which we call Ext/soap. It is provided as part of PHP, so you do not need to download, install, and manage individual packages. This is the first SOAP implementation written for PHP in C instead of PHP, so the authors claim it is much faster.
Because the new extension is one of the complete components of PHP, the related documentation is included in the Function Reference section of the PHP manual. The SOAP reference begins with an important disclaimer:
Warning: This extension is experimental (experimental). The behavior of this extension, including the function name and other content about this extension, may change at any time in future PHP versions without notice. The risk of using the extension is conceited.
The warning looks a little bit worrying, but in fact the extension seems to have been well supported. As with any new code, the extension is flawed, but the reported problem is often corrected quickly. A list of bugs can be seen on the PHP site. We estimate that in future versions of PHP, the extension will shift from experimental functionality to mainstream functionality.
Installing the PHP SOAP extension
PHP 5 should be installed and running on the WEB server. Our experiment uses PHP 5.0.2, which is now the latest version, fixes a lot of bugs in the initial version of PHP 5. As mentioned above, Ext/soap is provided as part of PHP 5, so there is no need to download it separately, but you may need to make some modifications to enable it. The changes you need to make depend on whether you are downloading the source code, compiling PHP yourself, or downloading the binaries directly.
If you download the PHP source code and compile it on your own platform, you may need to rebuild it because Ext/soap is not enabled by default. Repeat the previous build process and add the--ENABLE-SOAP option to the Configure command.
If you download a precompiled platform binary, Ext/soap may have been compiled but not loaded, so you need to update your PHP configuration to load the Ext/soap. Edit php.ini and locate the Dynamic Extensions section, where you add a line of code to automatically load the extension.
On Windows, this line of code is:
Extension=php_soap.dll
On UNIX are:
Extension=php_soap.so
If you have not previously loaded any optional extensions, you may also want to set the EXTENSION_DIR directive to point to the directory that contains the extension library, including the Php_soap, for example:
Extension_dir= "c:/php/ext/" (using a forward slash on Windows)
Do not put directory information into the extension instruction, you can use Extension_dir if necessary.
For Windows, you can download the other two binary packages. The Windows Installer package does not contain any extensions, so to use the Windows Zip compression package, this package contains Ext/soap.
Note that Ext/soap relies on the GNOME XML library, which must use 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 section in PHP.ini, and after the configuration is complete, Ext/soap is as follows:
[SOAP]
; Enables or disables WSDL caching feature.
Soap.wsdl_cache_enabled=1
; Sets the directory name where SOAP extension'll put cache files.
soap.wsdl_cache_dir= "/tmp"
; (Time to live) Sets the number of second while cached file would be used
; Instead of original one.
soap.wsdl_cache_ttl=86400
This configuration controls the WSDL caching characteristics of the SOAP extension. By default, the WSDL description file is slow to exist in the/tmp directory within 24 hours (86,400 seconds). We'll talk about this later, and now we're going to set the soap.wsdl_cache_enabled=0, otherwise you're going to run into some weird behavior when you're developing code. Once you've finished developing, remember to turn on the WSDL cache to make your code run faster.
For ease of reference, we will use EXT/SOAP in two environments:
Linux Centos 3.3 (free rebuild version of Red Hat EL 3), Apache 2.0.47, PHP 5.0.2, need to upgrade LIBXML2 to 2.6.12.
Windows XP SP1, Apache 2.0.46, PHP 5.0.22 compression Pack, LIBXML2 2.6.11.
These instructions also apply to other configurations.
Weather Forecast Application
The Web service we want to access from PHP is a weather application. This is the sample application developed in the WebSphere Version 5.1 application Developer 5.1.1 Web Services Handbook. Download the sample Weather Forecast application, see the Download section later in this article. This book is designed for several different scenarios, but we only consider one in which the scenario is called "bottom-up development, using HTTP transport and SOAP messages to generate WEB services from a session EJB." Here, bottom-up means that WEB services are packaged around existing enterprise applications.
Figure 1. Weather Forecast Application
The main components of the Weather Forecast application that are identified in Figure 1 include:
Forecast weather in the background WEATHER database. The information in the weather forecast includes:
Wind, eight directions.
Wind speed, km/h
Temperature, degree Celsius
Weather conditions: Clear, sometimes cloudy, overcast, rain, rainstorm
Date
The Weatherpredictor class is used to access the WEATHER database. If the database does not have a forecast that applies to the requested date, then Weatherpredictor will randomly generate a weather forecast (different from the actual weather forecast) and save it to the database.
The business logic is provided by the WEATHERFORECASTEJB session Bean and exposed as a WEB service, which provides three actions:
Getdayforecast returns the weather forecast for one day.
Getforecast returns the weather forecast for a certain period.
Gettemperatures returns the temperature forecast for a certain period.
All the elements required to deploy this session bean as a Web service are generated by the Web Service Wizard of the WebSphere Studio application Developer and are generated as itsowebservice2routerweb projects. The router servlet is a bridge between the SOAP message and the EJB container, which requires the configuration and deployment of the router servlet and the URL ITSOWEBSERVICE2EJBROUTERWEB/SERVICES/WEATHERSERVICEEJB to enable We The Ather service is available. The WSDL document is itso.session.WeatherForecastEJB.wsdl in the ITSOWEBSERVICE2EJBROUTERWEB/WSDL directory.
The Java client is one of several Weather Service clients developed in this book. The WEATHERCLIENTEJB in the Itsowebservice2ejbclient project is a simple Java servlet that invokes the Getforecast Web service operation. Typical results of the operation are as follows:
Figure 2. Java weatherclient
The next step is to create the equivalent client functionality in PHP.
Reading this article does not require you to run this example, you can set up a PHP client for the service selected from the XMethods Web site.
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.