如何从 php SOAPServer 中的 SOAP 请求的 XML 内容中捕获数据? [英] How do I catch data from the XML content of a SOAP request in a php SOAPServer?

查看:27
本文介绍了如何从 php SOAPServer 中的 SOAP 请求的 XML 内容中捕获数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 php 中设置 SOAP 服务.我声明了一个服务器 php 函数,我可以使用 SOAP 类型的 http 请求调用该函数,其中内容是我的 SOAP 信封.

I am trying to set up a SOAP service in php. I declared a server php function and I am able to call that function with a SOAP type http request where the content is my SOAP envelope.

SOAP 正文的 XML 内容是我假设的函数的参数,但我不知道如何在我的 php 代码中访问其中的信息.

The XML content of the SOAP body is the argument of the function I assume, but I don't know how to access the information in it in my php code.

我注意到函数参数默认是 stdClass 的一个实例,我实际上想知道为什么它没有被转换为 XMLDOM object by php - 这是一个 SOAP 调用,不是吗?但是好吧,现在由我来从对象中获取信息,这并不容易,因为没有分配给 stdClass 的方法,所以它必须是标准的 php 函数.所以我尝试了 serialize,但这给了我一些垃圾,而不是我期望的 XML 字符串.

I noticed that the function argument is an instance of stdClass by default, and I actually wonder why it is not casted on an XML or DOM object by php - it's a SOAP call isn't it? But all right, now it's up to me to get the information out of the object, which is not easy because there's no methods assigned to stdClass, so it'll have to be standard php functions. So I tried serialize, but this gave me some rubbish, not the XML string I expected.

怎么办?

编辑

注意,下面没有我想要做的示例代码 - 从 SOAP 请求的 XML 内容中获取一些详细数据 - 因为我不知道如何编码从 stdClass 对象中获取它

应 david 的要求,这里有一些详细信息.

On request of david, here's some details.

php 代码:

<?php
    function mi102($arg) {
        $txt = serialize ($arg);
        $result = new SoapVar ($txt, XSD_ANYXML);
        return($result);
    }
    ini_set( "soap.wsdl_cache_enabled", "0");
    $server = new SoapServer ("test.wsdl");
    $server -> addFunction ("mi102");
    try {
        $server -> handle();
    }
    catch (Exception $e) {
        $server -> fault ('Client', $e -> getMessage());
    }
?php>

http 请求是由我使用的应用程序构建的;http标头和soap信封+正文是围绕我提供的XML生成的:

http request is constructed by an application that I use; the http header and the soap envelope + body are generated around the XML I feed it:

SOAP 请求正文内容:

SOAP request body content:

<mi102 xmlns="http://pse">
  <cdhead cisprik="21"/>
  <instr>
    <insid>
      <bcdt>20120930</bcdt>
    </insid>
  </instr>
</mi102>

使用的WSDL如下:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://pse/" xmlns:xs="http://www.w3.org/2001/XMLSchema" name="PSE" targetNamespace="http://pse/">
    <types>
        <xs:schema>
            <xs:import namespace="http://pse/" schemaLocation="PSE.xsd"/>
        </xs:schema>
    </types>
    <message name="MI102Req">
        <part name="cdhead" type="tns:cdhead_T"/>
        <part name="instr" type="tns:instr_T"/>
    </message>
    <message name="Res">
        <part name="cdhead" type="tns:cdhead_T"/>
    </message>
    <portType name="MIPortType">
        <operation name="mi102">
            <input message="tns:MI102Req"/>
            <output message="tns:Res"/>
        </operation>
    </portType>
    <binding name="MIBinding" type="tns:MIPortType">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="mi102">
            <soap:operation soapAction="http://testServerURL/test_soap.php#mi102"/>
            <input>
                <soap:body use="literal" namespace="http://pse/"/>
            </input>
            <output>
                <soap:body use="literal" namespace="http://pse/"/>
            </output>
        </operation>
    </binding>
    <service name="PSE">
        <port name="MIPortType" binding="tns:MIBinding">
            <soap:address location="http://testServerURL/test_soap.php"/>
        </port>
    </service>
</definitions>

生成的 XML(同样是我使用的应用程序从 SOAP 主体中提取的)是

And the resulting XML (again, extracted from the SOAP body by the application I use), is

SOAP 响应:

<?xml version="1.0" encoding="UTF-8"?>
<ns1:mi102Response xmlns:ns1="http://pse/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">O:8:"stdClass":2:{s:7:"cisprik";i:21;s:7:"version";s:2:"13";}</ns1:mi102Response>

不太好.

推荐答案

我最终在 SO 的其他线程中找到了答案,例如 get-received-xml-from-php-soap-server

I eventually found the answer in other threads on SO, like get-recieved-xml-from-php-soap-server

解决方案是使用以下方法:

The solution is to use the following:

$inp = file_get_contents ('php://input');

注意:我找不到任何可以对 stdClass 输入参数进行操作并且可以从中检索 XML SOAP Body 内容的函数.
所以最好的选择是使用标准的 php 输入变量.请注意,它具有以下结构:Envelope/Body/..inputXML..,这是发布到服务器的确切 http 请求内容.

Note: I could not find any function that can act on the stdClass input argument and can retrieve the XML SOAP Body contents from it.
So the best option is to use the standard php input variable. Note that this has the following structure: Envelope/Body/..inputXML.., which is the exact http request content that is posted to the server.

注意:http_get_request_body 也可以,但是我的 php 服务器不支持这个功能.我认为从某些版本开始,每个 php 服务器都支持 file_get_contents.

Note: http_get_request_body may work too, but my php server did not support this function. I think that file_get_contents is supported on every php server from some version onwards.

这篇关于如何从 php SOAPServer 中的 SOAP 请求的 XML 内容中捕获数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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