JBoss PHP integration with Web Services

User Rating: 2 / 5

Star ActiveStar ActiveStar InactiveStar InactiveStar Inactive
 
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.

It's possible as well to perform the reverse path: that is coding the Web Service in PHP and invoke it with a Java Client - if any reader is interested to know more about it please leave some feedback in the comments and we'll try to add later an article about it.

Coding the Web Service

Now 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:

jboss php java integration

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.
the first thing you have to do is enabling RCP and SOAP libraries in your php.ini file:

extension=php_soap.dll
extension=php_xmlrpc.dll


Using an Unix/Linux platform you'll have to enable the corresponding .so libraries.

After activating the SOAP extension in the PHP configuration file, we are able to create both  SOAP servers and a SOAP clients using the SOAP PHP class library.

Creating SOAP Client is just a matter of using the SoapClient class which accepts as input in the constructor the name/location of the WSDL.
Using a reference to the SoapClient Class we will then invoke Web Service methods:

 

<?php 
  $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 :
  $response = $client->sayHello($name);

Save this php page in your htdocs Apache folder and verify that the it correctly invokes the JBoss Web Service:

http://localhost/soapclient.php

Welcome Frank !

That's all ! I hope you have enjoyed this simple tutorial about PHP and Java integration.

Related articles available on mastertheboss.com

JBoss web services

JAX-WS simplifies the development model for a web service endpoin

What is a Web Service One Way invocation?

JBoss recipe of the day

Asynchronous web services with JBoss WS

Developing rigorous and responsive web service client application

How to change the default Web Service deployment Port ?

JBoss recipe of the day

Using Axis Web Services with JBoss

Still not ready for JBoss WS ? if you don't have a JDK 1.5 compli

Invoking JBoss Web Services with Flex

Flex® is a free, open source framework for building highly in

Follow us on Twitter