簡體   English   中英

從PHP SOAP或nusoap向.NET服務發送XML字符串(不是數組)

[英]Sending XML string (not array) from PHP SOAP or nusoap to .NET service

我是SOAP的新手,遇到了一些問題(是的,我已經進行了廣泛的搜索,但是我似乎無法滿足我非常簡單的要求-發送單個XML字符串),並將一些輸出發送到.NET服務器。符合以下條件:

    POST /someurl.asmx HTTP/1.1
Host: www.somehost.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://somehost.com/SubmitCalls"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <SubmitCalls xmlns="http://somehost/">
      <request>string</request>
    </SubmitCalls>
  </soap:Body>
</soap:Envelope>

我的nusoap代碼如下所示:

<?php
require_once('../lib/nusoap.php');

$bodyxml = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<SubmitCalls xmlns="http://somehost/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<request>
<?xml version="1.0" encoding="UTF-8"?>
<bXML xmlns="http://somehost/Schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <From>
  <UserName>some username</UserName>
  <Password>some password</Password>
 </From>
 <Calls>
  <Call>
   <Reference>11111</Reference>
   <Name>Joe Bloggs</Name>
   <Tel1>02075574200</Tel1>
   <Tel2>02075574201</Tel2>
   <Tel3>02075574202</Tel3>
   <Tel4>02075574203</Tel4>
   <Tel5>02075574204</Tel5>
   <CLI>08448220640</CLI>
   <CallTime>09:00</CallTime>
   <FileName>02075574200_1</FileName>
  </Call>
 <Call>
   <Reference>11111</Reference>
   <Name>Joe Bloggs</Name>
   <Tel1>02075574200</Tel1>
   <Tel2>02075574206</Tel2>
   <Tel3>02075574207</Tel3>
   <Tel4>02075574208</Tel4>
   <Tel5>02075574209</Tel5>
   <CLI>08448220640</CLI>
   <CallTime>09:00</CallTime>
   <FileName>02075574200_2</FileName>
  </Call>
 </Calls>
</bXML>
</request>
</SubmitCalls>
</soap:Body>
</soap:Envelope>
';


$client = new nusoap_client("somehost?WSDL",true);
$err = $client->getError();
if ($err) {
 echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
 exit();
}

$client->soap_defencoding = 'utf-8';
$client->useHTTPPersistentConnection();
$client->setUseCurl($useCURL);
$bsoapaction = "http://somehost/SubmitCalls";
$result = $client->send($bodyxml, $bsoapaction);
// Check for a fault
if ($client->fault) {
 echo '<h2>Fault</h2><pre>';
 print_r($result);
 echo '</pre>';
} else {
 // Check for errors
 $err = $client->getError();
 if ($err) {
  // Display the error
  echo '<h2>Error</h2><pre>' . $err . '</pre>';
 } else {
  // Display the result
  echo '<h2>Result</h2><pre>';
  print_r($result);
  echo '</pre>';
 }
}
echo '<h2>Request</h2><pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2><pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
echo '<h2>Client Debug</h2><pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
echo '<h2>Proxy Debug</h2><pre>' . htmlspecialchars($proxy->debug_str, ENT_QUOTES) . '</pre>';
?>

(很可能在最終腳本中所有somehost和用戶名都是正確的)。 我可以連接到WSDL,進行閱讀,只有一種我感興趣的方法(SubmitCalls),它只有一部分,在WSDL模式中名為“參數”。 上面引發了400錯誤的請求錯誤-有什么想法我要出錯嗎?

我嘗試使用PHP SOAP代替,但是我似乎根本無法發送XML字符串作為SOAP請求的主體。 我一直在三天的時間里一直在擺弄這個,並且閱讀了無數的網頁,但是我仍然做得不好。 請幫助...。如果您可以向我展示如何使用任一庫來執行此操作,我將不勝感激。

您可以使用$ client-> send()方法發送純XML。

$raw_xml = "<Your_XML>...</Your_XML>";
$msg = $client->serializeEnvelope("$raw_xml");
$result=$client->send($msg, $endpoint);

您可以在此處查看示例:

http://itworkarounds.blogspot.com/2011/07/send-raw-xml-with-php-nusoap.html

如果這樣不起作用,您可以嘗試使用CURL發布XML。

-嘗試這個-

$xml = simplexml_load_string('<data>x</data>')

然后(nusoap)

$result = $client->call('host', array('parameter' =>$xml)

不完全是問題的答案-但現在已解決。 服務提供者創建了一個新方法,該方法在所有方面都是相同的,只是它允許使用XML文檔而不是字符串。 通過對$ bodyxml變量的內容進行一些細微改動,並發送到此新方法,它似乎可以正常工作。

順便說一句-任何想要調試SOAP應用程序的人都應該真正地從Sourceforge那里獲取SOAP UI。 這確實幫助我理智地檢查了我的問題,並為修復提供了一些有用的指示。

您總是可以只將XML作為字符串發送並擰緊庫。 不推薦使用,但在某些情況下更容易。

不要忘記Header("SoapAction: ...")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM