简体   繁体   English

使用带有XML主体的PHP创建SOAP调用

[英]Creating a SOAP call using PHP with an XML body

I'm trying to call a SOAP method using PHP. 我正在尝试使用PHP调用SOAP方法。

Here's the code I've got: 这是我得到的代码:

$data = array('Acquirer' =>
  array(
    'Id' => 'MyId',
    'UserId' => 'MyUserId',
    'Password' => 'MyPassword'
  ));
$method = 'Echo';
$client = new SoapClient(NULL,
           array('location' => 'https://example.com/ExampleWebServiceDL/services/ExampleHandler', 
           'uri' => 'http://example.com/wsdl', 'trace' => 1));
$result = $client->$method($data);

Here's the request it creates: 这是它创建的请求:

  <?xml version="1.0" encoding="UTF-8"?>
  <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://example.com/wsdl" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SOAP-ENV:Body>
      <ns1:Echo>
        <param0 xsi:type="ns2:Map">
          <item>
            <key xsi:type="xsd:string">Acquirer</key>
            <value xsi:type="ns2:Map">
              <item>
                <key xsi:type="xsd:string">Id</key>
                <value xsi:type="xsd:string">mcp</value>
              </item>
              <item>
                <key xsi:type="xsd:string">UserId</key>
                <value xsi:type="xsd:string">tst001</value>
              </item>
              <item>
                <key xsi:type="xsd:string">Password</key>
                <value xsi:type="xsd:string">test</value>
              </item>
            </value>
          </item>
        </param0>
      </ns1:Echo>
    </SOAP-ENV:Body>
  </SOAP-ENV:Envelope>

And here's what I want the request to look like: 这就是我想要的样子:

  <?xml version="1.0" encoding="UTF-8"?>
  <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://example.com/wsdl" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SOAP-ENV:Body>
      <Echo>
        <Acquirer>
          <Id>MyId</Id>
          <UserId>MyUserId</UserId>
          <Password>MyPassword</Password>
        </Acquirer>
      </Echo>
    </SOAP-ENV:Body>
  </SOAP-ENV:Envelope>

There are a couple of ways to solve this. 有几种方法可以解决这个问题。 The least hackiest and almost what you want: 最不可能的,几乎是你想要的:

$client = new SoapClient(
    null,
    array(
        'location' => 'https://example.com/ExampleWebServiceDL/services/ExampleHandler',
        'uri' => 'http://example.com/wsdl',
        'trace' => 1,
        'use' => SOAP_LITERAL,
    )
);
$params = new \SoapVar("<Acquirer><Id>MyId</Id><UserId>MyUserId</UserId><Password>MyPassword</Password></Acquirer>", XSD_ANYXML);
$result = $client->Echo($params);

This gets you the following XML: 这将获得以下XML:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://example.com/wsdl">
    <SOAP-ENV:Body>
        <ns1:Echo>
            <Acquirer>
                <Id>MyId</Id>
                <UserId>MyUserId</UserId>
                <Password>MyPassword</Password>
            </Acquirer>
        </ns1:Echo>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

That is almost exactly what you want, except for the namespace on the method name. 除了方法名称上的命名空间之外,这几乎就是您想要的。 I don't know if this is a problem. 我不知道这是不是一个问题。 If so, you can hack it even further. 如果是这样,你可以进一步破解它。 You could put the <Echo> tag in the XML string by hand and have the SoapClient not set the method by adding 'style' => SOAP_DOCUMENT, to the options array like this: 您可以手动将<Echo>标记放在XML字符串中,并让SoapClient不通过将'style' => SOAP_DOCUMENT,添加到options数组来设置方法,如下所示:

$client = new SoapClient(
    null,
    array(
        'location' => 'https://example.com/ExampleWebServiceDL/services/ExampleHandler',
        'uri' => 'http://example.com/wsdl',
        'trace' => 1,
        'use' => SOAP_LITERAL,
        'style' => SOAP_DOCUMENT,
    )
);
$params = new \SoapVar("<Echo><Acquirer><Id>MyId</Id><UserId>MyUserId</UserId><Password>MyPassword</Password></Acquirer></Echo>", XSD_ANYXML);
$result = $client->MethodNameIsIgnored($params);

This results in the following request XML: 这导致以下请求XML:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
        <Echo>
            <Acquirer>
                <Id>MyId</Id>
                <UserId>MyUserId</UserId>
                <Password>MyPassword</Password>
            </Acquirer>
        </Echo>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Finally, if you want to play around with SoapVar and SoapParam objects, you can find a good reference in this comment in the PHP manual: http://www.php.net/manual/en/soapvar.soapvar.php#104065 . 最后,如果你想使用SoapVar和SoapParam对象,你可以在PHP手册的这篇评论中找到一个很好的参考: http//www.php.net/manual/en/soapvar.soapvar.php#104065 If you get that to work, please let me know, I failed miserably. 如果你这样做,请让我知道,我失败了。

First off, you have to specify you wish to use Document Literal style: 首先,您必须指定您希望使用Document Literal样式:

$client = new SoapClient(NULL, array(
    'location' => 'https://example.com/path/to/service',
    'uri' => 'http://example.com/wsdl',
    'trace' => 1,
    'use' => SOAP_LITERAL)
);

Then, you need to transform your data into a SoapVar; 然后,您需要将数据转换为SoapVar; I've written a simple transform function: 我写了一个简单的变换函数:

function soapify(array $data)
{
        foreach ($data as &$value) {
                if (is_array($value)) {
                        $value = soapify($value);
                }
        }

        return new SoapVar($data, SOAP_ENC_OBJECT);
}

Then, you apply this transform function onto your data: 然后,将此转换函数应用于您的数据:

$data = soapify(array(
    'Acquirer' => array(
        'Id' => 'MyId',
        'UserId' => 'MyUserId',
        'Password' => 'MyPassword',
    ),
));

Finally, you call the service passing the Data parameter: 最后,您调用传递Data参数的服务:

$method = 'Echo';

$result = $client->$method(new SoapParam($data, 'Data'));

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

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