How to create SOAP service using ZF2?

2019-09-08 19:13发布

What is wrong in my code? How to create SOAP service for my Math class?
Please note that, i don't mentioned namespace for Math.php because if i did that i got class Math does not exist message on browser. Without mentioning namespace of Math class how to create Math object in indexAction().
Please guid me how to create my first wsdl for Math class.

Folder structure
Module
--Soap
----Controller
------>IndexController.php
----Services
------>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';
       }
    }

Resulted XML

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

1条回答
SAY GOODBYE
2楼-- · 2019-09-08 19:50

You are right about the namespace written in the Math.php.

Try this in 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();
    }
}

I have tried it and it works fine.

查看更多
登录 后发表回答