Yii 1.1: soapbehavior

A behavior that manages everything SOAP related. Connection, errors (WSDL not found, timeout)
10 followers

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). Also try/catch works even when xdebug is enabled (see Issue 609)

Requirements

Yii 1.1.8 or above (tested ONLY with 1.1.8, though it's nothing fancy and should work with every 1.1 version)

Usage

Webservice Component

Required Properties

To use it, your component needs the following variables:

public $wsdl;
public $config = array();
public $behaviors = array(
    'soapBehavior' => array(
        'class' => 'ext.SoapBehavior'
    ),
);

Config

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

'modules' => array(
    'moduleName' => array(
        'wsConfig' => array(
            'wsdl' => 'http://example.com/wsdl',
            'config' => array(
                'proxyHost' => 'example.com',
                'proxyPort' => '8080',
            ),
        ),
    ),

Set Component In Module

$this->setComponents(array(
    'compName' =>
        CMap::mergeArray(array('class' => 'module.components.WSComponent'), $this->wsConfig),
));

SOAP Call

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

The Actual SoapBehavior

<?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,
        );
    }
}

Links

GitHub

Total 2 comments

#7035 report it
hofrob at 2012/02/20 11:49am
it's arbitrary

"does that get referenced somewhere else?"

No, the only place I used this variable is in the init method of the module (and in the config of course). So you can use more than 1 configs in the same module (with different variable names for example).

But to be honest I am not initializing the component that way anymore. One advantage of registering your webservices as a "normal" component is you can access it from anywhere in the application with

Yii::app()->componentName

For example I had 2 nested modules and when I wanted to access the component within a (form)model of one of the modules I had to use

Yii::app()->getModule('firstModule')->getModule('secondModule')->componentName

hth

#7033 report it
meadows at 2012/02/20 11:32am
Multiple wsdl files

I see in your config you use wsConfig to name your wsdl configuration information. I am starting on an app that uses 5 different wsdl files, is that name arbitrary so I can just use one with significance or does that get referenced somewhere else?

Leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet