如何使用 ZF2 创建 SOAP 服务? [英] How to create SOAP service using ZF2?

查看:27
本文介绍了如何使用 ZF2 创建 SOAP 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

文件夹结构
模块
--肥皂
----控制器
------>IndexController.php
----服务
------>数学.php

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';
       }
    }

结果 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 中编写的命名空间是正确的.

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

在 IndexController.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();
    }
}

我试过了,效果很好.

这篇关于如何使用 ZF2 创建 SOAP 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆