首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用PHP和XML主体创建SOAP调用

使用PHP和XML主体创建SOAP调用
EN

Stack Overflow用户
提问于 2013-03-09 02:30:09
回答 2查看 105.9K关注 0票数 23

我正在尝试使用PHP调用SOAP方法。

下面是我得到的代码:

代码语言:javascript
复制
$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);

下面是它创建的请求:

代码语言:javascript
复制
  <?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>

下面是我希望请求看起来是这样的:

代码语言:javascript
复制
  <?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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-12 22:15:01

有几种方法可以解决这个问题。最少的麻烦和几乎你想要的:

代码语言:javascript
复制
$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);

这将为您提供以下XML:

代码语言:javascript
复制
<?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>

这几乎就是您想要的,除了方法名上的命名空间。我不知道这是不是一个问题。如果是这样的话,你可以进一步修改它。您可以手动将<Echo>标记放在XML字符串中,并通过将'style' => SOAP_DOCUMENT,添加到选项数组来让SoapClient不设置该方法,如下所示:

代码语言:javascript
复制
$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);

这将产生以下请求XML:

代码语言:javascript
复制
<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>

最后,如果你想尝试SoapVar和SoapParam对象,你可以在PHP手册的注释中找到一个很好的参考:http://www.php.net/manual/en/soapvar.soapvar.php#104065。如果你成功了,请让我知道,我失败得很痛苦。

票数 51
EN

Stack Overflow用户

发布于 2013-03-14 12:56:03

首先,您必须指定您希望使用文档文字样式:

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

然后,您需要将数据转换为SoapVar;我已经编写了一个简单的转换函数:

代码语言:javascript
复制
function soapify(array $data)
{
        foreach ($data as &$value) {
                if (is_array($value)) {
                        $value = soapify($value);
                }
        }

        return new SoapVar($data, SOAP_ENC_OBJECT);
}

然后,将此转换函数应用于数据:

代码语言:javascript
复制
$data = soapify(array(
    'Acquirer' => array(
        'Id' => 'MyId',
        'UserId' => 'MyUserId',
        'Password' => 'MyPassword',
    ),
));

最后,调用传递Data参数的服务:

代码语言:javascript
复制
$method = 'Echo';

$result = $client->$method(new SoapParam($data, 'Data'));
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15300843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档