添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

<CredentialsHeader xmlns="http://www.url.com/Services/P24ListingService11"
    <EMail>[email protected]</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' => '[email protected]', '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>[email protected]</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>[email protected]</EMail>
        <Password>password</Password>
    </CredentialsHeader>

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

$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.

Could you write an example? I've been trying for ages to get that parameter working to my example but can't! – rickyduck Jun 6, 2012 at 12:31

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>[email protected]</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 = '[email protected]';
$credentialObject->Password = 'password';
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.