7

I've looked at quite a few tutorials on using Java from PHP, but they seem to conflict with eachother. Many of them say to change php.ini to use a dll file, but apparently there are "built in" capabilities in PHP 5 now. But that requires Tomcat? Do I have to get rid of Apache to run that? http://php-java-bridge.sourceforge.net/pjb/installation.php says to put the .war file into Tomcat's autodeploy directory, but I don't see any directory called "autodeploy". Would someone explain step-by-step how to get get Java stuff working on a Windows machine (I'm using Windows 7) with PHP 5 and Apache?

4
  • 1
    Perhaps you could explain why you're trying to mix Java and PHP? Commented May 6, 2011 at 11:23
  • Because I'm going to use CMU Sphinx 4, which is written entirely in Java. I have a Java application to do the speech recognition task, and a php application to collect audio. Right now I collect the audio and analyze it with Sphinx later, then send the results of it via email whenever I have a chance. I need to change the application to collect the audio, run speech recognition, and immediately send back results via the php application. Commented May 7, 2011 at 16:26
  • What it means by autodeploy directory is webapps directory under tomcat main dir. Commented May 9, 2011 at 8:26
  • And why not to collect audio using Java? Why to make such wild mixes? Commented May 9, 2011 at 9:19

3 Answers 3

14

You will probably find tons of useless and outdated information regarding how to use Java classes from your PHP webapp which doesn't help at all. About a year ago I had faced a similar problem and I found out that the only way that is mature enough to use in production is PHP/Java Bridge -- the one you found on Sourceforge. It works better than one might expect and doesn't require knowledge of Java (however if you do know Java, you should consider writing a Web-service for this purpose and consume it on the PHP-side).

The documentation of the Bridge is not very obvious, however remember you should stick with it and not read other resources that describe different methods - don't mix things up, i.e. you don't have to install any PHP-extensions or smth -- all of them relates to another (usually outdated and unsupported) ways to call Java from PHP.

PHP/Java Bridge is a network protocol, so the only thing you need on your PHP-side is to ensure "allow_url_include" is enabled in your php.ini

From the Java side you have to get PHP/Java Bridge bound to a free port and listen to connections. Then your PHP-app will able to do "require_once" of the Bridge's proxy file which will be available on that port. That's it and you can instantiate and call Java classes transparently from your PHP script using that proxy Bridge object (it will serialize and transfer calls and results behind the scenes).

There are two options how get the Bridge up and bind it to some local port.

1) complex one: setup a Tomcat, create a war file with the bridge itself and the library you'd like to use. This way is described here:

http://php-java-bridge.sourceforge.net/pjb/webapp.php

To deploy the war into your Tomcat installation simply put the war into Tomcat's webapp folder and restart it.

2) easier one: package a jar instead of war and run it (it will use embedded web-server, so you won't have to install Tomcat and deploy anything). To go this way refer this page:

http://php-java-bridge.sourceforge.net/pjb/desktop-apps.php

Both ways will work for you (don't look at the "desktop" word at the second option, this way will work fine with your PHP webapp as well).

Refer to examples on how to use it at the "Examples" section (obvously). Also you can refer to my small PHP client that talks to Neo4J embedded database in Java using PHP/Java Bridge. However this sample might be more complex and it was intended to be deployed to the Tomcat, anyway you can have an idea how to instantiate and use classes from it:

https://github.com/coffeesnake/neo4j-php-wrapper

Sign up to request clarification or add additional context in comments.

Comments

1

You could just call a simple Java app from PHP by forking a process and communicating over stdin / stdout. Alternatively, if you integrate Sphinx into a webapp you could integrate by making HTTP calls.

1 Comment

I don't want to invoke java several times, just once and then communicate about new audio files to recognize. This is because there's lots of overhead in allocating the recognizer, but not much after that. A webapp sounds interesting, but I've never written one before...
1

Quercus:

Quercus is Caucho Technology's fast, open-source, 100% Java implementation of the PHP language. Quercus is a feature of Caucho Technology's Resin Application Server and is built into Resin - there is no additional download/install. Developers using Resin can launch PHP projects without having to install the standard PHP interpreter (http://www.php.net) as Quercus takes on the role of the PHP engine.

1 Comment

Neat. It's GPL, so I definitely could have used it for my research applications.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.