如何将 nuSOAP 用于具有多个命名空间的消息

发布于 2024-07-16 08:18:18 字数 1181 浏览 9 评论 0原文

我正在尝试使用 nuSOAP 访问 WebService(因为我在这里绑定到 PHP4),该服务在消息中使用超过 1 个命名空间。 那可能吗?

示例请求消息如下所示:

<soapenv:Envelope ...
  xmlns:ns1="http://domain.tld/namespace1"
  xmlns:ns2="http://domain.tld/namespace2">
  <soapenv:Header/>
  <soapenv:Body>
    <ns1:myOperation>
      <ns2:Person>
        <ns2:Firstname>..</ns2:Firstname>
        ..
      </ns2:Person>
      <ns1:Attribute>..</ns1:Attribute>
    </ns1:myOperation>
  </soapenv:Body>
</soapenv:Envelope>

我尝试遵循:

$client = new nusoap_client("my.wsdl", true);
$params = array(
  'Person' => array(
    'FirstName'  => 'Thomas',
    ..
   ),
   'Attribute' => 'foo'
 );

 $result = $client->call('myOperation', $params, '', 'soapAction');

希望 nuSOAP 尝试将这些名称与正确的名称空间和节点相匹配。 然后我尝试使用soapval() 生成元素及其名称空间 - 但如果我调用一个操作,nuSOAP 会创建以下请求:

<SOAP-ENV:Envelope ...>
  <SOAP-ENV:Body>
    <queryCCApplicationDataRequest xmlns="http://domain.tld/namespace1"/>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

因此,在“匹配”阶段会出现问题。

I'm trying to access a WebService using nuSOAP (because I'm bound to PHP4 here) that uses more than 1 namespace in a message. Is that possible?

An example request message would look like this:

<soapenv:Envelope ...
  xmlns:ns1="http://domain.tld/namespace1"
  xmlns:ns2="http://domain.tld/namespace2">
  <soapenv:Header/>
  <soapenv:Body>
    <ns1:myOperation>
      <ns2:Person>
        <ns2:Firstname>..</ns2:Firstname>
        ..
      </ns2:Person>
      <ns1:Attribute>..</ns1:Attribute>
    </ns1:myOperation>
  </soapenv:Body>
</soapenv:Envelope>

I tried to following:

$client = new nusoap_client("my.wsdl", true);
$params = array(
  'Person' => array(
    'FirstName'  => 'Thomas',
    ..
   ),
   'Attribute' => 'foo'
 );

 $result = $client->call('myOperation', $params, '', 'soapAction');

in the hope that nuSOAP would try to match these names to the correct namespaces and nodes. Then I tried to use soapval() to generate the elements and their namespace - but if I call an operation, nuSOAP creates the following request:

<SOAP-ENV:Envelope ...>
  <SOAP-ENV:Body>
    <queryCCApplicationDataRequest xmlns="http://domain.tld/namespace1"/>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

So something goes wrong during the "matching" phase.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

俏︾媚 2024-07-23 08:18:18

尝试匹配后,我发现了两种可能的解决方案:

1)不要使用 WSDL 创建 nusoap_client 和soapval() 创建消息
这样做的缺点是消息包含大量开销(命名空间在每个元素中定义)。 不太好。

2)不要依赖参数的匹配,而是在 xml 中构建回复并将所有前缀定义放在第一个元素中 - 例如

$params = "<ns1:myOperation xmlns:ns1="..." xmlns:ns2="...">
      <ns2:Person>
        <ns2:Firstname>..</ns2:Firstname>
        ..
      </ns2:Person>
      <ns1:Attribute>..</ns1:Attribute>
    </ns1:myOperation>";

仍然不是一个非常好的解决方案,但它有效:-)

After trying around with the matching, I found two possible solutions:

1) Don't use the WSDL to create the nusoap_client and soapval() to create the message
This has the disadvantage that the message contains a lot of overhead (the namespace is defined in each element). Not so fine.

2) Instead of relying on the matching of parameters, construct your reply in xml and put all the definition for prefixes in the first element - e.g.

$params = "<ns1:myOperation xmlns:ns1="..." xmlns:ns2="...">
      <ns2:Person>
        <ns2:Firstname>..</ns2:Firstname>
        ..
      </ns2:Person>
      <ns1:Attribute>..</ns1:Attribute>
    </ns1:myOperation>";

Still not a very nice solution, but it works :-)

各自安好 2024-07-23 08:18:18

在 Irwin 的帖子的基础上,我手动创建了 xml,并让 nusoap 完成其余的工作。 我的网络主机没有 php Soap 扩展,所以我必须使用 nusoap,并且我尝试使用的 Web 服务需要每个标签上的命名空间(例如,我的示例中的用户名和密码)。

require_once('lib/nusoap.php');

$client = new nusoap_client('https://service.somesite.com/ClientService.asmx');
$client->soap_defencoding = 'utf-8';
$client->useHTTPPersistentConnection(); // Uses http 1.1 instead of 1.0
$soapaction = "https://service.somesite.com/GetFoods";

$request_xml = '<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
  <env:Body>
    <n1:GetFoods xmlns:n1="https://service.somesite.com">
      <n1:username>banjer</n1:username>
      <n1:password>theleftorium</n1:password>
    </n1:GetFoods>
  </env:Body>
</env:Envelope>
';

$response = $client->send($request_xml, $soapaction, ''); 

echo '<h2>Request</h2><pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2><pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->getDebug(), ENT_QUOTES) . '</pre>';

然后我遇到了一个错误:

Notice: Undefined property: nusoap_client::$operation in ./lib/nusoap.php  on line 7674

所以我走了懒惰的路线,进入 nusoap.php 并在第 7674 行之前添加了这段代码以使其满意:

    if(empty($this->operation)) {
        $this->operation = "";
    }

Building on Irwin's post, I created the xml manually and had nusoap do the rest. My webhost does not have the php soap extension, so I had to go with nusoap, and the web service I'm trying to consume required the namespaces on each tag (e.g. on username and password in my example here).

require_once('lib/nusoap.php');

$client = new nusoap_client('https://service.somesite.com/ClientService.asmx');
$client->soap_defencoding = 'utf-8';
$client->useHTTPPersistentConnection(); // Uses http 1.1 instead of 1.0
$soapaction = "https://service.somesite.com/GetFoods";

$request_xml = '<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
  <env:Body>
    <n1:GetFoods xmlns:n1="https://service.somesite.com">
      <n1:username>banjer</n1:username>
      <n1:password>theleftorium</n1:password>
    </n1:GetFoods>
  </env:Body>
</env:Envelope>
';

$response = $client->send($request_xml, $soapaction, ''); 

echo '<h2>Request</h2><pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2><pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->getDebug(), ENT_QUOTES) . '</pre>';

Then I had an error that said:

Notice: Undefined property: nusoap_client::$operation in ./lib/nusoap.php  on line 7674

So I went the lazy route and went into nusoap.php and added this code before line 7674 to make it happy:

    if(empty($this->operation)) {
        $this->operation = "";
    }
夜雨飘雪 2024-07-23 08:18:18

另一个绕过此问题的方法是修改 nusoap_client::call() 函数。 在 nusoap.php 中的这一行(版本 1.123 中为 7359)旁边:

$nsPrefix = $this->wsdl->getPrefixFromNamespace($namespace);

我添加了这一行:

$nsPrefix = $this->wsdl->getPrefixFromNamespace("other_ns_name");

它起作用了! 由于我只需要为一个项目使用这个库,因此我可以对这个 hack 进行硬编码。 否则,我会深入挖掘并修改函数以接受数组而不是字符串作为命名空间参数。

Another bypass this issue would be a modification to nusoap_client::call() function. Next to the this line (7359 in version 1.123) in nusoap.php:

$nsPrefix = $this->wsdl->getPrefixFromNamespace($namespace);

I added this one:

$nsPrefix = $this->wsdl->getPrefixFromNamespace("other_ns_name");

And it worked! Since I only needed this library for one project, it was OK for me to hardcode this hack. Otherwise, I would dig more and modify the function to accept array instead of string for a namespace parameter.

心清如水 2024-07-23 08:18:18

是的,我也遇到了同样的问题(通过谷歌找到了你的q!),我遇到了这个:
http://www.heidisoft.com/博客/using-nusoap-consume-net-web-service-10-min
这里,开发人员在 coe 中创建消息的 xml 正文,然后使用 nusoap 提交。

Yeah, i've been having this same problem (found your q via google!) and i've come across this:
http://www.heidisoft.com/blog/using-nusoap-consume-net-web-service-10-min
Here, the dev creates the xml body of the message in coe and then uses nusoap to submit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文