如何覆盖 Laminas XML-RPC 服务器响应的 headers?

How to override the headers of Laminas XML-RPC server response?

正在尝试覆盖 Laminas XML-RPC server 响应的 header。 header Content-Type 应该是 application/xml 而不是默认的 text/html。阅读文档后不清楚该怎么做,它指出:

Similar to request objects, Laminas\XmlRpc\Server can return custom response objects; by default, a Laminas\XmlRpc\Response\Http object is returned, which sends an appropriate Content-Type HTTP header for use with XML-RPC. Possible uses of a custom object would be to log responses, or to send responses back to STDOUT.

To use a custom response class, use Laminas\XmlRpc\Server::setResponseClass() prior to calling handle().

setResponseClass() 的用法示例,但没有 class 的用法示例。通过查看源代码唯一清楚的是它应该扩展 Laminas\XmlRpc\Response 就是这样。

我尝试过但不起作用的方法:

use Laminas\XmlRpc\Response as XmlRpcResponse;

/**
 * HTTP response
 */
class XmlRpcService extends XmlRpcResponse
{
    protected $type = 'application/xml'; // This was just for testing but isn't working either

    /**
     * Override __toString() to send HTTP Content-Type header
     *
     * @return string
     */
    public function __toString()
    {
        if (! headers_sent()) {
            header('Content-Type: application/xml; charset=' . strtolower($this->getEncoding()));
        }

        return parent::__toString();
    }
}


$server = new \Laminas\XmlRpc\Server();
$server->setClass( 'SomeClass', 'namespace' );
$server->setResponseClass( XmlRpcService::class);
return $server->handle();

希望有人能为我指明正确的方向,指导我如何覆盖 header。相关报道:https://discourse.laminas.dev/t/how-to-override-the-headers-of-xml-rpc-server-response/1632

因为我正在使用 Laravel,所以我可以执行以下操作。这样就不需要为 $server->setResponseClass( XmlRpcService::class); 使用额外的 class。显然 Laravel 可以采用 $server->handle(),它本身已经是一个响应,并设置所需的 headers.

return response($server->handle(), 200)->header('Content-Type', 'text/xml');