2015-10-19 23 views
3

我試圖與在SOAP調用多個信封的xmlns命名空間SOAP調用,但我無法弄清楚如何正確地做到這一點...PHP - 使用多個的xmlns在信封與SoapClient的

下面的代碼我現在所擁有的:

$soapClient = new SoapClient(WSDL_URL, array(
      "trace" => true, 
      "exceptions" => true 
     )); 
$soapClient->__setLocation(WSDL_LOCATION); 

$request = ' 
     <ns1:someNodeName xmlns="http://some.webservice.url.com"> 
      <ns2:someOtherNodeName> 
       // [REQUEST DETAILS] 
      </ns2:someOtherNodeName> 
     </ns1:someNodeName> 
    '; 

$params = new SoapVar($request, XSD_ANYXML); 

try { 
    $results = $soapClient->someFunctionName($params); 
    return $results; 
} 
catch (Exception $e) { 
    $error_xml = $soapClient->__getLastRequest(); 
    echo $error_xml . "\n"; 
    echo $e->getMessage() . "\n"; 
} 

此代碼給了我像一個XML請求如下:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://some.webservice.url.com"> 
    <SOAP-ENV:Body> 
     <ns1:someNodeName xmlns="http://some.webservice.url.com"> 
      <ns2:someOtherNodeName> 
       // [REQUEST DETAILS] 
      </ns2:someOtherNodeName> 
     </ns1:someNodeName> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

我試圖改變的是包絡線,要達到這樣的:

<SOAP-ENV:Envelope 
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns1="http://some.webservice.url.com" 
    xmlns:ns2="http://some.other.webservice.url.com" 
> 

請問有什麼辦法可以實現這個嗎?

回答

1

XMLNS屬性是名稱空間定義,如果您在該名稱空間中添加節點,則會添加它們。它們不需要位於根元素上,而是位於使用它的元素或其祖先之一上。

$request = ' 
    <ns1:someNodeName 
     xmlns:ns1="http://some.webservice.url.com"     
     xmlns:ns2="http://some.other.webservice.url.com"> 
     <ns2:someOtherNodeName> 
      // [REQUEST DETAILS] 
     </ns2:someOtherNodeName> 
    </ns1:someNodeName> 
'; 

如果您動態創建XML,則應使用XML擴展名,如DOM或XMLWriter。他們有特定的方法來創建具有名稱空間的元素,並自動添加定義。

$xmlns = [ 
    'ns1' => "http://some.webservice.url.com",     
    'ns2' => "http://some.other.webservice.url.com" 
]; 

$document = new DOMDocument(); 
$outer = $document->appendChild(
    $document->createElementNS($xmlns['ns1'], 'ns1:someNodeName') 
); 
$inner = $outer->appendChild(
    $document->createElementNS($xmlns['ns2'], 'ns2:someOtherNodeName') 
); 
$inner->appendChild(
    $document->createComment('[REQUEST DETAILS]') 
); 

$document->formatOutput = TRUE; 
echo $document->saveXml($outer); 

輸出:

<ns1:someNodeName xmlns:ns1="http://some.webservice.url.com"> 
    <ns2:someOtherNodeName xmlns:ns2="http://some.other.webservice.url.com"> 
    <!--[REQUEST DETAILS]--> 
    </ns2:someOtherNodeName> 
</ns1:someNodeName> 
+0

謝謝了很多。像魅力一樣工作! –

相關問題