繁体   English   中英

使用angularjs php mysql实时通知

[英]Real time notification using angularjs php mysql

我想使用angular,php和mysql在我的网站上实现像Facebook这样的实时通知。 是否有任何参考链接或教程可以帮助我?

另外,还有其他工具可以用来在我的网站上实现吗?

尝试使用Socket.io,它将帮助您实现通知,聊天或任何实时应用程序。

您可以使用这些东西来实现这些功能。

  1. 基于PHP JSON的API
  2. 基于PHP套接字的JSON API
  3. 基于套接字IO的API

所有这些API都可以通过AngularJS或jQuery轻松解析

对于PHP,如何制作API

<?php
//Set header for Javascript to recognize it as the JSON output
header('Content-Type:application/json;');
//I am using GET parameters, but POST can also be used and can make amazing APIs
switch($_GET['action']){
    case "addlike":
         //SQL Query passed to add a like as facebook
         //Set the output array to provide json Output, here's the example
         $output['status'] = 200;
         $output['message'] = 'Like Added';
    break;
    case "addcomment":
    break;
}
echo json_encode($output);

要使用上面的代码,URL将是:

http://yourserve/youfile.php/?action=addlike

输出将是

{
  "status":200,
  "message":"Like Added"
}

如何在jQuery中使用它

/** For Example you have like button with class="btnlike" **/
$('.btnlike').on('click',function(){
    $.get('http://yourserve/youfile.php',{action:'addlike'},function(data){
        if(data['status'] == 200){ alert('You Liked the Post'); }
    });
});

如何在AngularJS中使用

app.controller('myCtrl',function($scope,$http){
    $scope.message = '';
    $scope.addlike = function(){
        $http.get('http://yourserve/youfile.php',{action:"addlike"}).success(function(data){
             if(data['status']==200){ $scope.messages = 'You liked the post'; }
        });
    };
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM