CWebServiceAction.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * CWebServiceAction class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CWebServiceAction implements an action that provides Web services.
  12. *
  13. * CWebServiceAction serves for two purposes. On the one hand, it displays
  14. * the WSDL content specifying the Web service APIs. On the other hand, it
  15. * invokes the requested Web service API. A GET parameter named <code>ws</code>
  16. * is used to differentiate these two aspects: the existence of the GET parameter
  17. * indicates performing the latter action.
  18. *
  19. * By default, CWebServiceAction will use the current controller as
  20. * the Web service provider. See {@link CWsdlGenerator} on how to declare
  21. * methods that can be remotely invoked.
  22. *
  23. * Note, PHP SOAP extension is required for this action.
  24. *
  25. * @property CWebService $service The Web service instance.
  26. *
  27. * @author Qiang Xue <qiang.xue@gmail.com>
  28. * @package system.web.services
  29. * @since 1.0
  30. */
  31. class CWebServiceAction extends CAction
  32. {
  33. /**
  34. * @var mixed the Web service provider object or class name.
  35. * If specified as a class name, it can be a path alias.
  36. * Defaults to null, meaning the current controller is used as the service provider.
  37. * If the provider implements the interface {@link IWebServiceProvider},
  38. * it will be able to intercept the remote method invocation and perform
  39. * additional tasks (e.g. authentication, logging).
  40. */
  41. public $provider;
  42. /**
  43. * @var string the URL for the Web service. Defaults to null, meaning
  44. * the URL for this action is used to provide Web services.
  45. * In this case, a GET parameter named {@link serviceVar} will be used to
  46. * deteremine whether the current request is for WSDL or Web service.
  47. */
  48. public $serviceUrl;
  49. /**
  50. * @var string the URL for WSDL. Defaults to null, meaning
  51. * the URL for this action is used to serve WSDL document.
  52. */
  53. public $wsdlUrl;
  54. /**
  55. * @var string the name of the GET parameter that differentiates a WSDL request
  56. * from a Web service request. If this GET parameter exists, the request is considered
  57. * as a Web service request; otherwise, it is a WSDL request. Defaults to 'ws'.
  58. */
  59. public $serviceVar='ws';
  60. /**
  61. * @var array a list of PHP classes that are declared as complex types in WSDL.
  62. * This should be an array with WSDL types as keys and names of PHP classes as values.
  63. * A PHP class can also be specified as a path alias.
  64. * @see http://www.php.net/manual/en/soapclient.soapclient.php
  65. */
  66. public $classMap;
  67. /**
  68. * @var array the initial property values for the {@link CWebService} object.
  69. * The array keys are property names of {@link CWebService} and the array values
  70. * are the corresponding property initial values.
  71. */
  72. public $serviceOptions=array();
  73. private $_service;
  74. /**
  75. * Runs the action.
  76. * If the GET parameter {@link serviceVar} exists, the action handle the remote method invocation.
  77. * If not, the action will serve WSDL content;
  78. */
  79. public function run()
  80. {
  81. $hostInfo=Yii::app()->getRequest()->getHostInfo();
  82. $controller=$this->getController();
  83. if(($serviceUrl=$this->serviceUrl)===null)
  84. $serviceUrl=$hostInfo.$controller->createUrl($this->getId(),array($this->serviceVar=>1));
  85. if(($wsdlUrl=$this->wsdlUrl)===null)
  86. $wsdlUrl=$hostInfo.$controller->createUrl($this->getId());
  87. if(($provider=$this->provider)===null)
  88. $provider=$controller;
  89. $this->_service=$this->createWebService($provider,$wsdlUrl,$serviceUrl);
  90. if(is_array($this->classMap))
  91. $this->_service->classMap=$this->classMap;
  92. foreach($this->serviceOptions as $name=>$value)
  93. $this->_service->$name=$value;
  94. if(isset($_GET[$this->serviceVar]))
  95. $this->_service->run();
  96. else
  97. $this->_service->renderWsdl();
  98. Yii::app()->end();
  99. }
  100. /**
  101. * Returns the Web service instance currently being used.
  102. * @return CWebService the Web service instance
  103. */
  104. public function getService()
  105. {
  106. return $this->_service;
  107. }
  108. /**
  109. * Creates a {@link CWebService} instance.
  110. * You may override this method to customize the created instance.
  111. * @param mixed $provider the web service provider class name or object
  112. * @param string $wsdlUrl the URL for WSDL.
  113. * @param string $serviceUrl the URL for the Web service.
  114. * @return CWebService the Web service instance
  115. */
  116. protected function createWebService($provider,$wsdlUrl,$serviceUrl)
  117. {
  118. return new CWebService($provider,$wsdlUrl,$serviceUrl);
  119. }
  120. }