在yii中soap协议的配置和使用
作者:zccst
1,定义Service Provider
Yii依靠文档注解(doc comments)和类反射(class reflection)来识别哪个方法可以被远程调用,包括他们的参数和返回值。
我们定义服务器端的类,继承CController,如下面的例子所示:
class StockController extends CController{/*** @param String the symbol of the stock* @return float the stock price* @soap*/public function getPrice($symbol){$prices = array('IBM'=>100, 'GOOGLE'=>350);return isset($prices[$symbol]) ? $prices[$symbol] : 0;// ... return stock price for $symbol}}
class StockController extends CController{/*********** 增加 start **************/public function actions(){return array('quote'=>array('class'=>'CWebServiceAction'),);}/*********** 增加 end **************//*** @param String the symbol of the stock* @return float the stock price* @soap*/public function getPrice($symbol){$prices = array('IBM'=>100, 'GOOGLE'=>350);return isset($prices[$symbol]) ? $prices[$symbol] : 0;// ... return stock price for $symbol}}
$client = new SoapClient('http://hostname/path/to/index.php?r=stock/quote');echo $client->getPrice('GOOGLE');//运行上面的脚本,通过Web或命令行方式,可以得出结果:350
class outsource_header {/** * @var string 访问密钥, a和b之间的访问控制 * @soap */public $token;/** * @var integer 外包单号 * @soap */public $id;/** * @var string 该外包单对应的机房名称 * @soap */public $idc_name;/** * @var outsource_handler 处理人信息 * @soap */public $handler;// 处理人信息}
class XXController extends CController{public function actions(){return array('service'=>array('class'=>'CWebServiceAction',//增加 classMap 配置项'classMap'=>array('outsource_header',),),);}}
class PostController extends CController{/*** @return Post[] a list of posts* @soap*/public function getPosts(){return Post::model()->findAll();}}
class Post extends CActiveRecord{/** * @var integer post ID * @soap */public $id;/** * @var string post title * @soap */public $title;public static function model($className=__CLASS__){return parent::model($className);}}