2

Which one is the best way to call a PHP script from JAVA? I'm calling now the PHP executable using the next code:

CommandLine command = new CommandLine("php");
command.addArgument("file.php");

DefaultExecutor exec = new DefaultExecutor();

PumpStreamHandler streamHandler = new PumpStreamHandler(null, null);
exec.setStreamHandler(streamHandler);

int execute = exec.execute(command);

But loading and disposing the php executable consumes resources. What about Java SAPI or something like loading a php module into java?

I'm calling the "exec.execute(command);" inside a loop.

4 Answers 4

1

This seems to be a dup of various questions and answers, including:

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

Comments

0
+50

If you really want to use the native php on the system from java, you can try this PHP/Java Bridge. It claims to be faster and more efficient than just shelling out to php. It also appears to be able to call from Java to php or php to java in web and desktop applications. This Java to php example code might be helpful.

2 Comments

Looking for some info, I'm interested in PhpCGIServlet or JSR 223 API. Gonna try.
I wrote my own bridge, but in fact, I'm using this answer.
0

I've never tried it myself, but it seems there's actually PHP interpreter written in Java, so you can run PHP code straight from Java programs (sort of like JRuby)

http://www.caucho.com/resin-3.0/quercus/

1 Comment

Or maybe it's a good idea... but the only examples I have seen are about installing resin like a JAVA web server. I have to research more.
0

By Java Servlet SAPI, you can enable the entire PHP processor to be run as a servlet.

2 Comments

Mmmmm, this one seems interesting, I'm going to research.
Have you tried it? I wan't to call a Bootstrap.php, a file that include the entire project from JAVA.

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.