Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

php - SOAP request with attribute

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'
);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...