簡體   English   中英

如何使用ZF2創建SOAP服務?

[英]How to create SOAP service using ZF2?

我的代碼有什么問題? 如何為我的Math類創建SOAP服務?
請注意,我沒有提到Math.php的命名空間,因為如果這樣做,我得到的class Math does not exist在瀏覽器class Math does not exist消息。 不提及Math類的命名空間,如何在indexAction()中創建Math對象。
請指導我如何為Math類創建我的第一個wsdl。

資料夾結構
模組
- 肥皂
----控制器
------> 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();
    }
}

我已經嘗試過了,效果很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM