SoapBehavior

Hello,

I am working with a lot of different webservices and some time along the way I was tired of writing almost the same code all over again, so I decided to work on a behavior for my webservice components. My main goal was to separate error handling. Connection problems (eg WSDL not found, timeouts) are now isolated from errors because of for example a "wrong" call (eg a missing field or wrong type).

If anyone knows how to improve this behavior I’d be happy to do so.

To use it, your component needs the following variables:




public $wsdl;

public $config = array();

public $behaviors = array(

	'soapBehavior' => array(

		'class' => 'ext.SoapBehavior'

	),

);



I initialize $wsdl and $config through my config files (eg protected/config/main.php) and setComponents in the module I wanna use it.

My config:




'modules' => array(

	'moduleName' => array(

		'wsConfig' => array(

			'wsdl' => 'http://example.com/wsdl',

			'config' => array(

				'proxyHost' => 'example.com',

				'proxyPort' => '8080',

			),

		),

	),



In the module




$this->setComponents(array(

	'compName' =>

		CMap::mergeArray(array(

			'class' => 'module.components.WSComponent',

			), $this->wsConfig),

));



To do a call you have to run soapRequest($nameOfSoapMethod, $request):




$ret = $this->soapRequest('getUserInformation', array(

	'userId' => 321,

));

$ret['success'];  // indicates if the soap call was successful

$ret['wsReturn']; // what the webservice returned



A few notes:

  • I deactivate xdebug because it causes problems with the try/catch (see Issue 609)

  • I use “warning” for logging because I don’t want to get every Mail twice and also I don’t want to deactivate “error”-mails

  • Right now it tries to establish a connection every time soapRequest is invoked




<?php


/**

 * Description of SoapBehavior

 *

 * @author yodel

 */

class SoapBehavior extends CBehavior {


	private $__soapClient;


	public function getSoapClient() {


		if(empty($this->__soapClient)) {

			if(function_exists('xdebug_disable'))

				@xdebug_disable();


			try {

				$this->__soapClient = @new SoapClient($this->owner->wsdl, $this->owner->config);

			} catch(SoapFault $e) {

				Yii::log(__METHOD__.' No connection to SoapService: '.$e->getMessage()."\n\n".

					CVarDumper::dumpAsString($this->owner), 'warning', 'soap.behavior');

			}

		}


		return $this->__soapClient;

	}


	public function soapRequest($method, $request) {


		if(empty($this->soapClient))

			return array('success' => false);


		try {

			$ret = $this->soapClient->$method($request);

			$success = true;

		} catch(SoapFault $e) {

			Yii::log(__METHOD__.' soapRequest failed: '.$e->getMessage()."\n\n".

				'$method: '.CVarDumper::dumpAsString($method)."\n\n".

				'$request: '.CVarDumper::dumpAsString($request), 'warning', 'soap.behavior');

			$success = false;

		}


		return array(

			'success' => $success,

			'wsReturn' => $ret,

		);

	}

}



edit: CVarDumper::dumpAsString instead of var_export.

I do not understand. Could you explain it better?

-> ko

I really do not understand

/protected/config/main.php -> ok

-> ko

I really do not understand

-> ko

i have added into class SoapBehavior. Is that correct?

Have you a complete sample?

For example, for use this wsdl:

http://www.w3schools.com/webservices/tempconvert.asmx

how should I do?

I have to create a new class in components?

Unless you are creating webservice components, there isn’t much point in using this behavior:

it is a behavior specifically for webservice components in other words.

If you need to work with soap based web services, use the built-in Yii soap api.

See the docs. ;)

I have found this beautiful extension

http://www.yiiframework.com/extension/wsdl2php