是否有可能基于PHP CURL请求调用javascript函数

时间:2018-11-17 12:51:37

标签: javascript php jquery codeigniter php-curl

我想使用Codeigniter从CURL请求中调用javascript函数。基本上,我想在数据库中输入数据时在浏览器中触发通知。因此,当输入数据时,我将使用CURL请求并加载一页,然后在该页面中调用触发通知功能。我在下面尝试过,但是没有用。

用于卷曲请求的PHP代码

$post = $_POST;

        $url = str_replace('/ws/'.APP_VERSION,'',site_url('mydomain/notification_update'));
       $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $server_output = curl_exec($ch);

        curl_close ($ch);

另一个基于CURL请求调用的PHP页面

public function notification_update() {
        $post = $_POST;
        $this->load->view('header', $post, FALSE);

    }

查看页面

<script type="text/javascript">
        var w_type = '<?= (isset($w_type) && $w_type == 'for_window_noty') ? $w_type : '' ?>';
            console.log('w_type', w_type);
          if(w_type) {
                console.log('succes test');
          }


</script>

我这里只是用于查看页面的javascript代码。

注意:我的发布参数是w_type = for_window_noty

1 个答案:

答案 0 :(得分:0)

服务器发送事件

使用事件监听器监听Server Sent Events

触发后,显示您的消息。

它使用事件源

if (!!window.EventSource) {
  var source = new EventSource('stream.php')
} else {
  // Result to xhr polling :(
}

你可以听

source.addEventListener('message', function(e) {
  console.log(e.data)
}, false)

WebSockets API

另一种方法是使用WebSockets API

WebSocket允许客户端和服务器之间进行双向通信。

要付出一些代价才能实现。

相关问题