如何在充当监听器的脚本中查看变量的内容:Facebook 实时更新
我有两个文件,index.php 和callback.php。 Index.php 用于在网站上显示数据。 callback.php 是一个充当侦听器的脚本。其中callback.php由服务器调用,callback.php中的$update变量由服务器实时更新。我想在每次更新时查看 $update 变量的内容。
Callback.php:
if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe' &&
$_GET['hub_verify_token'] == VERIFY_TOKEN) {
echo $_GET['hub_challenge'];
} else if ($method == 'POST') {
$updates = json_decode(file_get_contents("php://input"), true);
//I want to see the content of $updates
// resend the push notification again.
error_log('updates = ' . print_r($updates, true));
}
我该怎么做?请告诉我。如果您有一些我可以开始使用的示例代码,我将不胜感激。
I have two files, index.php and a callback.php. Index.php is used to display data on the website. The callback.php is a script that acts like a listener. The callback.php is called by a server and the $update variable in callback.php is updated by the server in real time. I want to see the contents of $update variable every time it updates.
Callback.php:
if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe' &&
$_GET['hub_verify_token'] == VERIFY_TOKEN) {
echo $_GET['hub_challenge'];
} else if ($method == 'POST') {
$updates = json_decode(file_get_contents("php://input"), true);
//I want to see the content of $updates
// resend the push notification again.
error_log('updates = ' . print_r($updates, true));
}
How can i do this? Please let me know. I would appreciate if you have some sample code i could start with.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您所描述的技术称为长轮询。
基本上,客户端向服务器发送 ajax 请求并等待响应。在发生某些事件时(这是像 Node 这样的 SSJS 真正派上用场的地方),服务器向客户端发送新数据。客户端收到数据后,立即发送新的请求并等待下一次更新。
流程如下:
页面加载:
然后呢?悬念:
别人实时更新你想看的内容:
冲洗,重复
这里要记住的是,HTTP1.1 支持持久连接,这意味着除非您愿意,否则它不会超时。我建议在服务器端查看 NodeJS 来触发这些事件。
The technique I believe you are describing is called long-polling.
Basically, the client sends an ajax request to the server and sits there waiting for a response. Upon some event (this is where SSJS like Node comes in really handy), the server sends the client the new data. Upon receiving the data, the client immediately sends a new request and waits for the next update.
Here is the flow:
Page loads:
Then what? Suspense:
Someone else updates content you want to see in real time:
Rinse, Repeat
The thing to remember here is that HTTP1.1 supports a persistent connection, meaning it won't timeout unless you want it to. I'd recommend looking into NodeJS on your server side to trigger these events.