【问题标题】:How to override the headers of Laminas XML-RPC server response?如何覆盖 Laminas XML-RPC 服务器响应的标头?
【发布时间】:2020-05-20 10:51:01
【问题描述】:

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

类似于请求对象,Laminas\XmlRpc\Server可以返回自定义响应对象;默认情况下,会返回一个Laminas\XmlRpc\Response\Http 对象,该对象会发送一个适当的Content-Type HTTP 标头以用于XML-RPC。自定义对象的可能用途是记录响应,或将响应发送回STDOUT

要使用自定义响应类,请在调用 handle() 之前使用 Laminas\XmlRpc\Server::setResponseClass()

有一个setResponseClass() 的用法示例,但不是该类的样子。查看源代码唯一清楚的是它应该扩展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();

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

【问题讨论】:

    标签: zend-framework xml-rpc laminas


    【解决方案1】:

    当我使用 Laravel 时,我能够做到以下几点。这样就不需要为$server->setResponseClass( XmlRpcService::class); 使用额外的类。显然 Laravel 可以获取 $server->handle(),它本身已经是一个响应,并设置所需的标题。

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

    【讨论】:

      猜你喜欢
      • 2016-08-06
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 2021-08-29
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      • 2010-12-07
      相关资源
      最近更新 更多