试图覆盖Laminas XML-RPC server响应的标题。标头Content-Type
应该为application/xml
,而不是默认的text/html
。阅读文档尚不清楚该怎么做,它指出:
类似于请求对象,
Laminas\XmlRpc\Server
可以返回自定义响应对象;默认情况下,返回一个Laminas\XmlRpc\Response\Http
对象,该对象发送一个适当的Content-Type
HTTP标头以与XML-RPC一起使用。自定义对象的可能用途是记录响应,或将响应发送回STDOUT
。要使用自定义响应类,请在调用
Laminas\XmlRpc\Server::setResponseClass()
之前使用handle()
。
有一个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
当我使用Laravel时,我可以执行以下操作。这样,不需要为$server->setResponseClass( XmlRpcService::class);
使用其他类。显然,Laravel可以采用$server->handle()
,它本身已经是一个响应,并设置所需的标头。
return response($server->handle(), 200)->header('Content-Type', 'text/xml');