将 XML 字符串(非数组)从 PHP SOAP 或 nusoap 发送到 .NET 服务
我是 SOAP 新手,在将一些输出发送到 .NET 服务器时遇到问题(是的,我进行了广泛搜索,但我似乎无法满足我非常简单的要求 - 发送单个 XML 字符串)匹配这个:
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>';
?>
显然,最终脚本中的所有主机和用户名都是正确的)。我可以连接到 WSDL,阅读它,只有一种方法我感兴趣(SubmitCalls),它只有一个部分,在 WSDL 模式中名为“参数”。上面抛出了 400 Bad request 错误 - 你知道我哪里出错了吗?
我尝试过使用 PHP SOAP,但似乎无法将 XML 字符串作为 SOAP 请求的正文发送。我在三天的时间里一直在摆弄这个问题,并阅读了无数的网页,但我仍然无法做到正确。请帮忙......如果你能告诉我如何使用任一库来做到这一点,我将非常感激......
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:
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:
<?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?
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....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 $client->send() 方法发送纯 XML。
您可以在此处查看示例:
http:// itworkarounds.blogspot.com/2011/07/send-raw-xml-with-php-nusoap.html
如果这不起作用,您可以尝试使用 CURL 发布 XML。
You can send plain XML with the $client->send() method.
You can see the example here:
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.
-试试这个-
然后(nusoap)
-try this-
and then (nusoap)
不完全是问题的答案 - 但现在已经解决了。服务提供商创建了一种新方法,该方法在所有方面都相同,只是它允许使用 XML 文档而不是字符串。通过对 $bodyxml 变量的内容进行一些细微的更改,然后发送到这个新方法,它似乎工作正常。
顺便说一下,任何想要调试 SOAP 应用程序的人都应该考虑从 Sourceforge 获取 SOAP UI。这确实帮助我理智地检查了我的问题,并为修复提供了一些有用的指导。
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.
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.
您始终可以将 xml 作为字符串发送并修改库。不推荐,但在某些情况下这更容易。
不要忘记
Header("SoapAction: ...")
You can always just send the xml as a string and screw the libraries. Not recommended but in some cases that is easier.
don't forget the
Header("SoapAction: ...")