GeeCON Prague 2014*
The moose likes Java in General and the fly likes Hot to execute php from java api Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Hot to execute php from java api" Watch "Hot to execute php from java api" New topic
Author

Hot to execute php from java api

Melanie Walsh
Greenhorn

Joined: Dec 14, 2004
Posts: 20
Hi
Im have real problems. i am building an application for my final yr project. The app is java based, it takes in a collection of vars from the user and stores. The problem im having is, the app needs to call a php to execute, find web pages, perform a search, filter and return results to java app. I have managed to sort out the php side as php can happily call java classes but i cant seem to get java to execute php script.
I have found info on java.net using the url object, this only retrieves the page but does not execute it. Although this seems a pefect app for a web service i am really hoping to have a stand alone application. Is there a way to call a php script to execute, do its stuff and retrieve the results without using browsers.
I would really appriciate any feedback on this as i have spent weeks going round and round in circles.
Thanks guys
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30598
    
154

Melanie,
The URL object is a good lead. Are you using the same URL a user would type in at the browser?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Melanie Walsh
Greenhorn

Joined: Dec 14, 2004
Posts: 20
No, the application is a TV Guide, the user will add Channels, Programs, Interests etc to the java app then request a TV Guide based on the stored info. The java app must then call a php script to execute do its stuff and bring back reformatted results. The problem is the php stuff is behind the scenes. the url object in java allows you to open, read a url but i need to run a script. Ideally the user will not see a browser as all web-based stuff should not be visible but im having real problems.... Do you have any suggestions?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30598
    
154

Melanie,
There are generally two ways of getting output from a scripting language. (I don't know much about PHP, so I'll assume it is the same idea as Perl.)
1) Call through URL and parse results
2) Call through command line and parse results

For the first one, you would use the URL a user would use if they were going through a browser. It's ok if no real users do that as long as the script returns something. For this technique, you would use the URL object.

For the second one, the language needs to support command line processing. Then you can use Runtime.exec(). You'll notice that I put "and parse results" with both techniques. Since it is not possible to pass an object back to your Java program, you need to parse the stream/string returned.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Note that to use the URL approach, the PHP script has to be installed as a CGI on a web server somewhere. If you have a local PHP script on the same machine running the Java program, you can use the System.exec() method Jeanne suggested. You're basically running a command to interpret the script as if you typed it in a command shell. You can then get an OutputStream of some sort to read the output of the script.
Melanie Walsh
Greenhorn

Joined: Dec 14, 2004
Posts: 20
Yes i have apache installed so php scripts are local, i have been told i can execute script using
URL localScript = new URL("http://localhost/BBC2Test3.php/?program");
URLConnection phpConnection = localScript.openConnection();
from a friend at uni but this doesnt seem to work
Will look into your suggestion. thank you
 
GeeCON Prague 2014
 
subject: Hot to execute php from java api