tags:

views:

1069

answers:

4

I have a php script which is executed over a URL. (e.g. www.something.com/myscript?param=xy)

When this script is executed in a browser it gives a coded result, a negative or positive number.

I want to execute this script from Java code(J2EE) and store that result in some object.

I'm trying to use httpURLConnection for that. I establish a connection but can not fetch the result. I'm not sure if I execute the script at all.

A: 

Any help would be appreciated; I'm not sure if I execute the script at all.

Check your web server logs to see if it is being executed, and if so what the errors are. Useless without these.

Ali A
php script is executing but I could not get the result,I solved the problem with bufferedReader class
+3  A: 
public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL yahoo = new URL("http://www.yahoo.com/");
        URLConnection yc = yahoo.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

This snippet is from the offical Java tutorial (http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html). This should help you.

Mork0075
this is the solution,thanks a lot
+4  A: 

If your J2EE app is deployed on the same server the PHP script is on, you can also execute it directly through as an independent process like this:

  public String execPHP(String scriptName, String param) {
    try {
      String line;
      StringBuilder output = new StringBuilder();
      Process p = Runtime.getRuntime().exec("php " + scriptName + " " + param);
      BufferedReader input =
        new BufferedReader
          (new InputStreamReader(p.getInputStream()));
      while ((line = input.readLine()) != null) {
          output.append(line);
      }
      input.close();
    }
    catch (Exception err) {
      err.printStackTrace();
    }
    return output.toString();
  }

You will pay the overhead of creating and executing a process, but you won't be creating a network connection every time you need to execute the script. I think that depending on the size of your output, one will perform better than the other.

Pablo Santa Cruz
I don't have PHP on the same server so this is not working for me
+3  A: 

If you are trying to run it over HTTP I would recommend the Apache Commons HTTP Client libraries. They make it incredibly easy to perform this type of task. For example:

    HttpClient http = new HttpClient();
    http.setParams(new HttpClientParams());
    http.setState(new HttpState());

    //For Get
    GetMethod get = new GetMethod("http://www.something.com/myscript?param="+paramVar);
    http.executeMethod(get);

    // For Post
    PostMethod post = new PostMethod("http://www.something.com/myscript");
    post.addParameter("param", paramVar);
    http.executeMethod(post);
Dr.Pil
thanks for the post,solved the problem with the solution gave by Mork0075