简体   繁体   English

从PHP SOAP或nusoap向.NET服务发送XML字符串(不是数组)

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

I'm new to SOAP and I'm having issues (yes, I have searched - extensively, but I can't seem to match my very simple requirement - sending a single XML string) with sending some output to a .NET server to match this: 我是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>

My nusoap code looks like this: 我的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>';
?>

(Obiously, all the somehost and usernames are correct in the final script). (很可能在最终脚本中所有somehost和用户名都是正确的)。 I can connect to the WSDL, read it, there's only one method I'm interested in (SubmitCalls) which only has one part, named 'parameters' in the WSDL schema. 我可以连接到WSDL,进行阅读,只有一种我感兴趣的方法(SubmitCalls),它只有一部分,在WSDL模式中名为“参数”。 The above throws a 400 Bad request error - any ideas where I'm going wrong? 上面引发了400错误的请求错误-有什么想法我要出错吗?

I've tried using PHP SOAP instead, but I simply can't seem to send an XML string as the body of the SOAP request. 我尝试使用PHP SOAP代替,但是我似乎根本无法发送XML字符串作为SOAP请求的主体。 I've been fiddling with this for the best part of three days and read a zillion web pages, but I still can't get it right. 我一直在三天的时间里一直在摆弄这个,并且阅读了无数的网页,但是我仍然做得不好。 Please help.... if you could show me how to do this using either library I would be enormously grateful.... 请帮助...。如果您可以向我展示如何使用任一库来执行此操作,我将不胜感激。

You can send plain XML with the $client->send() method. 您可以使用$ client-> send()方法发送纯XML。

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

You can see the example here: 您可以在此处查看示例:

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

If that does't work you can try posting the XML with CURL. 如果这样不起作用,您可以尝试使用CURL发布XML。

-try this- -尝试这个-

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

and then (nusoap) 然后(nusoap)

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

Not exactly an answer to the issue - but it is now solved. 不完全是问题的答案-但现在已解决。 The service provider created a new method, which was identical in all respects except that it allowed for an XML document rather than a string. 服务提供者创建了一个新方法,该方法在所有方面都是相同的,只是它允许使用XML文档而不是字符串。 By making some minor alterations to the contents of the $bodyxml variable, and sending to this new method, it appears to work fine. 通过对$ bodyxml变量的内容进行一些细微改动,并发送到此新方法,它似乎可以正常工作。

By the way - anyone looking to debug SOAP applications should really look at grabbing SOAP UI off Sourceforge. 顺便说一句-任何想要调试SOAP应用程序的人都应该真正地从Sourceforge那里获取SOAP UI。 This really helped me sanity check my issue, and provided some useful pointers for the fix. 这确实帮助我理智地检查了我的问题,并为修复提供了一些有用的指示。

You can always just send the xml as a string and screw the libraries. 您总是可以只将XML作为字符串发送并拧紧库。 Not recommended but in some cases that is easier. 不推荐使用,但在某些情况下更容易。

don't forget the Header("SoapAction: ...") 不要忘记Header("SoapAction: ...")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM