草庐IT

php - zend soap 服务器响应设置自定义 ns1 命名空间

coder 2024-04-14 原文

我正在使用 Zend_Soap_Server(WSDL 模式)输出对客户端调用的 xml 响应。 但是,我想在响应中为 ns1 命名空间设置自定义名称。

我注意到响应中的命名空间默认设置为:“ns1:getDoubleResponse”,其中“getDouble”是被调用的服务器方法。

这是我的 Controller 和 SOAP 服务器设置:

class TestController extends Zend_Controller_Action {

    public function testAction() {

        // diable laoyouts and renderers
        $this->getHelper ( 'viewRenderer' )->setNoRender ( true );
        $server = new Zend_Soap_Server ('http://example.com/public/test/testwsdl'); 
        $server->setClass ( 'Application_Model_test');

        // register exceptions that generate SOAP faults
        $server->registerFaultException('Application_Model_soapException');

        // handle request
        $server->handle ();
    }

    public function testwsdlAction() {
        // diable laoyouts and renderers
        $this->getHelper ( 'viewRenderer' )->setNoRender ( true );      
        $wsdl = new Zend_Soap_AutoDiscover ();

        $wsdl->setClass ( 'Application_Model_test');    
        $wsdl->setUri ('http://example.com/public/test/test');

        // handle request
        $wsdl->handle ();
    }
}

这是我的模型代码:

class Application_Model_test
{
    /**
     * Returns the double of an integer value
     * @param integer $int
     * @return string
     */
    public function getDouble($int)
    {
        $doc = new DOMDocument ( '1.0', 'utf-8' );

        $response = $doc->createElement("IntegerResult");
        $val = $doc->createElement("Value");
        $val->appendChild ($doc->createTextNode($int * 2));     
        $response->appendChild($val);           

        $doc->appendChild ($response);      
        $result = $doc->saveXML();
        return $result;
    }   
}

根据 SOAP UI,这是我看到的请求:

    <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:test="http://example.com/public/test/test">
   <soapenv:Header/>
   <soapenv:Body>
      <test:getDouble soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <int xsi:type="xsd:int" xs:type="type:int" xmlns:xs="http://www.w3.org/2000/XMLSchema-instance">3</int>
      </test:getDouble>
   </soapenv:Body>
</soapenv:Envelope>

根据 SOAP UI,这是关联的响应:

    <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://example.com/public/test/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
      <ns1:getDoubleResponse>
         <return xsi:type="xsd:string">&lt;?xml version="1.0" encoding="utf-8"?>
&lt;IntegerResult>&lt;Value>6&lt;/Value>&lt;/IntegerResult></return>
      </ns1:getDoubleResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我只想更改 <ns1:getDoubleResponse>在 SOAP 中对类似 <ns1:TestResult> 的响应

如何修复命名空间?我不介意通过 DOM 或 Xpath 处理响应。 我也有兴趣扩展 Zend_Soap_Server 以自定义响应。

更新:

我已经用类扩展了 Zend_Soap_Server,现在尝试通过 handle() 方法发送自定义响应。

// TestController.php
//$server = new Zend_Soap_Server ('http://example.com/public/test/testwsdl');
$server = new TestSoapServer ('http://example.com/public/test/testwsdl');

这是扩展 Zend_Soap_Server 并处理响应的类:

// TestController.php
class TestSoapServer extends Zend_Soap_Server 
{
    public function __construct($wsdl, $options = null)
    {
        return parent::__construct($wsdl, $options);
    }

    public function handle($request = null)
    {
      $result = parent::handle($request);
      $result = str_replace("getDoubleResponse", "TestResult", $result);
      return $result;       
    }
}

但是现在,当我在 SOAP UI 中运行请求时,我看到一个空的响应。不知道我做错了什么。

最佳答案

最后,我决定在我的 Controller 中手动解析传入的 SOAP 请求:

// TestController.php
class TestSoapServer extends Zend_Soap_Server 
{
    // Handle the request and generate suitable response    
    public function handle($request = null)
    {
      if (null === $request) {    
       $request = file_get_contents('php://input');
      }
      // Parse request, generate a static/dynamic response and return it.

      // return parent::handle($request); // Actual response

    // Custom response
    $doc = new DOMDocument();
    libxml_use_internal_errors(true);
    $doc->loadHTML($request);
    libxml_clear_errors();
    $xml = $doc->saveXML($doc->documentElement);
    $xml = simplexml_load_string($xml);
    $int = $xml->body->envelope->body->getdouble->int;
    $value = $int * 2;

    $result = '<SOAP-ENV:Envelope 
               SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"             
               xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
               xmlns:ns1="http://example.com/public/test/test" 
               xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
               <SOAP-ENV:Body>
               <ns1:TestResult>';

     $result .= '<IntegerResult><Value>'.$value.'</Value></IntegerResult>';

    $result .= '</ns1:TestResult>
                </SOAP-ENV:Body>
                </SOAP-ENV:Envelope>';

    return $result;
    }
}

关于php - zend soap 服务器响应设置自定义 ns1 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20836908/

有关php - zend soap 服务器响应设置自定义 ns1 命名空间的更多相关文章

  1. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

    我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

  2. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  3. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  4. ruby-on-rails - 启动 Rails 服务器时 ImageMagick 的警告 - 2

    最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru

  5. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  6. ruby-on-rails - 如何重命名或移动 Rails 的 README_FOR_APP - 2

    当我在我的Rails应用程序根目录中运行rakedoc:app时,API文档是使用/doc/README_FOR_APP作为主页生成的。我想向该文件添加.rdoc扩展名,以便它在GitHub上正确呈现。更好的是,我想将它移动到应用程序根目录(/README.rdoc)。有没有办法通过修改包含的rake/rdoctask任务在我的Rakefile中执行此操作?是否有某个地方可以查找可以修改的主页文件的名称?还是我必须编写一个新的Rake任务?额外的问题:Rails应用程序的两个单独文件/README和/doc/README_FOR_APP背后的逻辑是什么?为什么不只有一个?

  7. ruby - 用 Ruby 编写一个简单的网络服务器 - 2

    我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b

  8. ruby-on-rails - 在 Rails 中调试生产服务器 - 2

    您如何在Rails中的实时服务器上进行有效调试,无论是在测试版/生产服务器上?我试过直接在服务器上修改文件,然后重启应用,但是修改好像没有生效,或者需要很长时间(缓存?)我也试过在本地做“脚本/服务器生产”,但是那很慢另一种选择是编码和部署,但效率很低。有人对他们如何有效地做到这一点有任何见解吗? 最佳答案 我会回答你的问题,即使我不同意这种热修补服务器代码的方式:)首先,你真的确定你已经重启了服务器吗?您可以通过跟踪日志文件来检查它。您更改的代码显示的View可能会被缓存。缓存页面位于tmp/cache文件夹下。您可以尝试手动删除

  9. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

  10. ruby-on-rails - 如何生成传递一些自定义参数的 `link_to` URL? - 2

    我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些

随机推荐