Yii Framework Forum: SoapBehavior - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

SoapBehavior useful behavior for working with SoapClient Rate Topic: -----

#1 User is offline   hofrob 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 22-July 10
  • Location:Vienna

Posted 14 September 2011 - 04:53 AM

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.

This post has been edited by yodel: 14 September 2011 - 06:33 AM

0

#2 User is offline   Giancarlo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 249
  • Joined: 20-December 11
  • Location:Turin, Italy

Posted 04 October 2012 - 08:11 AM

I do not understand. Could you explain it better?

Quote

To use it, your component needs the following variables:
-> ko
I really do not understand


/protected/config/main.php -> ok

Quote

In the module....
-> ko
I really do not understand

Quote

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

i have added into class SoapBehavior. Is that correct?


Have you a complete sample?

For example, for use this wsdl:
http://www.w3schools...empconvert.asmx


how should I do?
I have to create a new class in components?
Posted Image Non preoccuparti, è dentro il monitor!

Sapevi che cercando su google yii2 + "la funzione su cui stai impazzendo", trovi quasi sempre dei buoni indizi in italiano, per risolvere i tuoi problemi, senza perdere tempo a postare?
0

#3 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 04 October 2012 - 08:27 AM

View PostGiancarlo, on 04 October 2012 - 08:11 AM, said:

I do not understand. Could you explain it better?


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. ;)
"Less noise - more signal"
0

#4 User is offline   Giancarlo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 249
  • Joined: 20-December 11
  • Location:Turin, Italy

Posted 04 October 2012 - 09:18 AM

I have found this beautiful extension
http://www.yiiframew...ension/wsdl2php
Posted Image Non preoccuparti, è dentro il monitor!

Sapevi che cercando su google yii2 + "la funzione su cui stai impazzendo", trovi quasi sempre dei buoni indizi in italiano, per risolvere i tuoi problemi, senza perdere tempo a postare?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users