Tuesday 23 April 2013

Run php script from java code

below program shows how to run php script from a java code
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;


public class Check
    {
        public static void main(String[] args) throws Exception
            {
                URL yahoo = new URL("http://localhost/base.php");
                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();
            }
    }

For more information click here 

1 comment:

  1. you can also run more than one php script but for each script you have to make a separate URL connection.........

    Code for the following is as shown below:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;


    public class Check
    {
    public static void main(String[] args) throws Exception
    {
    URL yahoo = new URL("http://localhost/base.php");
    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();
    URL check = new URL("http://localhost/check.php");
    URLConnection ch = check.openConnection();
    BufferedReader sec = new BufferedReader(new InputStreamReader(ch.getInputStream()));
    String inputLine2;
    while ((inputLine2 = sec.readLine()) != null)
    System.out.println(inputLine2);
    sec.close();


    }
    }

    ReplyDelete