• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hot to execute php from java api

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Melanie,
The URL object is a good lead. Are you using the same URL a user would type in at the browser?
 
Melanie Walsh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic