cURL - 构建请求以验证服务器发送的事件 [英] cURL - Structuring request to validate server sent events

查看:41
本文介绍了cURL - 构建请求以验证服务器发送的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 cURL 和服务器发送事件的新手.我知道如何使用 cURL 构建简单的 GETPOST 请求并获取响应.此外,理论上我知道服务器发送的事件是通过创建事件源的侦听器来处理的,但我不确定如何继续使用 cURL 验证任何此类 API.感谢您提供任何指导.

I am new to cURL and server sent events. I know how to build a simple GET, POST requests using cURL and getting response. Also, theoretically I am aware that server sent events are handled by creating a listener to event source but I am not sure how to proceed with validating any such API with cURL. Any guidance is appreciated.

推荐答案

SSE 是一种基于文本的协议,curl 是一种很好的方式来准确排除连接发送的问题.命令很简单:

SSE is a text-based protocol, and curl is a great way to troubleshoot exactly what your connection is sending. The command is this simple:

curl -N http://127.0.0.1/path/to/clock.php

(-N 停止任何缓冲,因此数据在接收时显示.)

(The -N stops any buffering, so data is shown as it is received.)

它输出这个:

data:2015-07-07 06:19:27

data:2015-07-07 06:19:28

data:2015-07-07 06:19:29

data:2015-07-07 06:19:30

data:2015-07-07 06:19:31

data:2015-07-07 06:19:32

注意它如何显示 SSE 协议的data:"前缀,并且还清楚地显示了双 LF.它永远运行,直到你按下 ctrl-c.

Notice how it shows the "data:" prefix of the SSE protocol, and also clearly shows the double LFs. It runs forever, until you press ctrl-c.

唯一需要指出的是,您必须使用网络服务器;您不能通过 file://协议运行 SSE.

About the only thing to point out is that you must use a web server; you cannot run SSE over the file:// protocol.

要获得更多核心故障排除,请添加 --verbose,它将显示正在发送的标头和正在接收的标头.

For more hard-core troubleshooting, add --verbose, which will show the headers being sent, and the headers being received.

SSE 确实支持 cookie,您可以像这样提供:(您首先必须准备cookies.txt"文件):

SSE does support cookies, which you could give like this: (you would first have to prepare the "cookies.txt" file):

curl -N --cookie cookies.txt http://127.0.0.1/path/to/clock.php

参见其他答案curl 文档 了解您可能想要考虑使用的其他选项.如果您正在对特定浏览器中的问题进行故障排除,请使用他们的开发工具准确找出正在发送的标头,然后您可以告诉 curl up 执行相同的操作.

See other answer and the curl documentation for other options you might want to consider using. If you are troubleshooting problems in a specific browser, use their devtools to find out exactly what headers are being sent, and then you can tell curl up to do the same.

为了完整起见,这里是clock.php脚本:

For completeness, here is the clock.php script:

<?php 
set_time_limit(0);
header("Content-type: text/event-stream");

while(1){
    echo "data:" . date("Y-m-d H:i:s") . "

";
    @ob_flush();flush();
    sleep(1);
    }

这篇关于cURL - 构建请求以验证服务器发送的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆