system system.base system.caching system.caching.dependencies system.collections system.console system.db system.db.ar system.db.schema system.db.schema.cubrid system.db.schema.mssql system.db.schema.mysql system.db.schema.oci system.db.schema.pgsql system.db.schema.sqlite system.gii system.i18n system.i18n.gettext system.logging system.test system.utils system.validators system.web system.web.actions system.web.auth system.web.filters system.web.form system.web.helpers system.web.renderers system.web.services system.web.widgets system.web.widgets.captcha system.web.widgets.pagers zii.behaviors zii.widgets zii.widgets.grid zii.widgets.jui

CWebService

system.web.services
继承 class CWebService » CComponent
可用自 1.0
源码 framework/web/services/CWebService.php
CWebService encapsulates SoapServer and provides a WSDL-based web service.

PHP SOAP extension is required.

CWebService makes use of CWsdlGenerator and can generate the WSDL on-the-fly without requiring you to write complex WSDL. However WSDL generator could be customized through generatorConfig property.

To generate the WSDL based on doc comment blocks in the service provider class, call generateWsdl or renderWsdl. To process the web service requests, call run.

公共属性

隐藏继承的属性

属性类型描述被定义在
actor string actor of the SOAP service. CWebService
cacheID string the ID of the cache application component that is used to cache the generated WSDL. CWebService
classMap array a list of classes that are declared as complex types in WSDL. CWebService
encoding string encoding of the Web service. CWebService
generatorConfig string|array WSDL generator configuration. CWebService
methodName string the currently requested method name. CWebService
persistence integer the persistence mode of the SOAP server. CWebService
provider string|object the web service provider class or object. CWebService
serviceUrl string the URL for the Web service. CWebService
soapVersion string SOAP version (e.g. '1. CWebService
wsdlCacheDuration integer number of seconds that the generated WSDL can remain valid in cache. CWebService
wsdlUrl string the URL for WSDL. CWebService

受保护的属性

隐藏继承的属性

属性类型描述被定义在
options array options for creating SoapServer instance CWebService

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CWebService
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
generateWsdl() Generates the WSDL as defined by the provider. CWebService
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getMethodName() Returns the currently requested method name. Empty if no method is being requested. CWebService
handleError() The PHP error handler. CWebService
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
raiseEvent() Raises an event. CComponent
renderWsdl() Generates and displays the WSDL as defined by the provider. CWebService
run() Handles the web service request. CWebService

受保护的方法

隐藏继承的方法

方法描述被定义在
getOptions() Returns options for creating SoapServer instance CWebService

属性详情

actor 属性
public string $actor;

actor of the SOAP service. Defaults to null, meaning not set.

cacheID 属性
public string $cacheID;

the ID of the cache application component that is used to cache the generated WSDL. Defaults to 'cache' which refers to the primary cache application component. Set this property to false if you want to disable caching WSDL.

classMap 属性
public array $classMap;

a list of classes that are declared as complex types in WSDL. This should be an array with WSDL types as keys and names of PHP classes as values. A PHP class can also be specified as a path alias.

encoding 属性
public string $encoding;

encoding of the Web service. Defaults to 'UTF-8'.

generatorConfig 属性 (自版本 v1.1.12 可用)
public string|array $generatorConfig;

WSDL generator configuration. This property may be useful in purpose of enhancing features of the standard CWsdlGenerator class by extending it. For example, some developers may need support of the xsd:xsd:base64Binary elements. Another use case is to change initial values at instantiation of the default CWsdlGenerator. The value of this property will be passed to Yii::createComponent to create the generator object. Default value is 'CWsdlGenerator'.

methodName 属性 只读
public string getMethodName()

the currently requested method name. Empty if no method is being requested.

options 属性 只读
protected array getOptions()

options for creating SoapServer instance

persistence 属性
public integer $persistence;

the persistence mode of the SOAP server.

provider 属性
public string|object $provider;

the web service provider class or object. If specified as a class name, it can be a path alias.

serviceUrl 属性
public string $serviceUrl;

the URL for the Web service. This is required by generateWsdl() and renderWsdl().

soapVersion 属性
public string $soapVersion;

SOAP version (e.g. '1.1' or '1.2'). Defaults to null, meaning not set.

wsdlCacheDuration 属性
public integer $wsdlCacheDuration;

number of seconds that the generated WSDL can remain valid in cache. Defaults to 0, meaning no caching.

wsdlUrl 属性
public string $wsdlUrl;

the URL for WSDL. This is required by run().

方法详情

__construct() 方法
public void __construct(mixed $provider, string $wsdlUrl, string $serviceUrl)
$provider mixed the web service provider class name or object
$wsdlUrl string the URL for WSDL. This is required by run().
$serviceUrl string the URL for the Web service. This is required by generateWsdl() and renderWsdl().
源码: framework/web/services/CWebService.php#99 (显示)
public function __construct($provider,$wsdlUrl,$serviceUrl)
{
    
$this->provider=$provider;
    
$this->wsdlUrl=$wsdlUrl;
    
$this->serviceUrl=$serviceUrl;
}

Constructor.

generateWsdl() 方法
public string generateWsdl()
{return} string the generated WSDL
源码: framework/web/services/CWebService.php#142 (显示)
public function generateWsdl()
{
    
$providerClass=is_object($this->provider) ? get_class($this->provider) : Yii::import($this->provider,true);
    if(
$this->wsdlCacheDuration>&& $this->cacheID!==false && ($cache=Yii::app()->getComponent($this->cacheID))!==null)
    {
        
$key='Yii.CWebService.'.$providerClass.$this->serviceUrl.$this->encoding;
        if((
$wsdl=$cache->get($key))!==false)
            return 
$wsdl;
    }
    
$generator=Yii::createComponent($this->generatorConfig);
    
$wsdl=$generator->generateWsdl($providerClass,$this->serviceUrl,$this->encoding);
    if(isset(
$key))
        
$cache->set($key,$wsdl,$this->wsdlCacheDuration);
    return 
$wsdl;
}

Generates the WSDL as defined by the provider. The cached version may be used if the WSDL is found valid in cache.

getMethodName() 方法
public string getMethodName()
{return} string the currently requested method name. Empty if no method is being requested.
源码: framework/web/services/CWebService.php#236 (显示)
public function getMethodName()
{
    if(
$this->_method===null)
    {
        
// before PHP 5.6 php://input could be read only once
        // since PHP 5.6 $HTTP_RAW_POST_DATA is deprecated
        
if(version_compare(PHP_VERSION'5.6.0''<') && isset($HTTP_RAW_POST_DATA))
            
$request=$HTTP_RAW_POST_DATA;
        else
            
$request=file_get_contents('php://input');
        if(
preg_match('/<.*?:Body[^>]*>\s*<.*?:(\w+)/mi',$request,$matches))
            
$this->_method=$matches[1];
        else
            
$this->_method='';
    }
    return 
$this->_method;
}

getOptions() 方法
protected array getOptions()
{return} array options for creating SoapServer instance
源码: framework/web/services/CWebService.php#258 (显示)
protected function getOptions()
{
    
$options=array();
    if(
$this->soapVersion==='1.1')
        
$options['soap_version']=SOAP_1_1;
    elseif(
$this->soapVersion==='1.2')
        
$options['soap_version']=SOAP_1_2;
    if(
$this->actor!==null)
        
$options['actor']=$this->actor;
    
$options['encoding']=$this->encoding;
    foreach(
$this->classMap as $type=>$className)
    {
        
$className=Yii::import($className,true);
        if(
is_int($type))
            
$type=$className;
        
$options['classmap'][$type]=$className;
    }
    return 
$options;
}

handleError() 方法
public void handleError(CErrorEvent $event)
$event CErrorEvent the PHP error event
源码: framework/web/services/CWebService.php#111 (显示)
public function handleError($event)
{
    
$event->handled=true;
    
$message=$event->message;
    if(
YII_DEBUG)
    {
        
$trace=debug_backtrace();
        if(isset(
$trace[2]) && isset($trace[2]['file']) && isset($trace[2]['line']))
            
$message.=' ('.$trace[2]['file'].':'.$trace[2]['line'].')';
    }
    throw new 
CException($message,self::SOAP_ERROR);
}

The PHP error handler.

renderWsdl() 方法
public void renderWsdl()
源码: framework/web/services/CWebService.php#128 (显示)
public function renderWsdl()
{
    
$wsdl=$this->generateWsdl();
    
header('Content-Type: text/xml;charset='.$this->encoding);
    
header('Content-Length: '.(function_exists('mb_strlen') ? mb_strlen($wsdl,'8bit') : strlen($wsdl)));
    echo 
$wsdl;
}

Generates and displays the WSDL as defined by the provider.

参见

run() 方法
public void run()
源码: framework/web/services/CWebService.php#161 (显示)
public function run()
{
    
header('Content-Type: text/xml;charset='.$this->encoding);
    if(
YII_DEBUG)
        
ini_set("soap.wsdl_cache_enabled",0);
    
$server=new SoapServer($this->wsdlUrl,$this->getOptions());
    
Yii::app()->attachEventHandler('onError',array($this,'handleError'));
    try
    {
        if(
$this->persistence!==null)
            
$server->setPersistence($this->persistence);
        if(
is_string($this->provider))
            
$provider=Yii::createComponent($this->provider);
        else
            
$provider=$this->provider;

        if(
method_exists($server,'setObject'))
        {
            if (
is_array($this->generatorConfig) && isset($this->generatorConfig['bindingStyle'])
                && 
$this->generatorConfig['bindingStyle']==='document')
            {
                
$server->setObject(new CDocumentSoapObjectWrapper($provider));
            }
            else
            {
                
$server->setObject($provider);
            }
        }
        else
        {
            if (
is_array($this->generatorConfig) && isset($this->generatorConfig['bindingStyle'])
                && 
$this->generatorConfig['bindingStyle']==='document')
            {
                
$server->setClass('CDocumentSoapObjectWrapper',$provider);
            }
            else
            {
                
$server->setClass('CSoapObjectWrapper',$provider);
            }
        }

        if(
$provider instanceof IWebServiceProvider)
        {
            if(
$provider->beforeWebMethod($this))
            {
                
$server->handle();
                
$provider->afterWebMethod($this);
            }
        }
        else
            
$server->handle();
    }
    catch(
Exception $e)
    {
        if(
$e->getCode()!==self::SOAP_ERROR// non-PHP error
        
{
            
// only log for non-PHP-error case because application's error handler already logs it
            // php <5.2 doesn't support string conversion auto-magically
            
Yii::log($e->__toString(),CLogger::LEVEL_ERROR,'application');
        }
        
$message=$e->getMessage();
        if(
YII_DEBUG)
            
$message.=' ('.$e->getFile().':'.$e->getLine().")\n".$e->getTraceAsString();

        
// We need to end application explicitly because of
        // http://bugs.php.net/bug.php?id=49513
        
Yii::app()->onEndRequest(new CEvent($this));
        
$server->fault(get_class($e),$message);
        exit(
1);
    }
}

Handles the web service request.