| JBoss PHP integration with Web Services |
| Written by F.Marchioni | |||||
|
In this article we are going to see how you can integrate easily Java and PHP using Web Services. In detail, we will see how a JAX-WS Web Service deployed on JBoss can be consumed by a PHP application
PHP is a very popular platform for developing web applications so it's very likely that you have in your enterprise a few portal developed with this technology.
At the beginning the only mean of communication between Java and PHP was through HTTP streaming which is still a viable option but a few other alternatives exists. One of the simplest way to integrate PHP and Java is by means of Web Services. Since we are mostly interested in building the Server-side in Java we will develop a JAX-WS Web Service and then consume it with a PHP Client. Coding the Web ServiceNow let's build a minimal Web Service using JEE annotations:
package com.sample;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
@WebService(targetNamespace = "http://www.mastertheboss.com/", serviceName = "HelloWorldService")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class HelloWorld
{
@WebMethod
public String sayHello(String input)
{
return "Welcome " +input + " !";
}
}
To be deployed as a Web Service this POJO needs to be packed in a .war file and declared in the web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>HelloWorldService</servlet-name>
<servlet-class>com.sample.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldService</servlet-name>
<url-pattern>/helloWorldService</url-pattern>
</servlet-mapping>
</web-app>
Now deploy this Web Service and verify from the Web Console that it's available as a Service: This will expose the following WSDL:
<definitions name="HelloWorldService" targetNamespace="http://www.mastertheboss.com/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.mastertheboss.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<types />
<message name="HelloWorld_sayHello">
<part name="arg0" type="xsd:string" />
</message>
<message name="HelloWorld_sayHelloResponse">
<part name="return" type="xsd:string" />
</message>
<portType name="HelloWorld">
<operation name="sayHello" parameterOrder="arg0">
<input message="tns:HelloWorld_sayHello" />
<output message="tns:HelloWorld_sayHelloResponse" />
</operation>
</portType>
<binding name="HelloWorldBinding" type="tns:HelloWorld">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="sayHello">
<soap:operation soapAction="" />
<input>
<soap:body namespace="http://www.mastertheboss.com/" use="literal" />
</input>
<output>
<soap:body namespace="http://www.mastertheboss.com/" use="literal" />
</output>
</operation>
</binding>
<service name="HelloWorldService">
<port binding="tns:HelloWorldBinding" name="HelloWorldPort">
<soap:address location="http://127.0.0.1:8080/HelloWorld/helloWorldService" />
</port>
</service>
</definitions>
Creating the PHP Client:It's assumed that you have a PHP 5 environment configured on Apache Server. $client = new SoapClient("http://127.0.0.1:8080/HelloWorld/helloWorldService?wsdl"); $name='Frank'; $response = $client->sayHello($name); echo $response; ?> As you can see the $client instance references directly the Web Service method sayHello with : Save this php page in your htdocs Apache folder and verify that the it correctly invokes the JBoss Web Service: http://localhost/soapclient.phpWelcome Frank !That's all ! I hope you have enjoyed this simple tutorial about PHP and Java integration.
JBoss.org Search
Custom Search
Only registered users can write comments!
Powered by !JoomlaComment 3.26
3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |


