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

  1. Requirements
  2. Usage
  3. The Actual SoapBehavior
  4. Links

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

3 0
9 followers
0 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Web Service
Developed by: hofrob hofrob
Created on: Sep 16, 2011
Last updated: 14 years ago

Related Extensions