Connect PHP code to Java server

I am embedding a website using PHP for the front-end and the Java service as the back end. Two parts are as follows:

  • The front end of PHP listens for http requests and interacts with the database.

  • The Java back panel works continuously and answers calls from the front.

More specifically, the back end is a daemon that connects and communicates with several IM services (AOL, MSN, Yahoo, Jabber ...).

Both levels will be deployed on the same system (I suppose the CentOS box) and presenting the middle level (for example, using XML-RPC) will decrease performance (the resource is also limited).

Question: Is there a way to directly link two layers? (there are no more web services between them)

+7
java php native service daemon
source share
9 answers

Since this is a connection between two separate running processes, a β€œdirect” call (as in JNI ) is not possible. The easiest way to use such interprocess switching is probably named pipes and network sockets. In both cases, you will need to define a communication protocol and implement it on both sides. Using a standard protocol such as XML-RPC makes this simpler, but not strictly necessary.

+9
source share

There are usually four patterns for integrating applications:

  • through the file system, i.e. one manufacturer writes data to a consumer-controlled catalog.
  • through the database, i.e. two applications use a schema or table and use it to exchange data.
  • through RMI / RPC / web service / any lock, call synchronization from one application to another. For PHP in Java, you can choose from the various integration libraries listed above, or use some web service standards such as SOAP.
  • via messaging / any non-blocking asynchronous operation when one application sends a message to another application.

Each of these templates has its pros and cons, but a good rule is to choose one of the weakest links that you can handle. For example, if you select # 4, your Java application may crash without deleting your PHP application.

I would suggest, before looking at the specific libraries or technologies listed in the answers here, that you choose the right template for you, and then research your specific options.

+4
source share

I tried the PHP-Java modem (php-java-bridge.sourceforge.net/pjb/) and it works very well. Basically, we need to run the jar file (JavaBridge.jar) that listens on the port (there are several options available, such as Local socket, port 8080, etc.). Java class files must be accessible to JavaBridge in the classpath. You need to include the Java.inc file in your php, and you can access the Java classes.

+3
source share

Of course, there are many ways, but you said about a limited resource ...

IMHO defines your own lightweight RPC-like protocol and uses TCP / IP sockets for communication. In fact, in this case there is no need to take full advantage of RPC, etc. You only need to define an API for this particular case and implement it on both sides. In this case, you can serialize your packages to very small ones. You can even assign a kind of GUID to your remote methods and use them to save traffic and speed up your connection.

The advantage of using sockets is that your solution will be quite scalable.

+2
source share

You can try PHP / Java integration .

In addition, if the connection is one-way (something like "sendmail for IM"), you can write PHP requests to a file and control this in your Java application.

+1
source share

I recently ran into this problem. The Resin solution above is actually a complete rewrite of PHP in Java along the lines of JRuby, Jython, and Rhino. It is called Quercus. But I guess for you as well as for me, and uploading Apache / PHP settings is actually not an option.

And besides Quercus, there are other problems: the free version is the GPL, which is difficult if you are developing commercial software (although not as difficult as Resin would like you to believe (but IANAL)), and in addition to this, the free version does not support compilation into byte code, so its mostly interpreter is written in Java.

In the end, I decided that I just needed to exchange simple messages over HTTP. I used PHP json_encode() / json_decode() and Java json-lib to encode messages in JSON (simple, text, good match for data model).

Another interesting and easy option would be to force Java to generate PHP code and then use the PHP include () directive to retrieve this via HTTP and execute it. I have not tried this though.

If its actual HTTP calls you are concerned about (for performance), none of these solutions will help. All I can say is that I had no problems with PHP and Java on the same local network. I feel this will not be a problem for the vast majority of applications if you keep your RPC calls pretty clear (which you really need to do anyway).

+1
source share

Sorry, this is a little quick answer, but: I heard that the Resin application server supports java and PHP integration.

They claim they can break php and java together: http://www.caucho.com/resin-3.0/quercus/

I used the resin to serve J2ee applications, but not to support PHP.

I would be interested to hear about such adventures.

0
source share

Why not use a web service?

Make a Java layer and put ws access (Axis, SpringWS, etc.), and Php accesses the Java layer using a single ws client.

I think this is simple and useful.

0
source share

I came across this page, which is a tool for connecting two layers. However, it still requires an intermediate layer (TCP / IP). Moreover, other services may use the Java service as well because it accepts all incoming connections.

http://www.devx.com/Java/Article/20509

[Exploring ...]

-one
source share

All Articles