企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
YurunPHP内置Soap支持,可以搭配PHP的SoapServer类使用。 由于文字可能表述不清,大多数内容将以代码形式展现。 #### 配置文件 添加自动加载 ~~~ 'AUTOLOAD_RULES' => array( array('type'=>'Word','word'=>'SoapControl','path'=>'Ex/Lib/Soap'), array('type'=>'Word','word'=>'SoapParser','path'=>'Ex/Lib/Soap'), array('type'=>'Word','word'=>'SoapProxy','path'=>'Ex/Lib/Soap'), array('type'=>'Word','word'=>'ClassToWsdl','path'=>'Ex/Lib/Soap/PHPWsdl'), array('type'=>'Word','word'=>'WSDL','path'=>'Ex/Lib/Soap/PHPWsdl'), ), ~~~ #### 控制器 ~~~ <?php class ServiceControl extends SoapControl { /** * 服务首页动作名 */ protected $index = 'index'; /** * wsdl文档动作名 */ protected $wsdl = 'wsdl'; /** * 服务接口执行动作名 */ protected $exec = 'exec'; /** * 服务接口测试页面动作名 */ protected $test = 'test'; /** * wsdl中的Style */ protected $wsdlStyle = 'document'; /** * wsdl中的use */ protected $wsdlUse = 'literal'; /** * 是否启用缓存 */ protected $cacheStatus = true; /** * 友好展示页面 * @param mixed $serviceName * @return mixed */ public function index($serviceName) { $this->__index($serviceName); } /** * wsdl文件 * @param mixed $serviceName * @return mixed */ public function wsdl($serviceName) { $this->__wsdl($serviceName); } /** * webservice调用入口 * @param mixed $serviceName * @return mixed */ public function exec($serviceName) { $this->__exec($serviceName); } /** * 测试入口 * @param mixed $serviceName * @param mixed $methodName * @return mixed */ public function test($serviceName,$methodName) { $this->__test($serviceName,$methodName); } } ~~~ #### 路由 路由这一步其实无关紧要,使用默认的规则访问也可以,如果想要Url美观,可以参考我给的示例自行修改。 ~~~ 'rules' => array( 'Service/[serviceName:word]/[methodName:word]/test' => 'Test/Service/test', 'Service/[serviceName:word]/[action:word]' => 'Test/Service/$2', 'Service/[serviceName:word]' => 'Test/Service/index', ) ~~~ 这样配置,路径就是这样的 > index:http://xxx.com/Service/服务名 > wsdl:http://xxx.com/Service/服务名/wsdl > exec:http://xxx.com/Service/服务名/exec > test:http://xxx.com/Service/服务名/方法名/test #### 服务文件 一般放在对应模块的Lib目录下 `Test.class.php` ~~~ <?php /** * 测试接口 * @namespace http://www.baidu.com/ */ class Test { /** * 加法1 * @soap * @param int $a1 数字1 * @param int $b1 数字2 * @return int */ public function add($a1,$b1) { return $a1 + $b1; } /** * 减法 * @soap * @param int $a 数字1 * @param int $b 数字2 * @return int[] */ public function sub($a,$b) { return array($a,$b); } public function test1() { } private function test2() { } } ~~~ > namespace可以自行修改 > 开放出来的soap方法只有add和sub,也就是依靠注释来识别哪个是soap方法。 > 参数和返回值一定要详细注释,否则生成的wsdl不对,调用不出来。