Issue
I have a game server application (Java) and want to have PHP communicate directly with it. For example, sending a command (from PHP) that returns a list of users. Kicking a specific player from the game server etc.
How can I interface Java with PHP? I've thought of a few things, would any of these work?
- Java game server running an additional service (e.g. HTTP server) for remote control (on another port)
- PHP and Java communicating via. database - e.g. Java checks table for commands to-do
- Java and PHP both connecting to an additional service that interfaces the two
- Java connects (socket etc.) to server
- PHP connects to server
- Server handles traffic either way
Which would be best and how would I go about implementing that method?
Solution
The exact conditions of the problem you are trying to solve are not clear to me. But let me suggest a few scenarios, hopefully at least one of which addressess your problem.
First of all, I am assuming both your java code and php code reside on a web server, which means they can be executed using standard http protocol. In php, that simply means that you have a .php web page that can use the standard $_REQUEST object and standard output methods (such as header and echo). Similarly, in java that means your code resides in a servlet or jsp and uses the standard request and response objects.
I will discuss methods for sharing global data between java and php below, but for the moment let's not worry about that. Let's discuss 3 possible communication scenarios:
1) Your game is running in a browser (or another program that uses http to communicate with your web server) and wants to call the java and php separately. In that case, your game code simply calls the appropriate url, using the standard GET or POST methodology for passing in arguments, and processing the response text returned by your code. However, I suspect that scenario does not answer your question.
2) If you want your java to call your php, then you can use the java.net classes (such as URL) to call your php page. The java tutorial has a good explanation on how to do this: http://docs.oracle.com/javase/tutorial/networking/urls/index.html
3) Similarly, if you want your php to call your java, there are several possibilities. The most general one is the CURL php extension. There is a good discussion on how to do this here: How to send a GET request from PHP? BTW, the Java Bridge mentioned above uses this methodology.
Ok, so now how do we share data between java and php? If there is no need to share global data between your code, then, as mentioned above, you can use http to pass in request parameters and send back text output responses (using json, xml, or whatever else you like). However, if there is global data that needs to be shared, that is a bit more complicated. You need to use the server file system to share the data, as there is no memory sharing between java and php (at least that I know of). Since both java and php can read and write files, there is no problem writing the code. However, there is the problem of concurrency. If it is possible for several pieces of code to access and modify the global data at exactly the same time, then you have to worry about that, because otherwise data changes could be lost or the data could get corrupted. So the easiest solution, as suggested above, is to use a database, because the database software takes care of the concurrency for you. Otherwise, you have to have some sort of file locking mechanism, which is doable, but probably not worth the effort.
Answered By - Howard Schutzman
Answer Checked By - Mary Flores (JavaFixing Volunteer)