简体   繁体   中英

Can PHP make HTTP Get request to Java?

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.


Try the exmple of 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);
?>

I hope this will give you some better ideas to use cURL() for RESTFul webservices .

 $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);

u can manipulate the $curl_reponse in php.

I would advice you to read up on Spring MVC and create a REST full webservice. Calling an executable java class might work to ofcourse, but I do believe that isn't common practice.

Here is an example of a possible controller class to call.

@Controller
public class TestController extends BaseController {

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

This is pretty advanced java though especially if you want to configure it all in Spring. I can't explain all of this in detail in this little post. However this site might enlighten you more http://viralpatel.net/blogs/tutorial-spring-3-mvc-introduction-spring-mvc-framework/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM