如何在PHP中为SoapClient添加URN?

3

我有一个由SoapUI从这个wsdl生成的xml文件:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:safetypay:mws:api:messages">
   <soapenv:Header/>
   <soapenv:Body>
      <urn:CommunicationTestRequest>
         <urn:ApiKey>?</urn:ApiKey>
         <urn:RequestDateTime>?</urn:RequestDateTime>
         <urn:Signature>?</urn:Signature>
      </urn:CommunicationTestRequest>
   </soapenv:Body>
</soapenv:Envelope>

它可以正常工作,但是当我使用PHP客户端SoapClient创建时,它失败了...我发现错误是因为稍微有些不同,只是缺少了urn(我不知道它是什么)。来自SoapClient的XML如下:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
        <CommunicationTestRequest>
            <RequestDateTime>2012-04-17T23:21:54</RequestDateTime>
            <ApiKey>XXXXX</ApiKey>
            <Signature>XXXXX</Signature>
        </CommunicationTestRequest>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

三个主要的区别如下: 1)在定义中,SoapClient 生成的代码缺少以下内容: xmlns:urn="urn:safetypay:mws:api:messages" 2)在 SoapUI 生成的 XML 中,我有 soapenv,而在 SoapClient 生成的 XML 中为 SOAP-ENV。 3)在 SoapUI 的 XML 中,每个子元素之前都有 urn,而 SoapClient 的没有。
我几乎整天都在更改配置并从对象向数组后退(因为过去它可以使用其他 WSDL),但是我在互联网上的研究没有给我任何东西...所以...有人能想到我的错误吗?
请求如下:
$std = new RequestDateTime();
$std->RequestDateTime = date ( 'Y-m-d\TH:i:s', time () );
$std->ApiKey = SafetyPaySoap::API_KEY_SAND;
$std->Signature = SafetyPaySoap::SIG_KEY_SAND;
$this->_wsdl->CommunicationTest($std);

这里是SoapClient的配置

$config = array ();
$config ['exceptions'] = true;
$config ['trace'] = true;
$config ['cache_wsdl'] = WSDL_CACHE_NONE;
$config ['soap_version'] = SOAP_1_1;
parent::SoapClient ( $url . '?wsdl', $config );

编辑: 我已经成功解决了这个错误。我不得不重写__doRequest方法。现在它看起来像这样:

public function __doRequest($request, $location, $action, $version, $one_way = null) {
    $request = str_ireplace("<{$this->_method}Request>", "<{$this->_method}Request xmlns='urn:safetypay:mws:api:messages'>", $request);
    return parent::__doRequest ( $request, $location, $action, $version, $one_way );
}

所以,我所做的是在请求中添加命名空间,并生成以下XML:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
        <CommunicationTestRequest xmlns='urn:safetypay:mws:api:messages'>
            <ApiKey>XXXX</ApiKey>
            <RequestDateTime>2012-04-18T20:22:51</RequestDateTime>
            <Signature>XXXXX</Signature>
        </CommunicationTestRequest>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
1个回答

1

urn: 实际上是一个 XML 命名空间。查看 SoapClient 的文档,它似乎将目标命名空间作为配置数组中的 'uri' 条目传递。尝试使用以下内容,看看是否有所不同。

$config = array();
$config['uri'] = "urn:safetypay:mws:api:messages";
// your other configurations
parent::SoapClient('https://mws2.safetypay.com/sandbox/express/ws/v.2.4/MerchantWS.asmx?wsdl', $config);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接