Yii2中使用Soap WebSerivce 很是简单,有人已经提供了相关的扩展可供咱们使用,固然也能够本身写,下面就拿别人写好的扩展来讲明怎么在yii2中使用soapweb
注意:能正常使用soap的前提条件是记得打开PHP的soap扩展 json
一、首先安装soap服务端扩展(SOAP Server Extension)api
1 composer require --prefer-dist mongosoft/yii2-soap-server "*"
a、您须要将[[mongosoft \ soapserver \ Action]]添加到你的控制器。yii2
注意:在你的服务类中,可调用的方法必须是包含'@soap'标签的doc注释块的公共方法网络
1 class ApiController extends Controller 2 { 3 /** 4 * @inheritdoc 5 */ 6 public function actions() 7 { 8 return [ 9 'hello' => 'mongosoft\soapserver\Action', 这样配置以后,就能够经过http://www.myservice.com/api/hello hello即前面的键 10 ]; 11 } 12 13 /** 14 * @param string $name 15 * @return string 16 * @soap #这里必须是包含'@soap'标签的doc注释块,不然生成的wsdl文件中将不会展现这个方法 17 */ 18 public function getHello($name) 19 { 20 return 'Hello ' . $name; 21 } 22 }
b、若是你想禁用SoapService的WSDL模式,能够经过serviceOptions参数来设置app
1 /** 2 * @inheritdoc 3 */ 4 public function actions() 5 { 6 return [ 7 'index' => [ 8 'class' => 'mongosoft\soapserver\Action', 9 'serviceOptions' => [ 10 'disableWsdlMode' => true, 11 ] 12 ] 13 ]; 14 }
二、安装soap客户端扩展composer
1 composer require --prefer-dist mongosoft/yii2-soap-client "*"
a、配置:yii
1 'components' => [ 2 'siteApi' => [ 3 'class' => 'mongosoft\soapclient\Client', 4 'url' => 'http://myservice.com/api/hello', 5 'options' => [ 6 'cache_wsdl' => WSDL_CACHE_NONE, 7 ], 8 ] 9 ... 10 ]
或者也能够直接在代码中使用:ui
1 $client = new \mongosoft\soapclient\Client([ 2 'url' => 'http://myservice.com/api/hello', 3 ]);
b、使用url
1 $client = Yii::$app->siteApi; 2 echo $client->getHello('Alex');