2

Possible Duplicate:
invoking a php method from java

Never came across with this situation before, so would like to understand / know how to go about this ?

Goal: Call php function from Java

Let's say Java code looks like this

pulic class Testing 
{

String userid;
String pass;
String url;
public static void main (String[] args )
{
   String value1 = checker ( userid, pass, url );
   String value2 = dataGetter ( value1 )
}

public static String checker ( String userid, String pass, String url);
{
  // Code to get authenticated 
}

public static String dataGetter ( value1 );
}

and PHP code looks like this

 <?php
    $url;
    $size;

    function dataGetter( value1, $size)
    {
     // code to get data from server
    }
 ?>

Will this be possible ? if so can someone explain me how deployment will work ? i.e java being deployed on tomcat and php on apache ?

Community
  • 1
  • 1
Mad-D
  • 4,479
  • 18
  • 52
  • 93
  • 1
    IMO simplest solution is to turn your PHP code into a web service and call it from your java code via GET/POST/whatever to Apache. – Leigh Dec 06 '12 at 16:41
  • A better solution would be to just port the PHP code into Java. – NullUserException Dec 06 '12 at 16:44
  • On an unrelated note, killing off Mona Sax made me a sad panda. – NullUserException Dec 06 '12 at 16:45
  • @NullUserException I disagree, a better solution would be to just port the Java code into PHP ;) – Leigh Dec 06 '12 at 16:49
  • I had seen this earlier. Might be another option. http://php-java-bridge.sourceforge.net/pjb/ – dmikester1 Dec 06 '12 at 16:51
  • @Leigh Depends on which language most of their code is written in, in this case it looks like Java. I personally would never, ever, consider porting Java into PHP an option if most of my code is in Java. – NullUserException Dec 06 '12 at 16:52
  • @NullUserException: yes most of the code is written in java and i am just using php to execute cURL. – Mad-D Dec 06 '12 at 16:55
  • 1
    @Mad-D Then port it to Java. Instead of cURL, you can use Apache's [HTTPClient](http://hc.apache.org/httpcomponents-client-ga/) (easier) or [HttpURLConnection](http://docs.oracle.com/javase/6/docs/api/java/net/HttpURLConnection.html) (standard). You can even use [cURL in Java](http://curl.haxx.se/libcurl/java/). – NullUserException Dec 06 '12 at 17:03
  • 1
    @Mad-D Why don't you just write it in Java and use java.net.URL or java.net.URLConnection – Leigh Dec 06 '12 at 17:04
  • @NullUserException and Leigh : thanks a lot i shall try these alternative but i have php scrip up and running, so was curious to know how to implement it. – Mad-D Dec 06 '12 at 17:15

2 Answers2

0

While they cannot communicate directly, you can have them communicate in the same way that a client browser can communicate with the server, which is to say, using ajax and javascript. Have one page generated through jsp or php (doesn't matter which) load via ajax a php page or a jsp page (or servlet). The result is whatever you want it to be in either case and in this way you can get information from either.

Alternatively, you can make a Java program that opens a connection to a php page and uses what gets returned if you don't want to use a browser.

Of course, I'm assuming you are in the strange circumstance where you cannot simply drop one technology in order to use the other or I would highly recommend that solution over this. However, as these things usually go, if you're too far into your project to back out now, then this will also work for you.

Neil
  • 5,762
  • 24
  • 36
0

Call the PHP via the command-line or via http using java.net.URL.

jsleuth
  • 638
  • 4
  • 9