Home Web interfaces JBoss PHP integration with Web Services
11 | 03 | 2010
JBoss 5 AS Book
"JBoss AS 5 development" reviews
Please share your feedback/review with other readers!
Banner
Dashboard
Advertise with Us
Banner
RSS Feed
Login
Sign here for the NewsLetter.



Poll
What book could be in your wish list next XMas ?
 
JBoss admin resources
Banner
JBoss howto

How can you solve deployment errors caused by large war/jar/ear files ?

jboss recipe of the day ...
Read More

How do you configure your .war to be deployed after your EJB ?

jboss recipe of the day ...
Read More

How do I configure a Queue/Topic to work in a cluster?

JBoss recipe of the day ...
Read More
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.

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.

JBoss.org Search
Custom Search
Comments
Search
Only registered users can write comments!

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."