Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute a PHP script from Java?

Tags:

java

php

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.

like image 215
trivunm Avatar asked Mar 17 '09 19:03

trivunm


People also ask

Can I run PHP code in JavaScript?

Simply placing the PHP code inside JavaScript will not work in this case either. The reason you can't simply call a PHP function from JavaScript has to do with the order in which these languages are run. PHP is a server-side language, and JavaScript is primarily a client-side language.

Can I use PHP with Java?

There are two possible ways to bridge PHP and Java: you can either integrate PHP into a Java Servlet environment, which is the more stable and efficient solution, or integrate Java support into PHP. The former is provided by a SAPI module that interfaces with the Servlet server, the latter by this Java extension.


2 Answers

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.

like image 94
Mork0075 Avatar answered Sep 25 '22 18:09

Mork0075


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.

like image 36
Pablo Santa Cruz Avatar answered Sep 22 '22 18:09

Pablo Santa Cruz