如何在PHP上添加urn到SoapClient?

时间:2012-04-17 23:32:43

标签: php web-services soap namespaces wsdl

我有来自this wsdl的SoapUI生成的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时,它失败了...我发现错误因为它略有不同,它只是错过了瓮(我不知道这是什么)。 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:瓮= “瓮:safetypay:MWS:API:消息” 2)在SoapUI生成的xml中我有soapenv,而在SoapClient生成的那个中它是SOAP-ENV 3)在SoapUI中,我在每个孩子面前都有骨灰盒,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 个答案:

答案 0 :(得分:0)

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);