如何在PHP中设置mustUnderstand属性soap? [英] How to set mustUnderstand attribute soap in php?

查看:71
本文介绍了如何在PHP中设置mustUnderstand属性soap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将 mustUnderstand 属性设置为我的xml标头,但无法对其进行排序,这是我的代码段,

i've tried to set mustUnderstand attribute to my xml header but not able to sort it out , here is my code snippet,

$actionurl = array('Action'=>'www.example.com');
$headers = new SoapHeader('NAMESPACE','Action',$actionurl,true);
$client->__setSoapHeaders($headers);
$d = $client->__soapCall('MethodName',array($params));

我的请求xml,该属性要与设置了属性之后要匹配的

And my request xml ,which to be matched and which i want after setting up atribute ,

<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">www.example.com</Action>

所以我对soapheader方法中的这个命名空间"第一个参数感到困惑.它是什么,为什么我们使用相同的?指的是此链接,它根本没有用.任何人都可以帮我出来以获得确切的xml请求格式?

so i'm confused about this 'namespace' first param in soapheader method.what it it and why do we use just same? Was referring this link and it didn't work at all.Can anyone help me out to get that exact xml request format?

推荐答案

首先,如果您想将自己创建的代码与以示例为目标的XML进行比较,则需要寻找一些东西将在XML中创建 SoapClient .

First of all if you want to compare what you create with your code against the XML you have as an example to target for, you need to have something to look into the XML the SoapClient will create.

您可以通过模拟 SoapClient :

/**
 * Class MockSoapClient
 */
class MockSoapClient extends SoapClient
{
    /**
     * @var string|null
     */
    private $lastRequest;

    public function __doRequest($request, $location, $action, $version, $one_way = 0)
    {
        $config = ['input-xml' => 1, 'indent' => 1, 'wrap' => 0, 'indent-attributes' => 1];

        $this->lastRequest = tidy_repair_string($request, $config);

        return "";
    }

    /**
     * @return string|null
     */
    public function getLastRequest()
    {
        return $this->lastRequest;
    }
}

您可以使用此新的 MockSoapClient 替换常规的 SoapClient ,该功能可以显示请求,该请求将创建 SoapClient .

You can replace the normal SoapClient with this new MockSoapClient which is able to show the request which SoapClient would have been created.

让我们伪造一个像您这样的请求:

Let's fake a request like yours:

$client = new MockSoapClient(null, array('location' => "http://localhost/soap.php",
                                         'uri'      => "http://test-uri/"));

$actionurl = array('Action' => 'www.example.com');

$header = new SoapHeader('NAMESPACE', 'Action', $actionurl, true);

$client->__soapCall("echoVoid", array(), null, $header);

并查看请求的外观:

echo $client->getLastRequest();

在这里输出:

<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:ns1="http://test-uri/"
                   xmlns:ns2="NAMESPACE"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                   xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
                   SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Header>
    <ns2:Action SOAP-ENV:mustUnderstand="1">
      <item>
        <key>Action</key>
        <value>www.example.com</value>
      </item>
    </ns2:Action>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <ns1:echoVoid />
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

这里有趣的是肥皂头:

<SOAP-ENV:Header>
  <ns2:Action SOAP-ENV:mustUnderstand="1">
    <item>
      <key>Action</key>
      <value>www.example.com</value>
    </item>
  </ns2:Action>
</SOAP-ENV:Header>

此处"ns2:"是"NAMESPACE"名称空间的前缀,这是错误的名称空间.在您的示例中:

Here "ns2:" is the prefix of the "NAMESPACE" namespace which is the wrong one. In your example:

<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">www.example.com</Action>

<action>元素的名称空间为"http://schemas.microsoft.com/ws/2005/05/addressing/none".因此必须将"NAMESPACE"替换为"http://schemas.microsoft.com/ws/2005/05/addressing/none".

The namepspace of the <action> element is "http://schemas.microsoft.com/ws/2005/05/addressing/none". So "NAMESPACE" has to be replaced with "http://schemas.microsoft.com/ws/2005/05/addressing/none".

mustUnderstand属性位于正确的名称空间中,所以很好.这并不奇怪,因为它是标准的Soap命名空间之一,因此它们是预定义的,因此您在这里无需多加注意.只有它们的前缀不同,但是只要不同的前缀引用相同的名称空间,就没有区别.

The mustUnderstand attribute is in the correct namespace, so this is fine. That's not wondering, as it's one of the standard Soap namespaces, so those are predefined and you don't need to take much care here. Only their prefixes differ, but as long as the different prefixes refer to the same namespace, there is no difference.

错误的第三点是<action>元素的内容,您只需要在其中输入一个字符串,而不是<item>-<key>/<value>构造,这是由于您拥有的数组所致.让我们再试一次:

The third point which is wrong is the content of the <action> element, you only need a string in there, not the <item>-<key>/<value> construct which is because of the array you have. Let's try this again:

$namespace = 'http://schemas.microsoft.com/ws/2005/05/addressing/none';
$actionurl = 'www.example.com';

$header = new SoapHeader($namespace, 'Action', $actionurl, true);

现在输出看起来非常正确:

And now the output looks very much correct:

<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:ns1="http://test-uri/"
                   xmlns:ns2="http://schemas.microsoft.com/ws/2005/05/addressing/none"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                   xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
                   SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <SOAP-ENV:Header>
    <ns2:Action SOAP-ENV:mustUnderstand="1">www.example.com</ns2:Action>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <ns1:echoVoid />
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我希望这个答案可以帮助您在此处了解 XML命名空间的一些基础知识这样您就可以通过检查请求XML来更轻松地调试在此处创建的soap-request.

I hope this answer helps you to understand some of the basics of XML namespaces here as well as how you can more easily debug the soap-request you create here by inspecting the requests XML.

这篇关于如何在PHP中设置mustUnderstand属性soap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆