如何在充当侦听器的脚本中查看变量的内容:Facebook实时更新


How to see the content of a variable in a script that acts as a listener: Facebook real time update

我有两个文件,index.php和callback.php。php用于显示网站上的数据。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));
     }

我该怎么做?请让我知道。

如果你有一些示例代码,我将不胜感激。

我相信您正在描述的技术称为长轮询。

基本上,客户机向服务器发送一个ajax请求,然后坐在那里等待响应。在某些事件中(这是像Node这样的SSJS真正派上用场的地方),服务器向客户端发送新数据。接收到数据后,客户端立即发送新的请求并等待下一次更新。

流程如下:

页面加载:

 __________                              _________
|          |                            |         |
| Client A | ---> Request for Data ---> | Server  |
|__________|                            |_________|

然后呢?悬念:

 __________                              _________
|          |                            |         |
| Client A | .......................... | Server  |
| waiting  |                            | waiting |
|__________|                            |_________|

别人更新了你想要实时看到的内容:

                     __________
                    |          |
                    | Client B |
                    |__________|
                         |
                         | Sends update to Server
                         |___________________.
                                             |
                                             V
 __________                              _________
|          |                            |         |
| client A | ............<------------- | Server  |
| waiting  |                            | Reacts  |
|__________|                            |_________|
     |
     |
     V    
 __________                              _________
|          |                            |         |
| Client A |   ( No open connection)    | Server  |
| Updates  |                            | Idle... |
|__________|                            |_________|
     |
     |
     V
 ___________                              _________
|           |                            |         |
| Client A  |                            |         |
| Reacts to |                            | Server  |
| Update    | ---> Request for Data ---> |         |
|___________|                            |_________|

冲洗,重复这里要记住的是,HTTP1.1支持持久连接,这意味着它不会超时,除非您希望它超时。我建议在服务器端使用NodeJS来触发这些事件。