This example shows how to create an Zend Framework based SOAP Server and connect to it by a Java Client. The process itself is really easy but some things ara a bit tricky for example at the status were i created it there were some problems if using WS style for the SOAP Server so i decided to use RPC style. The Server basically allows a user to logon by credentials and gives back a userstatus, the Java Client is connecting through this service and requesting the userstatus.
The Soap Server Class
<?php
// orginal code has phpdoc compatible comments with slash star
//
// SoapUserServer Class
//
// @author
//
//
// Description of SoapUserServer
//
// @author johny
//
class Example_SoapUserServer {
//
//
// Returns Api Version
// @return string getApiVersion
//
public function getApiVersion()
{
$apiVersion="0.9.3";
return $apiVersion;
}
//
// getUser
// Returns user Status for a given Username, Password combination
// @param string $username
// @param string $password
// @return string getUserResponse
//
public function getUser($username, $password){
$adapter = new Example_AuthAdapter($username,$password);
$userStatus=false;
//retrive login
$auth = Zend_Auth::getInstance();
$auth->authenticate($adapter);
$acl = new Zend_Acl($auth);
if (!$auth->hasIdentity()) {
//user not valid
$userStatus=false;
}else{
//user okay
//@todo dynamic membership types
$membership=$auth->getIdentity()->getMembership();
if($membership && $membership->getMembershipTypeId()>=1){
$userStatus="premiummember";
}else{
$userStatus="normalmember";
}
}
if($userStatus!=false){
return $userStatus;
}else{
return "wrong credentials";
}
}
}
?>
Zend Controller for the Soap Server
<?phpclass SoapController extends Zend_Controller_Action
{ // // Zend Soap Server Action // Representing the soap Server /// public
function serverAction
(){ if(isset($_GET['wsdl'])) { $autodiscover =
new Zend_Soap_AutoDiscover
();
$autodiscover->
setBindingStyle(array('style' =>
'rpc'));
$autodiscover->
setClass('Example_SoapUserServer');
$autodiscover->
handle();
exit;
} // disabling WSDL cache cause we are still in Development ini_set("soap.wsdl_cache_enabled",
"0");
$server =
new SoapServer
('http://example.com/soap/server?wsdl');
$server->
setClass('Example_SoapUserServer');
$server->
handle();
$this->_helper->
viewRenderer->
setNoRender();
$this->_helper->
layout->
disableLayout();
}}?>
Java Client Code
With Netbeans a webservice client is really easy to create, right click on the project create new "Web Service Client"
More Information:
http://netbeans.org/kb/docs/websvc/jax-ws.html
JAX RPC Style works better with Zend Framework and PHP.
The Stub and Port classes will be Auto generated by Netbeans, so you don“t need to worry about that.
package eu.
webconsults.
myapp.
userauth;
import java.rmi.RemoteException;import myapp.userauth.Example_SoapUserServerPort_Stub;import myapp.userauth.Example_SoapUserServerService_Impl;//// User Authorization class using Soap Server// @author johny//public class UserAuth
{ String username =
null;
String password =
null;
String usertype =
null;
Boolean isAuthorized =
false;
Example_SoapUserServerPort_Stub stub;
// // Authenticating remote // // @param username // @param password /// public UserAuth
(String username,
String password
) { this.
username = username;
this.
password=password;
this.
retrieveRemoteUserStatus();
} // // Retriving remote User Status from Stub // @return /// private String retrieveRemoteUserStatus
(){ try{ this.
usertype =
this.
getStub().
getUser(this.
username,
this.
password);
if(!
this.
usertype.
equals("wrong credentials")){ this.
isAuthorized=
true;
}else{ this.
isAuthorized=
false;
} } catch(RemoteException re
){ System.
out.
println("Remote Exception"+re.
getMessage());
} return this.
usertype;
} // // // @return // private Example_SoapUserServerPort_Stub getStub
(){ if(this.
stub ==
null){ this.
stub=
(Example_SoapUserServerPort_Stub
) (new Example_SoapUserServerService_Impl
().
getExample_SoapUserServerPort());
} return this.
stub;
} // // Getting Username // @return // public String getUsername
(){ return this.
username;
} // // Getting Password // @return // public String getPassword
(){ return this.
password;
} // // Returning Usertype // @return // public String getUsertype
(){ return this.
usertype;
}}