PHP is a top Web development language, and Java™ technology is popular for business application development. Thus, to use the strengths of each language on the AIX® Version 5.3 operating system, the PHP Java Bridge has been developed. This series shows AIX 5.3 developers how to integrate both PHP and Java technology in their Web application development.
To demonstrate this, you will build a simple Survey application that follows a typical development process, including:
- Developing the main Java application
- Exposing the Java application through servlets as a Java-based Web application
- Adding support for storing information within a database
- Exposing the original application as Web service and providing a PHP interface to the application
- Redeveloping the PHP interface using the dedicated PHP Java Bridge
The series is split into six parts:
- Part 1 looks at the application and sets up an environment ready for building Java applications and serving Java-based Web applications using Tomcat.
- Part 2 covers the main application code and the development of a simple Java Servlet to provide a Web interface to the information.
- Part 3 connects the core application to a DB2® database for the storage of the survey questions and responses.
- Part 4 converts the original application into one that can be accessed as a Web service, and it provides the base for the PHP interface.
- Part 5 builds the PHP interface to the Java application by using the PHP Java Bridge.
- Part 6 redevelops the application to make use of the PHP Java Bridge in place of the Web service interface.
Revisiting the Web service solution
In the fourth installment of this series (see Resources), you extended the original Java application and exposed it through a Web service. There are many advantages to exposing an application with Web services, but the primary one is to improve interoperability.
Web services are compatible with a wide range of platforms, languages, and environments. Once you have exposed the application through Web services, you can access it from almost any machine using a wide range of languages, including Perl, Python, JavaScript, Java and, of course, PHP.
Web services expose the Web service functions through a URL—for
example, your getSurveyQuestionCountReponse() method.
You can then encode the information that has been sent to the server and returned
by the client as an XML message.
You can best see this directly by accessing the Web service installed into the Axis Web service server. For example, by accessing the http://localhost:8080/axis/services/SQWSSOAP?method=getSurveyQuestionCount URL, you should get the XML message returned by the Simple Object Access Protocol (SOAP) service shown here in Listing 1.
Listing 1. XML message returned by the SOAP service
<soapenv:Envelope>
<soapenv:Body>
<getSurveyQuestionCountResponse>
<out>2</out>
</getSurveyQuestionCountResponse>
</soapenv:Body>
</soapenv:Envelope>
|
As a standard, you can use any SOAP client to obtain the same information. Listing 2 provides a simple client in Perl that demonstrates the interoperability.
Listing 2. Simple client in Perl
use SOAP::Lite;
my $client = SOAP::Lite->new(proxy =>
'http://localhost:8080/axis/services/SQWSSOAP');
print "Questions: ",$client->getSurveyQuestionCount()->result,"\n";
|
The same process in Listing 3 works the same way for PHP—setting the remote end point for the Web service base. You then just have to call the correct remote service to get the count of the number of questions in the survey.
Listing 3. Setting the remote end point in PHP
<?
require_once('nusoap.php');
$client=new soapclient('http://localhost:8080/axis/services/SQWSSOAP');
if (isset($fault)) {
print "Error: ". $fault;
}
$count = $client->call('getSurveyQuestionCount',array());
print "Questions: " . $count . "\n";
?>
|
The interoperability of Web services in this situation comes at a price. For your original purpose of writing a better Web wrapper around the application, you can now use the application from a variety of sources, other applications, and environments. The process of opening a new network connection and converting the content to and from XML for the SOAP message is an additional overhead in an application and environment, so you want to be as quick as possible.
The overhead involved in servicing the query in this way can be significant. SOAP and other Remote Procedure Call (RPC) technology was not designed with performance in mind. The compatibility, interoperability, and open standards were the key elements.
One of the original aims of this series was to develop an efficient Web interface for a Java application. The application you've used has been relatively simple, but it is not difficult to imagine hundreds of requests a second from using it. You want the best performance possible but, keep in mind, accessing your Java application through a SOAP service with a PHP client is not going to achieve that for you.
The PHP Java Bridge alternative
The PHP Java Bridge is an optimized XML-based protocol that enables you to directly use Java classes from within your PHP applications. Although in principle it works similarly to SOAP, it is in fact significantly more efficient than the SOAP method.
The system works by using a JSR 223 interface. The PHP component is able to communicate with any ECMA-335 back-end system, which includes Java technology, Java-based extension interfaces such as JPython and JRuby, .NET applications employing the CLR (Common Language Runtime), and even .NET compatible solutions such as Mono.
The process is two-way: a PHP script can call a Java/.NET class, or a Java component can call a PHP class or script. The bridge can even be integrated into the solution to allow the backends (Apache/IIS and PHP, or .NET, or Java/Tomcat) to be automatically started when a class is requested.
The system works by accessing a remote class on the Tomcat application server. When you run the PHP application, it loads the PHP Java Bridge code from the remote Java application serving platform. This initializes the system and lets the PHP script know how to communicate with the remote Java classes on the application server.
Obviously, for you to be able to access your Java classes using the PHP Java Bridge, you must have Apache and PHP installed before installing the PHP Java Bridge.
To install Apache httpd:
- You need a c compiler and an Apache
httpd source distribution. If you do not have a c compiler on your system, you can
download the open source GNU c compiler in rpm format from IBM (see Resources). You will ultimately want the following modules from this
site (see Listing 4):
Listing 4. The necessary modulesgcc V4.0.0 for AIX V5.3 (gcc-4.0.0-1.aix5.3.ppc.rpm) gcc-c++ V4.0.0 for AIX V5.3 (gcc-cplusplus-4.0.0-1.aix5.3.ppc.rpm) libgcc V4.0.0 for AIX V5.3 (libgcc-4.0.0-1.aix5.3.ppc.rpm) libstdc++ V4.0.0 for AIX V5.3 (libstdcplusplus-4.0.0-1.aix5.3.ppc.rpm) libstdc++-devel V4.0.0 for AIX V5.3 (libstdcplusplus-devel-4.0.0-1.aix5.3.ppc.rpm) make v3.80 (make-3.80-1.aix5.1.ppc.rpm) bison v1.875 (bison-1.875-3.aix5.1.ppc.rpm) bzip v1.0.2 (bzip2-1.0.2-3.aix5.1.ppc.rpm) expat v1.95.7 (expat-1.95.7-4.aix5.1.ppc.rpm) flex v2.5.4a (flex-2.5.4a-6.aix4.3.ppc.rpm) freetype2 v2.1.7 (freetype2-2.1.7-5.aix5.1.ppc.rpm) libjpeg v6b (libjpeg-6b-6.aix5.1.ppc.rpm) libpng v1.2.8 (libpng-1.2.8-5.aix5.1.ppc.rpm) xpm v3.4k (xpm-3.4k-7.aix5.1.ppc.rpm) zlib v1.2.3 (zlib-1.2.3-3.aix5.1.ppc.rpm)
- Download them to your system and install each of them as root with the
following command (example with gcc):
# rpm –Uvh gcc-4.0.0-1.aix5.3.ppc.rpm
If you want to be clever, use a shell script to install them all:# for I in *.rpm; do rpm –Uvh $I done
- You also need to install the Base Application Development Math Library (bos.adt.libm) from the AIX distribution if it's not installed already. You can use smit to do this.
- After all this, download the Apache 2.2.2 source httpd-2.2.2.tar.gz from http://archive.apache.org/dist/httpd/.
- Extract the archive:
# gunzip –C httpd-2.2.2.tar.gz | tar xvf -
Change to the distribution directory, run the configuration script, compile, and install:# cd httpd-2.2.2 # ./configure -C –enable-so –prefix=/usr/local/apache2 # make # make install
This installs Apache httpd into /usr/local/apache2. For more information on this process as well as configuring httpd, please consult Nigel Griffiths excellent article on the AIX Wiki (see Resources).
Configuring Apache with Web servers and other details is beyond the scope of this article. See the resources for some help on configuring a basic Apache installation.
You should install PHP from the source code. To do this:
- Download the PHP V5.2.3 source code from the PHP website (see Resources).
- Extract the PHP archive:
# gunzip –c php-5.2.3.tar.gz | tar –xf –
- Change to the PHP archive directory:
# cd php-5.2.3
- Now run configure, specifying the location of the apxs2 command. The
apxs2/opt/command provides information to the PHP configuration script about the location of Apache and how PHP should be configured:# ./configure –with-apx2=/usr/local/apache2/bin/apxs
- Once the configure process has completed, run
maketo build PHP. Because PHP’s configure is open source, you need to use the open sourcemakecommand to build the executable:# /opt/freeware/bin/make
- Install the new binaries into Apache (do not use
make install):# cp .libs/libphp5.so /usr/local/apache2/modules # cp php.ini-recommended /usr/local/apache2/conf/php.ini
The PHP installer should do everything for you to enable PHP. Now it’s time to install the PHP Java Bridge.
Installing the PHP Java Bridge
Installing the PHP Java Bridge is straightforward, and it requires a combination of installing the core JavaBridge.war Web archive and making a few modifications to the running system to ensure that you can access the bridge from the remote host.
To install the PHP Java Bridge:
- Download the PHP Java Bridge package (see Resources).
- Extract the contents of the package using unzip:
$ unzip php-java-bridge_4.2.0_j2ee.zip
- Copy the Web deployment archive into the Tomcat installation directory:
$ cp php-java-bridge-4.2.0_j2ee/JavaBridge.war /usr/local/tomcat/webapps/
- If it is already started, shutdown Tomcat, and then restart it. This extracts
the contents of the Web archive and deploys it for use (see
Listing 5).
Listing 5. Extracting the contents of the Web archive$ cd /usr/local/tomcat $ bin/shutdown.sh $ bin/startup.sh
By default, the PHP Java Bridge is configured not to allow connections from non-local servers. This means that if you want to deploy your application across different servers, then your connection might well be blocked. - To change this configuration, you need to alter the parameters used for the JavaBridge application. To do this, edit the web.xml file within the JavaBridge deployed Web application directory. This should be the webapps/JavaBridge/WEB-INF/web.xml file within the Tomcat installation directory.
- You need to uncomment this block to allow remote access to your Java classes (see
Listing 6).
Listing 6. Uncommenting the block to allow remote access to Java classes<init-param> <param-name>promiscuous</param-name> <param-value>On</param-value> </init-param>
- You must now shutdown and restart the Tomcat server (see
Listing
7).
Listing 7. Shutting down and restarting the Tomcat server$ cd /usr/local/tomcat $ bin/shutdown.sh $ bin/startup.sh
Testing your PHP Java Bridge installation
Your PHP Java Bridge installation works by importing a PHP include file directly from the remote Java application server. Unfortunately, just as the PHP Java Bridge wouldn't allow remote access, default PHP installations ban the remote inclusion of files, as it is a potential security risk.
To control this feature, you must edit the php.ini file. A copy of this file should be located in /usr/local/lib/php.ini. If it is not there, locate a copy of the file within the PHP source code. The php.ini-recommended is the best basis for a new configuration file.
You need to enable two PHP configuration parameters,
allow_url_fopen, which allows you to open a remote file
as if it were a local one, and allow_url_include, which
enables you to include remote PHP files to import code.
You should find the former option in a section called Fopen wrappers, as shown in Listing 8.
Listing 8. Fopen wrappers section
;;;;;;;;;;;;;;;;;;
; Fopen wrappers ;
;;;;;;;;;;;;;;;;;;
; Whether to allow the treatment of URLs (like http:// or ftp://) as files.
allow_url_fopen = On
allow_url_include = On
|
Change the configuration to On and add the
allow_url_include option.
Once this has been configured, restart Apache:
$ /usr/local/apache2/bin/apachectl restart |
Then create a file on your Web server like the one shown here in Listing 9.
Listing 9. Creating a file on your Web server
<?
require_once("http://sulaco.mcslp.pri:8080/JavaBridge/java/Java.inc");
$System = new Java("java.lang.System");
print_r($System->getProperties());
?>
|
The first line accesses the remote PHP Java Bridge running on the Tomcat application server. By loading the include file directly from the remote server, the required classes know which server to communicate with when accessing Java classes.
The second line in Listing 9 above creates a new instance of a Java class as a PHP object. In this case, you've used the java.lang.System class, which can be used to dump properties about the Java installation.
The last line dumps out the configured properties.
If you now access the PHP script on your Apache Web server, you should get output similar to that shown here in Listing 10.
Listing 10. Output
[[o:Properties]:"{java.runtime.name=Java(TM) 2 Runtime Environment, Standard Edition,
sun.boot.library.path=/usr/local/lib:/usr/local/apr/lib:/System/Library/Frameworks/
JavaVM.framework/Versions/1.5.0/Libraries, java.vm.version=1.5.0_07-87,
shared.loader=, awt.nativeDoubleBuffering=true, gopherProxySet=false,
java.vm.vendor="Apple Computer, Inc.", java.vendor.url=http://apple.com/,
path.separator=:, tomcat.util.buf.StringCache.byte.enabled=true,
java.util.logging.config.file=/Data/Dev/apache-tomcat-6.0.10/conf/logging.properties,
java.vm.name=Java HotSpot(TM) Client VM, file.encoding.pkg=sun.io, user.country=US,
sun.os.patch.level=unknown, java.vm.specification.name=Java Virtual Machine
Specification, user.dir=/Data/Dev/apache-tomcat-6.0.10, java.runtime.version=
1.5.0_07-164, java.awt.graphicsenv=apple.awt.CGraphicsEnvironment, java.endorsed.
dirs=/Data/Dev/apache-tomcat-6.0.10/endorsed, os.arch=i386, java.io.tmpdir=/Data/
Dev/apache-tomcat-6.0.10/temp, line.separator= , java.vm.specification.vendor=Sun
Microsystems Inc., java.naming.factory.url.pkgs=org.apache.naming, java.util.
logging.manager=org.apache.juli.ClassLoaderLogManager, os.name=Mac OS X, sun.jnu.
encoding=MacRoman,java.library.path=/usr/local/lib:/usr/local/apr/lib:.:/Library/
Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java, java.specification.
name=Java Platform API Specification, java.class.version=49.0, sun.management.
compiler=HotSpot Client Compiler, os.version=10.4.9, http.nonProxyHosts=mcslp.pri|
*.mcslp.pri|192.168.0.1|sendit.com|*.sendit.com, user.home=/Users/mc, catalina.
useNaming=true, user.timezone=Europe/London, java.awt.printerjob=apple.awt.
CPrinterJob, file.encoding=MacRoman, java.specification.version=1.5, catalina.home=/
Data/Dev/apache-tomcat-6.0.10,java.class.path=:/Data/Dev/apache-tomcat-6.0.10/bin/
bootstrap.jar:/Data/Dev/apache-tomcat-6.0.10/bin/commons-logging-api.jar:/System/
Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/.compatibility/
14compatibility.jar,user.name=root, java.naming.factory.initial=org.apache.naming.
java.javaURLContextFactory,package.definition=sun.,java.,org.apache.catalina.,
org.apache.coyote.,org.apache.tomcat.,org.apache.jasper., java.vm.specification.
version=1.0, java.home=/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/
Home, sun.arch.data.model=32, user.language=en, java.specification.vendor=Sun
Microsystems Inc., awt.toolkit=apple.awt.CToolkit, java.vm.info=mixed mode, sharing,
java.version=1.5.0_07,java.ext.dirs=/Library/Java/Extensions:/System/Library/Java/
Extensions:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/ext,
sun.boot.class.path=/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/
Classes/classes.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/
Classes/ui.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/
laf.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/
sunrsasign.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/
jsse.jar:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/jce.jar:/
System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/charsets.jar,
server.loader=, java.vendor=Apple Computer, Inc., catalina.base=/Data/Dev/
apache-tomcat-6.0.10, file.separator=/, java.vendor.url.bug=http://developer.
apple.com/java/, common.loader=${catalina.home}/lib,${catalina.home}/lib/*.jar,
sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, mrj.version=1040.1.5.0
_07-164, socksNonProxyHosts=mcslp.pri|*.mcslp.pri|192.168.0.1|sendit.com|
*.sendit.com, package.access=sun.,org.apache.catalina.,org.apache.coyote.,org.apache.
tomcat.,org.apache.jasper.,sun.beans., ftp.nonProxyHosts=mcslp.pri|*.mcslp.pri|
192.168.0.1|sendit.com|*.sendit.com, sun.cpu.isalist=}"]
|
Although it's probably not clear from that output, what you have just done is
created an instance of Java code within the PHP script, and then executed the
getProperties() method on that code. The way you
executed the code was PHP-like, even though the result was to execute the method
of a Java class. Furthermore, the Java class itself was on a remote
server—or least it could have been if that was how you wanted to
execute the code.
In the next and final part of the series, you'll use this functionality to build a PHP application that uses the original Java classes directly.
In this article, you looked at how to access the SOAP-enabled deployment of the Java classes you created in the fourth part of the series. The process for creating a Web services interface is complex and hardly a straightforward process; however, the Web services interface does allow access to information from any Web services client.
But Web services have their place, and if you want a high performance solution, then there might be better alternatives to the Web service route, and one of those is the PHP Java Bridge. Although the installation of the PHP Java Bridge was complex, once set up, the actual code for accessing a remote Java class was much more straightforward. In just three lines, you could access a remote Java class—it took a few hundred to achieve the same process by using Web services and a PHP client to access it.
Learn
-
Develop
with Java and PHP technology on AIX Version 5.3:
Check out other parts in this series.
-
IBM developerWorks Java zone:
More information on Java development can be located here.
- "Introduction to Java Servlet technology"
(Roy Miller, developerWorks, December 2004): Read this article for an introduction
to servlet technology.
- "Run PHP
applications in Apache Geronimo"
(Tyler Anderson, developerWorks, February 2006): This tutorial provides basic
information on developing using PHP and Java technology.
-
PHP Java Bridge: For more
information on PHP Java Bridge, be sure to check out sourceforge.net.
-
DB2 Version 9 system
requirements and links:
Get all the information you need to install DB2 Version 9.
-
Popular content:
See what AIX and UNIX® content your peers find interesting.
- Check out other articles and tutorials written
by Martin Brown:
- Check out other articles and tutorials written
by Doug Monroe:
-
AIX and
UNIX®
:
The AIX and UNIX developerWorks zone provides a wealth of information relating to
all aspects of AIX systems administration and expanding your UNIX skills.
-
New to AIX and UNIX?:
Visit the "New to AIX and UNIX" page to learn more about AIX and UNIX.
-
AIX
5L™ Wiki:
A collaborative environment for technical information related to AIX.
-
AIX Wiki:
Visit the AIX Wiki and consult Nigel Griffith's article.
- Search the AIX and UNIX library by topic:
- System administration
- Application development
- Performance
- Porting
- Security
- Tips
- Tools and utilities
- Java technology
- Linux®
- Open source
-
Safari bookstore:
Visit this e-reference library to find specific technical resources.
-
developerWorks technical events and webcasts:
Stay current with developerWorks technical events and webcasts.
-
Podcasts: Tune in and
catch up with IBM technical experts.
Get products and technologies
-
IBM trial software:
Build your next development project with software for download directly from
developerWorks.
-
A 90-day evaluation:
Get a evaluation copy of DB2 Version 9 enterprise server from IBM.
-
Quick links
for AIX fixes:
Get your AIX updates here.
-
GNU c
compiler: You can
download the open source GNU c compiler in rpm format from here.
- IBM
Fix Central:
This site provides fixes and updates for your system's software, hardware, and
operating system.
-
Eclipse Web site: You can obtain the Eclipse
IDE from here.
-
Java 5
64-bit SDK:
You need to register to download this package, but registration is free.
-
Tomcat: Download the latest package.
-
Mozilla: The Mozilla
Web browser for AIX can be downloaded from IBM.
-
NuSOAP: NuSOAP is a SOAP server
and client library for PHP.
-
Apache Web services site: Get Aixs (the
SOAP Web services toolkit) from here.
Discuss
- Participate in the
developerWorks blogs
and get involved in the developerWorks community.
- Participate in the AIX and UNIX forums:
- AIX —technical forum
- AIX for Developers Forum
- Cluster Systems Management
- IBM Support Assistant
- Performance Tools—technical
- Virtualization—technical
- More AIX and UNIX forums
Doug Monroe is a UNIX System Administration consultant and instructor with DMA Inc. He holds a bachelor's degree in computer science from Oregon State University, and he has been supporting various flavors of UNIX since 1984. You can reach him at monroe@sqnt.com.
Martin Brown has been a professional writer for more than seven years. He is the author of numerous books and articles across a range of topics. His expertise spans myriad development languages and platforms -- Perl, Python, Java™, JavaScript, Basic, Pascal, Modula-2, C, C++, Rebol, Gawk, Shellscript, Windows®, Solaris, Linux, BeOS, Mac OS X and more -- as well as Web programming, systems management, and integration. He is a Subject Matter Expert (SME) for Microsoft® and regular contributor to ServerWatch.com, LinuxToday.com, and IBM developerWorks. He is also a regular blogger at Computerworld, The Apple Blog, and other sites. You can contact him through his Web site.




