【问题标题】:Remove whitespaces from SoapClient request in PHP从 PHP 中的 SoapClient 请求中删除空格
【发布时间】:2020-11-23 12:18:17
【问题描述】:

我正在通过 PHP 原生 SoapClient 类创建 SOAP 请求。当我使用SoapClient::__getLastRequest 转储请求时,我会得到:

<?xml version="1.0" encoding="UTF-8"?>\n
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:microsoft-dynamics-schemas/page/itemb2cws"><SOAP-ENV:Body><ns1:ReadMultiple><ns1:filter/><ns1:setSize>100</ns1:setSize></ns1:ReadMultiple></SOAP-ENV:Body></SOAP-ENV:Envelope>

有什么办法,如何删除 XML 元素之间的换行符或任何其他空格? 我们的客户正在使用一些旧版本的 ERP Microsoft Dynamics Nav,当请求中出现空白时,服务器会崩溃。

【问题讨论】:

  • 您应该添加分辨率作为答案,以便将来的访问者理解:-)

标签: php soap microsoft-dynamics nav navision


【解决方案1】:

已解决

通过https://stackoverflow.com/questions/14363147/php-soapclient-malformed-xml#:~:text=override%20the%20SoapClient::__doRequest

只需扩展原生 PHP SoapClient 并覆盖 SoapClient::__doRequest() 方法。

例子:

<?php

declare(strict_types=1);


namespace App\Soap;


use SoapClient;

class Client extends SoapClient
{
    public function __doRequest($request, $location, $action, $version, $one_way = 0)
    {
        $request = $this->filter($request);

        parent::__doRequest($request, $location, $action, $version, $one_way);
    }

    private function filter(string $request): string
    {
        return preg_replace( '#\R+#', '', $request);
    }
}

你已经完成了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    相关资源
    最近更新 更多