将 XML 字符串(不是数组)从 PHP SOAP 或 nusoap 发送到 .NET 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4229239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending XML string (not array) from PHP SOAP or nusoap to .NET service
提问by odeclas
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). 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. The above throws a 400 Bad request error - any ideas where I'm going wrong?
(显然,最终脚本中的所有 somehost 和用户名都是正确的)。我可以连接到 WSDL,阅读它,我只对一种方法 (SubmitCalls) 感兴趣,它只有一个部分,在 WSDL 模式中名为“参数”。上面会抛出一个 400 Bad request 错误 - 有什么想法我哪里出错了吗?
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. 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....
我已经尝试使用 PHP SOAP,但我似乎无法将 XML 字符串作为 SOAP 请求的正文发送。三天的大部分时间里,我一直在摆弄这个并阅读了无数的网页,但我仍然无法做到正确。请帮助......如果你能告诉我如何使用任何一个库来做到这一点,我将不胜感激......
回答by Martin Taleski
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。
回答by gus
-try this-
-尝试这个-
$xml = simplexml_load_string('<data>x</data>')
and then (nusoap)
然后(nusoap)
$result = $client->call('host', array('parameter' =>$xml)
回答by odeclas
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. By making some minor alterations to the contents of the $bodyxml variable, and sending to this new method, it appears to work fine.
不完全是这个问题的答案 - 但现在已经解决了。服务提供者创建了一个新方法,该方法在所有方面都相同,只是它允许使用 XML 文档而不是字符串。通过对 $bodyxml 变量的内容进行一些小的改动,并将其发送到这个新方法,它似乎工作正常。
By the way - anyone looking to debug SOAP applications should really look at grabbing SOAP UI off Sourceforge. This really helped me sanity check my issue, and provided some useful pointers for the fix.
顺便说一句 - 任何想要调试 SOAP 应用程序的人都应该考虑从 Sourceforge 中获取 SOAP UI。这确实帮助我理智地检查了我的问题,并为修复提供了一些有用的指示。
回答by Byron Whitlock
You can always just send the xml as a string and screw the libraries. Not recommended but in some cases that is easier.
您始终可以将 xml 作为字符串发送并拧紧库。不推荐,但在某些情况下更容易。
don't forget the Header("SoapAction: ...")
不要忘记 Header("SoapAction: ...")