How to define a SoapVar namespace?

2020-06-12 03:24发布

I need to have this node in my SOAP Request (using 1.1):

<CredentialsHeader xmlns="http://www.url.com/Services/P24ListingService11"
    <EMail>ricky@email.net</EMail>
    <Password>password</Password>
</CredentialsHeader>

So I have the following PHP:

$client = new SoapClient("https://exdev.www.example.com/Services/example.asmx?WSDL", 
    array(
        "trace"      => 1,
        "exceptions" => 0,
        "cache_wsdl" => 0,
        'soap_version' => SOAP_1_1
        )
);

$CredentialObject = new SoapVar(array('EMail' => 'ricky@email.net', 'Password' => 'password'), SOAP_ENC_OBJECT);

Which generates:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.example.com/Services/Example">
    <SOAP-ENV:Header>
        <ns1:CredentialsHeader>
            <EMail>ricky@email.net</EMail>
            <Password>password</Password>
        </ns1:CredentialsHeader>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:EchoAuthenticated/>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

All I need to do is prevent it using ns1 and actually define the xmlns in the node like so:

<CredentialsHeader xmlns="http://www.example.com/Services/Example">
        <EMail>ricky@email.net</EMail>
        <Password>password</Password>
    </CredentialsHeader>

I have tested that in Firefox Poster and know for a fact that change fixes the problem.

3条回答
欢心
2楼-- · 2020-06-12 03:49

http://www.php.net/manual/tr/soapvar.soapvar.php

Parameter "node_namespace" is what you've been looking for i guess.

查看更多
ら.Afraid
3楼-- · 2020-06-12 03:49

I had the same problem and found out that if you map a dummy class to the credential complex type from your WSDL, PHP will output something like:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.example.com/Services/Example">
    <SOAP-ENV:Header>
        <ns1:CredentialsHeader>
            <ns1:EMail>ricky@email.net</ns1:EMail>
            <ns1:Password>password</ns1:Password>
        </ns1:CredentialsHeader>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns1:EchoAuthenticated/>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

This is not exactly what was requested but although more verbose, it is equivalent.

The code goes like this:

$client = new SoapClient("https://exdev.www.example.com/Services/example.asmx?WSDL", 
    array(
        "trace"         => 1,
        "exceptions"    => 0,
        "cache_wsdl"    => 0,
        "soap_version"  => SOAP_1_1,
        "classmap"      => array(
            'credential_complex_type'   => 'CredentialObject',
        ),
    )
);

class CredentialObject {}

$credentialObject = new CredentialObject();
$credentialObject->Email = 'ricky@email.net';
$credentialObject->Password = 'password';
查看更多
聊天终结者
4楼-- · 2020-06-12 04:00
$CredentialObjectXML  = '<CredentialsHeader xmlns="http://www.example.com/Services/Example">
        <EMail>'.$UserName.'</EMail>
        <Password>'.$Password.'</Password>
    </CredentialsHeader>';


$CredentialObject  = new SoapVar($CredentialObjectXML,XSD_ANYXML);

This way you can directly use the XML with Type XSD_ANYXML.

Hope this will resolve your problem.

查看更多
登录 后发表回答