如何使用ZF2创建SOAP服务


How to create SOAP service using ZF2?

我的代码有什么问题?如何为我的数学课创建SOAP服务?
请注意,我没有提到Math.php的命名空间,因为如果我这样做了,我在浏览器上得到了class Math does not exist消息。不提Math类的命名空间如何在indexAction()中创建Math对象。
请指导我如何为数学课创建我的第一个wsdl。

<<p> 文件夹结构/strong>
模块
——Soap
——控制器
——> IndexController.php
——服务
——> Math.php

IndexController.php

include_once __DIR__ . '/../Services/Math.php'
class IndexController extends AbstractActionController
{
  private $_URI = "http://zf2.services/soap";
  public function indexAction()
  {
   $server = new Server(null, array('uri' => $this->_URI));
   $server->setClass('Math');
   //$server->setObject(new Math());
   $server->handle();
  }
}

Math.php

//namespace Soap'Services;
    class Math
    {
       /**
        * Method
        * @return string
        */
       public function greeting()
       {
         return 'Hello world';
       }
    }

导致XML

<SOAP-ENV ..>
 <SOAP-ENV:Body>
  <SOAP-ENV:Fault>
   <faultcode>sender</faultcode>
   <faultstring>Invalid XML</faultstring>
  </SOAP-ENV:Fault>
 </SOAP-ENV:Body>
</SOAP-ENV>

您对Math.php中所写的名称空间的看法是正确的。

在IndexController.php中试试-

include_once __DIR__ . '/../Services/Math.php';
class IndexController extends AbstractActionController {
    private $_URI = "http://zf2.services/soap";
    public function indexAction() {
        $autodiscover = new 'Zend'Soap'AutoDiscover();
        $autodiscover->setClass('Math')
                     ->setBindingStyle(array('style' => 'document'))
                     ->setUri($this->_URI);
        header('Content-type: application/xml');
        echo $autodiscover->toXml();
        exit();
    }
}

我试过了,效果很好