簡體   English   中英

php SoapClient override __dorequest does not change xml after str_replace

[英]php SoapClient override __dorequest does not change xml after str_replace

我覆蓋 php SoapClient class 以便向元素添加屬性。 當我打印更改后的代碼時,就在發送之前,XML 似乎已按預期更改,但在通過 __getLastRequest() 查看 XML 時,XML 似乎根本沒有更改。 在代碼中,我嘗試向元素添加一個屬性。 也許有人知道為什么 $request_new XML 字符串沒有發送到服務器? (OutputXML() 只是一些很好的格式化與 colering 到 html)

class SoapClientDebug extends SoapClient
{
    public function __doRequest($request, $location, $action, $version, $one_way = 0) {
        // change / add whatever needed
        $request_new = str_replace( '<env:Body', '<env:Body ns1:Id="TheBody"', $request );
        // debug out
        OutputXML($request_new);
        // calling parent
        return parent::__doRequest($request_new, $location, $action, $version, $one_way);
    }
}

在調用父 __doRequest 之前檢查輸出的 XML

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://logius.nl/digipoort/koppelvlakservices/1.2/" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" xmlns:ns3="http://schemas.xmlsoap.org/soap/security/2000-12">
 <env:Header>
  <ns3:Security env:mustUnderstand="true">
   <ns2:Signature>
    <ns2:SignedInfo>
     <ns2:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
    <ns2:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
   <ns2:Reference>
    <ns2:Transforms>
     <ns2:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
    </ns2:Transforms>
   <ns2:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
  <ns2:DigestValue>$6hpmccmjxQmAI143OhQfIWpkryw=</ns2:DigestValue>
   </ns2:Reference>
  </ns2:SignedInfo>
 <ns2:SignatureValue>sv8n4h0rV4Xmbl+M+w+MLl7lVA8KFsoWRx5DqSKkwSie32jOFoJt0WvH6UWRQI</ns2:SignatureValue>
  <ns2:KeyInfo>
   <ns2:X509Data>
    <ns2:X509IssuerName>CN=TestSignCert</ns2:X509IssuerName>
     <ns2:X509SerialNumber>75496503122422458150193540449068096025</ns2:X509SerialNumber>
      </ns2:X509Data>
     </ns2:KeyInfo>
    </ns2:Signature>
   </ns3:Security>
  </env:Header>
 <env:Body ns1:Id="TheBody">
  <ns1:aanleverRequest>
   <ns1:berichtsoort>Omzetbelasting</ns1:berichtsoort>
    <ns1:aanleverkenmerk>Happyflow</ns1:aanleverkenmerk>
     <ns1:identiteitBelanghebbende>
      <ns1:nummer/>
     <ns1:type/>
    </ns1:identiteitBelanghebbende>
   <ns1:rolBelanghebbende>Intermediair</ns1:rolBelanghebbende>
    <ns1:berichtInhoud>
     <ns1:mimeType/>
    <ns1:bestandsnaam/>
   <ns1:inhoud/>
  </ns1:berichtInhoud>
 <ns1:autorisatieAdres>https://secure.inepd.nl/inloon</ns1:autorisatieAdres>
  </ns1:aanleverRequest>
 </env:Body>
</env:Envelope>

通過 __getLastRequest 查看 XML

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://logius.nl/digipoort/koppelvlakservices/1.2/" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" xmlns:ns3="http://schemas.xmlsoap.org/soap/security/2000-12">
 <env:Header>
  <ns3:Security env:mustUnderstand="true">
   <ns2:Signature>
    <ns2:SignedInfo>
     <ns2:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
    <ns2:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
   <ns2:Reference>
    <ns2:Transforms>
     <ns2:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
    </ns2:Transforms>
   <ns2:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
  <ns2:DigestValue>$6hpmccmjxQmAI143OhQfIWpkryw=</ns2:DigestValue>
   </ns2:Reference>
  </ns2:SignedInfo>
 <ns2:SignatureValue>sv8n4h0rV4Xmbl+M+w+MLl7lVA8KFsoWRx5DqSKkwSie32jOFoJt0WvH6UWRQI</ns2:SignatureValue>
  <ns2:KeyInfo>
   <ns2:X509Data>
    <ns2:X509IssuerName>CN=TestSignCert</ns2:X509IssuerName>
     <ns2:X509SerialNumber>75496503122422458150193540449068096025</ns2:X509SerialNumber>
      </ns2:X509Data>
     </ns2:KeyInfo>
    </ns2:Signature>
   </ns3:Security>
  </env:Header>
 <env:Body>
  <ns1:aanleverRequest>
   <ns1:berichtsoort>Omzetbelasting</ns1:berichtsoort>
    <ns1:aanleverkenmerk>Happyflow</ns1:aanleverkenmerk>
     <ns1:identiteitBelanghebbende>
      <ns1:nummer/>
     <ns1:type/>
    </ns1:identiteitBelanghebbende>
   <ns1:rolBelanghebbende>Intermediair</ns1:rolBelanghebbende>
    <ns1:berichtInhoud>
     <ns1:mimeType/>
    <ns1:bestandsnaam/>
   <ns1:inhoud/>
  </ns1:berichtInhoud>
 <ns1:autorisatieAdres>https://secure.inepd.nl/inloon</ns1:autorisatieAdres>
  </ns1:aanleverRequest>
 </env:Body>
</env:Envelope>

我想通了為什么,回答我自己的問題可能有點奇怪,但也許它可以幫助將來的人; 事實證明 __getLastRequest() 在這種情況下並不是實際的最后一個請求。 所以覆蓋 __doRequest 是一個選項,但在這種情況下不要使用 __getLastRequest!

正如您發現的那樣, __getLastRequest()返回發送到__do_Request()的請求,而不是通過網絡發出的請求...

你有兩個選擇:

a) 覆蓋 __getLastRequest(除了 __doRequest)

b) 在您的 __doRequest 方法中添加幾行以更新 SoapClient 的私有__last_request屬性

    if (PHP_VERSION_ID >= 80100) {
        $lastRequestRef = new \ReflectionProperty('SoapClient', '__last_request');
        $lastRequestRef->setAccessible(true);
        $lastRequestRef->setValue($this, $modifiedRequest);
    } else {
        $this->__last_request = $modifiedRequest;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM