how to handle SoapServer response

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi there,

I'm new into SOAP and trying my first service, a user login service.
The thing I do not understand is how to handle the response of the SOAP
server. When I'm sending values that are invalid it returns the correct
SoapFault. But when the login parameters are correct I should get an
instance of my class User (see code below) but instead I get an empty
stdClass. How do I have to change it that it works? Thanks so far...

/**
 * Entity class for a user.
 */
class User {
	
	public $id;
	public $username;
	public $realname;
	private $password;
	
}

### The Login class encapsulating the SoapClient

include_once("User.php");
/**
 * Class for handling the user login through SOAP call.
 */
class SOAPLogin {
	
	/**
	 * SOAP client instance variable.
	 */
	private $soapClient = null;
	
	/**
	 * Error message array.
	 * Index 0: fault code
	 * Index 1:	fault string
	 * Index 2: fault actor
	 */
	private $errors = array();
		
	/**
	 * The user.
	 */
	private $user = null;
	
	/**
	 * Success flag.
	 */
	private $success = false;
	
	/**
	 * Default contructor creating a new SoapClient instance with the user
login wsdl.
	 *
	 */
	function __construct() {
		$this->soapClient = new
SoapClient("http://localhost/soa/userservice.wsdl";,
			array(	'exceptions' => 0));
	}
	
	/**
	 * Destructor.
	 */
	function __destruct() {
		$this->soapClient = null;
	}
	
	/**
	 * Tries to login the user.
	 * If successful get user through getUser().
	 *
	 * @param string $username
	 * @param string $password
	 * @return true if login was successfull
	 */
	function login($username, $password) {
		if($this->soapClient) {
			$result = $this->soapClient->login(array('username' => $username,
'password' => $password));
			
			if(is_soap_fault($result)) {
				//call failed because of invalid values (empty vars) or something bad
				$this->errors[0] = $result->faultcode;
				$this->errors[1] = $result->faultstring;
				$this->errors[2] = $result->faultactor;
				$this->errors[3] = $result->detail;
				$this->errors[4] = $result->faultname;
				$this->errors[5] = $result->headerfault;
				$this->userId = 0;
				return false;
			} else {
				//call successful
				$this->errors = array();
				
				// TODO how to handle $result???
	
				//$this->success = ;
				//$this->user = ;				
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * Returns the user.
	 * @return user
	 */
	function getUser() {
		return $this->user;
	}
	
	/**
	 * Returns if the call was successful.
	 * @return if call was successful
	 */
	function getSuccess() {
		return $this->success;
	}
	
	/**
	 * Returns an array of error values if an error occured.
	 * Index 0: fault code
	 * Index 1:	fault string
	 * Index 2: fault actor
	 * 
	 * @return error array
	 */
	function getErrors() {
		return $this->errors;
	}
}

### The SoapServer
<?php
include_once("User.php");
/**
 * SOAP service class handling user requests.
 */
class UserService {
	/**
	 * SOAP login function.
	 * Tries to login the given user with the given password.
	 *
	 * @param string $username
	 * @param string $password
	 * @return User class or SoapFault if something bad happened 
	 */
	function login($username, $password) {
		if(strlen($username) > 0 && strlen($password) > 0) {	
			// hardcoded
			if(strcmp($username, "chefkoch") == 0 && strcmp($password, "gott") == 0)
{
				$user = new User();
				$user->id = 666;
				$user->username = "chefkoch";
				$user->realname = "Mr. Allmighty"; 
				return $user;
			} else {
				return new SoapFault("client", "login or password were incorrect.",
"SOAPServer.php");
			}
		} else {
			return new SoapFault("client", "input parameters were invalid.",
"SOAPServer.php");
		}
	}
}
// disable caching of wsdl for development
ini_set("soap.wsdl_cache_enabled", "0");
// SOAP server instance
$server = new SoapServer(NULL, array('uri' => "http://localhost/soa/";,
'classmap' => "User"));
// add SOAP server functions
$server->setClass("UserService");
// handle requests
$server->handle();
?>

### and finally the wsdl
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/";
	xmlns:tns="http://localhost/soa/userservice/";
	xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/";
	xmlns:xsd="http://www.w3.org/2001/XMLSchema"; name="userservice"
	targetNamespace="http://localhost/soa/userservice/";>
	<wsdl:types>
		<xsd:schema
			targetNamespace="http://localhost/soa/userservice/";>
			<xsd:element name="login">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name="username" type="xsd:string" />
						<xsd:element name="password"
							type="xsd:string">
						</xsd:element>
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>
			<xsd:element name="loginResponse">
				<xsd:complexType>
					<xsd:sequence>
						<xsd:element name="out" type="tns:User" />
					</xsd:sequence>
				</xsd:complexType>
			</xsd:element>

			<xsd:complexType name="User">
				<xsd:attribute name="id" type="xsd:int"></xsd:attribute>
				<xsd:attribute name="username" type="xsd:string"></xsd:attribute>
				<xsd:attribute name="realname" type="xsd:string"></xsd:attribute>
				<xsd:attribute name="password" type="xsd:string"></xsd:attribute>
			</xsd:complexType>
		</xsd:schema>
	</wsdl:types>
	<wsdl:message name="loginRequest">
		<wsdl:part element="tns:login" name="parameters" />
	</wsdl:message>
	<wsdl:message name="loginResponse">
		<wsdl:part element="tns:loginResponse" name="parameters" />
	</wsdl:message>
	<wsdl:portType name="userservice">
		<wsdl:operation name="login">
			<wsdl:documentation></wsdl:documentation>
			<wsdl:input message="tns:loginRequest" />
			<wsdl:output message="tns:loginResponse" />
		</wsdl:operation>
	</wsdl:portType>
	<wsdl:binding name="userserviceSOAP" type="tns:userservice">
		<soap:binding style="document"
			transport="http://schemas.xmlsoap.org/soap/http"; />
		<wsdl:operation name="login">
			<soap:operation
				soapAction="http://localhost/soa/userservice/login"; />
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:service name="userservice">
		<wsdl:port binding="tns:userserviceSOAP"
			name="userserviceSOAP">
			<soap:address
				location="http://localhost/soa/SOAPServer.php"; />
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>

Thanks for any help...! I'm stuck ...

-- 
View this message in context: http://www.nabble.com/how-to-handle-SoapServer-response-tf3579157.html#a10001294
Sent from the Php - Soap mailing list archive at Nabble.com.

-- 
PHP Soap Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Kernel Newbies]     [PHP Database]     [Yosemite]

  Powered by Linux