|
Replies:
8
-
Last Post:
Aug 18, 2008 12:18 AM
by: nico_v_c
|
|
|
|
|
|
|
Execute PHP Script from Java Code
Posted:
Aug 1, 2008 2:08 PM
|
|
|
How do I run a PHP script using Java code? Precisely, how do I execute a PHP script which resides on a web server with an Apache gateway from a Java applet at the front-end client side?
Suppose I have the method
public Resource get() {
}
what java code goes in here to execute a PHP GET script on the web server?
Thanks
|
|
|
|
|
|
|
Re: Execute PHP Script from Java Code
Posted:
Aug 5, 2008 2:44 AM
in response to: jeffbruce
|
|
|
If the client has a PHP interpreter, you can call the interpreter from the Java runtime just like you would any other program. If there are libraries which allow PHP execution, you can use these with a native interface (though I'm not sure whether applets --even when signed-- would allow this).
I'm thinking the best solution would be to check for Java libraries that implement PHP using the javax.script package. A Google search on "javax.script PHP" turns up a few hits that seem like they could be useful; you'd have to check whether they really are.
Hope this helps, Jonathan
|
|
|
|
|
|
|
|
Re: Execute PHP Script from Java Code
Posted:
Aug 5, 2008 7:14 AM
in response to: tarbo
|
|
|
But to execute a script on the server and get the result, I think you can use an URLConnection.
|
|
|
|
|
|
|
|
Re: Execute PHP Script from Java Code
Posted:
Aug 12, 2008 12:28 PM
in response to: walterln
|
|
|
Although this is helpful, I still can't figure out how to use a URLConnection object to execute a php script despite hours of searching on the web. Any takers?
|
|
|
|
|
|
|
|
Re: Execute PHP Script from Java Code
Posted:
Aug 12, 2008 1:09 PM
in response to: jeffbruce
|
|
|
Wouldn't it be the HTTPConnection subclass of URLConnection?
Anyway by causing a Java client app to request a given URL off a given HTTP server, if that URL refers to a PHP script the HTTP server has to invoke the PHP script in order to satisfy the URL request. So therefore the PHP invocation is done by the server and all the client app has to do is request that URL.
|
|
|
|
|
|
|
|
Re: Execute PHP Script from Java Code
Posted:
Aug 13, 2008 11:55 AM
in response to: robogeek
|
|
|
Excellent. Thanks for the clear explanation, you've helped me tremendously.
|
|
|
|
|
|
|
|
Re: Execute PHP Script from Java Code
Posted:
Aug 15, 2008 9:50 AM
in response to: jeffbruce
|
|
|
When my applet executes this code
try { url = new URL("http://www.domain.com//~username//storemp3.php"); con = (HttpURLConnection)url.openConnection(); con.getContent(); } catch (Exception e) { lbl_status.setText("Failed to run php script"); }
lbl_status's text is set to "Failed to run php script." Why doesn't this execute the php script? Am I using the wrong methods?
|
|
|
|
|
|
|
|
Re: Execute PHP Script from Java Code
Posted:
Aug 15, 2008 9:54 AM
in response to: jeffbruce
|
|
|
I also tried con.connect() instead of con.getContent();
|
|
|
|
|
|
|
|
Re: Execute PHP Script from Java Code
Posted:
Aug 18, 2008 12:18 AM
in response to: jeffbruce
|
|
|
The fact that lbl_status' text is set to "Failed to run php script." doesn't necessary mean that the script wasn't executed. Print out the exception that you get and it will (hopefully) tell you what the exception is. Also it is a good idea to catch more specific exception than just catch all exceptions in one general catch.
|
|
|
|
|