How can I execute a PHP script from Java?

后端 未结 6 380
迷失自我
迷失自我 2020-12-16 03:01

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 neg

6条回答
  •  情深已故
    2020-12-16 03:46

    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.

提交回复
热议问题