If you are wondering how to access a Java function from your PHP code, this blog post is just for you. Answer to your problem is PHP/Java Bridge. You can do many more things using PHP/Java Bridge. Without beating around the bush let’s get started(happy-face).

What’s PHP/Java Bridge?

“The PHP/Java Bridge is an implementation of a streaming, XML-based network protocol, which can be used to connect a native script engine, for example PHP, Scheme or Python, with a Java virtual machine. It is up to 50 times faster than local RPC via SOAP, requires less resources on the web-server side. It is faster and more reliable than direct communication via the Java Native Interface, and it requires no additional components to invoke Java procedures from PHP or PHP procedures from Java.”  – Source 

pjb

Prerequisites

  • To be able to read the blog post. (tongue-out-face)
  • Working Linux system
  • Apache HTTP Server + PHP5
  • Apache Tomcat

1. Environment Setup

1.1 Download the PHP/Java Bridge

You can download the PHP/Java Bridge from this URL.

1.2 Install php-cgi

To use the PHP/Java Bridge you are going to need php-cgi on your system. So let’s install it.

1.3. Let’s test the PHP/Java Bridge

Okay now let’s check whether the PHP/Java Bridge works fine. Extract the JavaBridge.war from the zip which you dowloaded. Navigate the directory where JavaBridge.war is located. To check run the below command.

Did you get the output below?

PHP/Java BridgeTest Installation

Hurrah!!! if so you are good to proceed to the next step.

2. Let’s deploy the PHP/Java Bridge on an Apache Tomcat

Put the JavaBridge.war file into the apache-tomcat-6.0.35/webapps folder. Now let’s try accessing the PHP/Java Bridge from the browser.

You should get the below output.

PHP/Java Bridge Works

If you do, don’t forget to check out the examples given. If you are done with the examples why stop here? Let’s move on.

3. Let the PHP/Java Bridge do it’s magic

Hang on! We’re almost there.

3.1 Configure the php.ini

To be able to use the PHP/Java Bridge you need to change your php.ini. By default allow_url_include is set to off. You need to set it to on. It tells whether to allow include/require to open URLs (like http:// or ftp://) as files. Find more info here.

 

3.2 one last step

Create a new .php file on your Apache HTTP Server and put the code below.

Now try accessing the PHP file from the browser. You should get the below out.

Output - PHP/Java Bridge

There is a lot more on PHP/Java Bridge. Let’s cover those topics in another blog post. Now you know the basics of the PHP/Java Bridge. Plus check out this video on PHP/Java Bridge installation.

If you have any questions let me know in the comments below. Your feedback is highly appreciated(happy-face).

Be Sociable, Share!