开发者

Java & PHP runtime communication

开发者 https://www.devze.com 2023-03-19 07:49 出处:网络
I\'m writing a watchdog-style program in Java - that is, it will be running constantly. I want to be able to someh开发者_如何学编程ow send input and receive a response using PHP.

I'm writing a watchdog-style program in Java - that is, it will be running constantly. I want to be able to someh开发者_如何学编程ow send input and receive a response using PHP.

What is the best way to do this?

Thanks!

EDIT: Just to clarify, the Java and PHP are running on the same machine.


A really simple solution,not as simple as the "file solution" by Itay), but more light-weight than HTTP or XML-RPC would be using a plain socket-based solution, as simple as illustrated here.

That would fit nicely for your "String back and forth" protocol.


There are a million ways to do this: one comparatively easy way is using a networked RPC system such as XML-RPC. You can quite cleanly call from PHP and receive the responses back.


very crud and not the most efficient
A socket like way:

  1. Create file A with permissions to both the Java process and php process.
  2. Java writes to the file, only when it is empty (or concatenate)
  3. PHP reads the file and deletes it.
  4. Do the same but the other way around for the PHP to send input to Java.

Again, this is crud and as you can see in the search result above, there are ready made solutions.


I'd do this by running the Java program in a servlet container (Jetty or Tomcat would do just fine) and use HTTP request/response between the PHP and Java. The Servlet will effectively provide an HTTP interface to your Java program. On the PHP side you can use cURL, file, or even fopen to perform GET requests, or cURL to perform POSTs and retrieve responses.

The data format you use for transactions is up to you, JSON seems like an easy choice, there's built-in support in PHP and excellent libraries for Java.


Maybe I'm missing something with all those giant libraries floating around, but what about using a socket and communicating over TCP? Can't get much simpler for transmitting simple data like strings and should be fairly performant.

0

精彩评论

暂无评论...
验证码 换一张
取 消