带属性的 SOAP 请求

发布于 2024-08-03 16:39:26 字数 937 浏览 4 评论 0原文

我似乎无法找出如何在不使用 XSD_ANYXML 编码的情况下为 SOAP 请求设置属性。

请求参数应如下所示

<request
    xmlns:ns="/some/ns">
    ...
        <ns:parameter attr="some attribute">
            value
        </ns:parameter>
    ...
</request>

当然,以下代码可以工作,但它相当丑陋(丑陋,因为它在应该使用 SOAP_Client API 的地方使用字符串连接,并且因为它不使用通用名称空间)

$param = new SoapVar(
    '<ns_xxx:parameter xmlns:ns_xxx="/some/ns" attr="some attribute">
        value
     </ns_xxx:parameter>',
    XSD_ANYXML
);

有没有更好的方法来创建带有命名空间和属性的 SOAP 请求参数?

我正在寻找某物。如下所示(这只是使用 SoapVar API 的一些伪代码):

$param = new SoapVar(
    array(
        '_' => 'value',
        'attr' => 'some attribute'
    ), 
    SOME_ENCODING,
    null,
    null,
    null,
    '/some/ns'
);

I can not seem to find out how to set an attribute to a SOAP request without using the XSD_ANYXML encoding.

The request parameter should look as follows

<request
    xmlns:ns="/some/ns">
    ...
        <ns:parameter attr="some attribute">
            value
        </ns:parameter>
    ...
</request>

Of course the following code works, but it's rather ugly (ugly, because it uses string concatenation where it should use the SOAP_Client API and because it does not use the general namespace)

$param = new SoapVar(
    '<ns_xxx:parameter xmlns:ns_xxx="/some/ns" attr="some attribute">
        value
     </ns_xxx:parameter>',
    XSD_ANYXML
);

Is there a better way to create a SOAP request parameter with a namespace and an attribute?

I am looking for s.th. like the following (this is just some pseudo code using the SoapVar API):

$param = new SoapVar(
    array(
        '_' => 'value',
        'attr' => 'some attribute'
    ), 
    SOME_ENCODING,
    null,
    null,
    null,
    '/some/ns'
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

许一世地老天荒 2024-08-10 16:39:26

为此,您需要从 SoapClient 派生类并重写方法 __doRequest():

class ABRSoapClient extends SoapClient {

    // return xml request
    function __doRequest($request, $location, $action, $version) {
        $dom = new DOMDocument('1.0', 'UTF-8');
        $dom->preserveWhiteSpace = false;
        $xml= $dom->loadXML($request);
        // Goto request Node and Set the attribute
        $attr_ns = $dom->createAttributeNS('xmlns:ns', '' ); // instead of xmlns:ns use Namespace URL
        $attr_ns->value = '/some/ns';
        // add atribute in businessReport node 
        $dom->getElementsByTagName($report_type)->item(0)->appendChild( $attr_ns );   
        $request = $dom->saveXML();
        return parent::__doRequest($request, $location, $action, $version);
    }
}

$client = new ABRSoapClient(.....);
$save_result = $client->request($param);

// You can check the form request using function
$client->__getLastRequest();

我希望这能解决您的问题。

For this, you need to derived the class from SoapClient and Override the method __doRequest():

class ABRSoapClient extends SoapClient {

    // return xml request
    function __doRequest($request, $location, $action, $version) {
        $dom = new DOMDocument('1.0', 'UTF-8');
        $dom->preserveWhiteSpace = false;
        $xml= $dom->loadXML($request);
        // Goto request Node and Set the attribute
        $attr_ns = $dom->createAttributeNS('xmlns:ns', '' ); // instead of xmlns:ns use Namespace URL
        $attr_ns->value = '/some/ns';
        // add atribute in businessReport node 
        $dom->getElementsByTagName($report_type)->item(0)->appendChild( $attr_ns );   
        $request = $dom->saveXML();
        return parent::__doRequest($request, $location, $action, $version);
    }
}

$client = new ABRSoapClient(.....);
$save_result = $client->request($param);

// You can check the form request using function
$client->__getLastRequest();

I hope this will resolve your problem.

小草泠泠 2024-08-10 16:39:26

SOAP 不支持属性,也许您应该使用 REST!

编辑:
请检查正文样式 w3c:"4.3 SOAP Body" 并记住
您需要使用“soap-envelope”命名空间对消息进行编码并描述
您的 XML 类型这就是为什么您不能使用属性来描述您的消息数据。

但如果你问我,这是可能的!您可以使用自定义 SoapClient 解析器或类似的东西,并根据需要转换消息。
例如,RSS over SOAP http://www.ibm.com /developerworks/webservices/library/ws-soaprdf
但是,问题是您会错过有关您的消息数据/类型的描述性信息,并且其他客户端无法轻松理解您的消息!

我对你的最佳实践是使用元素而不是属性,
我知道您需要修复您的 XML 模式,但这就是它的方式或切换到其他技术。

SOAP does not support attributes, may be you should use REST instead!

EDIT:
Please check the body style w3c:"4.3 SOAP Body" and remember that
you need to encode your message with "soap-envelope" namespace and describe
your XML types thats why, you can't use attributes to describe your message data.

But if you ask me, it can be made possible! You can use a custom SoapClient parser or something like that and convert your message as you like it.
A example of that may be RSS over SOAP http://www.ibm.com/developerworks/webservices/library/ws-soaprdf.
But, the problem would be that you would miss the descriptive information about your message data/types and other clients could not easy understand your messages!

My best practice for you would be to use elements instead of attributes,
i know you need to fix your XML schema but thats the way it goes or switch to a other technology.

欢烬 2024-08-10 16:39:26

SOAP 1 确实支持属性。以下是使用属性和值(来自客户端)的 Perl 代码示例:

$som = $client->call(
    'tran:getContent',
    SOAP::Header->name('cred:credentials')->attr({
        'username' => $username,
        'password' => 'xxx',
        'customerID' => 'xxx'}
    ),
    SOAP::Data->name('contentID')->value('9999')
)

SOAP 1 does support attributes. Here is an example of Perl code using both attributes and values (from a client):

$som = $client->call(
    'tran:getContent',
    SOAP::Header->name('cred:credentials')->attr({
        'username' => $username,
        'password' => 'xxx',
        'customerID' => 'xxx'}
    ),
    SOAP::Data->name('contentID')->value('9999')
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文