开发者

Access java app from PHP

开发者 https://www.devze.com 2023-03-07 02:03 出处:网络
I have a java app. It has 3 important functions that provide important data. I want to create a web interface for this app. I want access these functions (call them) from a PHP and retrieve the return

I have a java app. It has 3 important functions that provide important data. I want to create a web interface for this app. I want access these functions (call them) from a PHP and retrieve the returned data. What is the best way to achieve this? I previously created a WSDL web service but I encountered some problems explained here:

https://stackoverflow.com/questions/5929669/call-a-wsdl-web-service-created-by-java-from-nushphere-phped

and here:

PHP: SoapClient constructor is very slow (takes 3 minutes)

If there's any other way (or a better way), please let me know how can I do it. Thank you in advan开发者_C百科ce.


There are couple of more viable alternatives:

  • PHP Java Bridge
  • Thrift

Here is a good tutorial on integrating Java with PHP using Thrift


Hessian is a protocol you can you:

http://en.wikipedia.org/wiki/Hessian_%28web_service_protocol%29


Expose the java app as a service using JAX-RS. You can use Jersey http://jersey.java.net/ to get up and running relatively quickly. Once you do that, you can easily issue curl commands with stuff like:

Assuming you have the necessary jars from jersey and you've configured the web.xml (follow jersey getting started tutorial), you can do this on the java side:

@Path("/helloservice")
public class HelloServiceResource {
    @GET
    @Path("/sayhello")
    @Produces("application/json")
    public String sayHello() {
        return "{\"message\":\"Hello\"}";
    }
}

Now in PHP you can:

// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://myserver.com/myapp/helloservice/sayhello");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
0

精彩评论

暂无评论...
验证码 换一张
取 消