The Source for Java Technology Collaboration

Home » java.net Forums » Java Desktop Technologies » Swing & AWT

Thread: Execute PHP Script from Java Code

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 8 - Last Post: Aug 18, 2008 12:18 AM by: nico_v_c
jeffbruce

Posts: 15
Execute PHP Script from Java Code
Posted: Aug 1, 2008 2:08 PM
  Click to reply to this thread Reply

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

tarbo

Posts: 425
Re: Execute PHP Script from Java Code
Posted: Aug 5, 2008 2:44 AM   in response to: jeffbruce
  Click to reply to this thread Reply

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

walterln

Posts: 278
Re: Execute PHP Script from Java Code
Posted: Aug 5, 2008 7:14 AM   in response to: tarbo
  Click to reply to this thread Reply

But to execute a script on the server and get the result, I think you can use an URLConnection.

jeffbruce

Posts: 15
Re: Execute PHP Script from Java Code
Posted: Aug 12, 2008 12:28 PM   in response to: walterln
  Click to reply to this thread Reply

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?

robogeek

Posts: 38
Re: Execute PHP Script from Java Code
Posted: Aug 12, 2008 1:09 PM   in response to: jeffbruce
  Click to reply to this thread Reply

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.

jeffbruce

Posts: 15
Re: Execute PHP Script from Java Code
Posted: Aug 13, 2008 11:55 AM   in response to: robogeek
  Click to reply to this thread Reply

Excellent. Thanks for the clear explanation, you've helped me tremendously.

jeffbruce

Posts: 15
Re: Execute PHP Script from Java Code
Posted: Aug 15, 2008 9:50 AM   in response to: jeffbruce
  Click to reply to this thread Reply

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?

jeffbruce

Posts: 15
Re: Execute PHP Script from Java Code
Posted: Aug 15, 2008 9:54 AM   in response to: jeffbruce
  Click to reply to this thread Reply

I also tried con.connect() instead of con.getContent();

nico_v_c

Posts: 51
Re: Execute PHP Script from Java Code
Posted: Aug 18, 2008 12:18 AM   in response to: jeffbruce
  Click to reply to this thread Reply

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.




 XML java.net RSS