Php 服务器发送事件:如何触发发送

Php 服务器发送事件:如何触发发送,php,server-sent-events,Php,Server Sent Events,我读过关于服务器发送事件的文章:在这里,他们给出了一个PHP发送事件的示例。我了解到头指令必须是脚本中的第一条指令。但是,在发送事件之前,我需要进行一些预处理: //Do some preprocessing in order the determine whether to send an event or not if ($sendevent) { header("Content-Type: text/event-stream\n\n"); // echo the data

我读过关于服务器发送事件的文章:在这里,他们给出了一个PHP发送事件的示例。我了解到头指令必须是脚本中的第一条指令。但是,在发送事件之前,我需要进行一些预处理:

//Do some preprocessing in order the determine whether to send an event or not
if ($sendevent) {
    header("Content-Type: text/event-stream\n\n");
    // echo the data
}
显然这是不可能的,因为“header”不是第一条指令。如何解决这个问题?只能通过从客户端发送请求来触发发送事件吗?

记住,在发送任何实际输出之前必须调用header()

这意味着您可以包含任意数量的指令,但在调用
header()
之前,脚本不得输出任何内容。 如果您的
没有输出任何内容,那么就可以了


小心不要在PHP开始标记前有空白字符。

我明白了!这对我来说是一个误解:该要求只适用于传输到客户端的数据,而不适用于脚本中未交付到客户端的PHP代码。非常感谢您提供的信息!