2012-06-01 101 views
10

我需要有這個節點,我的SOAP請求(使用1.1):如何定義SoapVar命名空間?

<CredentialsHeader xmlns="http://www.url.com/Services/P24ListingService11" 
    <EMail>[email protected]</EMail> 
    <Password>password</Password> 
</CredentialsHeader> 

所以我有以下的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); 

產生:

<?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> 

所有我需要做的就是防止它使用ns1,並在節點實際上定義了xmlns像這樣:

<CredentialsHeader xmlns="http://www.example.com/Services/Example"> 
     <EMail>[email protected]</EMail> 
     <Password>password</Password> 
    </CredentialsHeader> 

我已經測試在Firefox海報,知道一個事實,即改變解決了這個問題。

回答

10
$CredentialObjectXML = '<CredentialsHeader xmlns="http://www.example.com/Services/Example"> 
     <EMail>'.$UserName.'</EMail> 
     <Password>'.$Password.'</Password> 
    </CredentialsHeader>'; 


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

這樣,您可以直接使用類型爲XSD_ANYXML的XML。

希望這能解決您的問題。

+0

不是最漂亮的,但它的工作!謝謝 – rickyduck

0

我有同樣的問題,結果發現,如果從WSDL映射一個虛擬類的憑據複雜類型,PHP將輸出類似:

<?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> 

這不是被要求什麼,但儘管更詳細的說,它是等價的。

的代碼是這樣的:

$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';