0

I'm trying to make communication between PHP and Java.

Here's what I want to do. PHP pass a parameter ID to a Java file.

The Java get the ID and run some script and return a, ARRAY to the PHP.

I have read out a lot such as PHP/Java Bridge, SOAP, RestCall ...

But I couldn't found out one which works. basically is I don't know how to configure.

I want to find some simple examples which I can understand. And I don't need to use PHP/Java Bridge.

Something easier would do.

Update.

*I had tried Curl call to the Java file, on the $result = curl_exec($curl) returns the entire CODE in the Java.*

I even try Java Servlet, it also return only the entire CODE in the Java.

What I want is PHP make a GET request to the Java, Java will detect the GET request and obtain a parameter ID. Run some process and return an ARRAY back to the PHP.

I want it to be done in a HTTP request only.

But I couldn't figure out how it works. I even tried the HTTPComponent.

Please help out and show me simple example in the PHP and Java file.

I even follow this servlet http://brajeshwar.com/2008/handling-http-get-requests-in-java-servlets/ CGI http://www.javaworld.com/jw-01-1997/jw-01-cgiscripts.html?page=3 , and not only these. A lot examples. But none work.

Did I miss out anything? I will provide more details. I used XAMPP, every project and file will be in my htdocs.

All request will be something like this "http: // localhost/test/...."

Thank you.


Sorry that I didn't stated clearly enough.

The JAVA file is the normal JAVA file such as

public class HelloWorld {
String hw = "Hello World";

public void getHelloWorld() {

            System.out.println("abc");
}

    public static void main(String [] args){
    System.out.println("abc");
    }

}

Thank you.


Update 2

Here's my next question.

Like those Curl, Rest, Cgi call.. Its actually called to the .Java file right? And the result return is the Entire source code of the .Java.

This is because there's no compiler to compile the Java class right? I put the Java file in the xampp/htdocs, I compile it using Netbeans.

So when I call from PHP, there's no one to compile it? Correct me if I'm wrong?

So I should put the .Java file in a server such as Tomcat right? Or ways to compile it? I tried to put in the tomcat webapps, and connect to it through localhost:8080/test/test.java

It return me error 404.

How to access to .Java fil using Web Browser? Please help me.

Thank you.

SOLVED

Works Java with RESTFul. And I can get Curl call to Java.

Follow this guide

Very clear for beginners in Java like me.


4

3 に答える 3

0

Rest の例を試す

<?php
$url = 'JAVA_PAGE_URL';
// Open a curl session for making the call 
$curl = curl_init($url);
// Tell curl to use HTTP POST 
curl_setopt($curl, CURLOPT_POST, true);
// Tell curl not to return headers, but do return the response 
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Set the POST arguments to pass to the Sugar server 
$parameters = array('id'=>$id);
$json = json_encode($parameters);

$postArgs = 'method=login&input_type=JSON&response_type=JSON&rest_data=' . $json;
curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs);
// Make the REST call, returning the result 
$response = curl_exec($curl);

// Close the connection 
curl_close($curl);
// Convert the result from JSON format to a PHP array 
$result = json_decode($response);
?>
于 2013-04-24T08:34:06.987 に答える
0

Spring MVC を読んで、REST 完全な Web サービスを作成することをお勧めします。もちろん、実行可能なJavaクラスを呼び出すことはうまくいくかもしれませんが、それは一般的な方法ではないと思います。

呼び出すことができるコントローラ クラスの例を次に示します。

@Controller
public class TestController extends BaseController {

    @RequestMapping(value = "/rest/helloworld", method = RequestMethod.GET)
    public @ResponseBody
    String helloWorld(HttpServletRequest req)
            throws Exception {
        return "hello world";
    }
}

これはかなり高度な Java ですが、Spring ですべてを構成する場合は特にそうです。この短い記事ですべてを詳しく説明することはできません。ただし、このサイトはhttp://viralpatel.net/blogs/tutorial-spring-3-mvc-introduction-spring-mvc-framework/でより多くのことを教えてくれるかもしれません。

于 2013-04-24T19:32:03.797 に答える
0

RESTFul Webservices に cURL() を使用するためのより良いアイデアが得られることを願っています。

 $curl = curl_init();     
 $url = "http:";
 $fields = 8;
 curl_setopt($curl, CURLOPT_URL, $url);
 curl_setopt($curl, CURLOPT_POST, true);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
 //To use https
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($fields));
 curl_setopt($curl, CURLOPT_COOKIEJAR, "cookie.txt");
 curl_setopt($curl, CURLOPT_COOKIEFILE, "cookie.txt");
 $curl_response = curl_exec($curl);

php で $curl_reponse を操作できます。

于 2013-04-24T08:41:14.570 に答える