dongzaocheng3214 2014-07-18 04:10
浏览 203
已采纳

实时监控API变化的最佳方法?

I've been learning PHP for about a month and I'm putting the skills that I've learned to the test by creating my first real project. I plan on doing everything from scratch in an MVC structure (I know that using a framework such as Laravel would be a better choice, but I want to understand how much easier frameworks makes things before I jump into them).

However, I've hit a wall with my project, and I would greatly appreciate some advice. What would be the best way to watch for API changes? For example, say I want to monitor the Stackoverflow info API: http://api.stackexchange.com/2.2/info?site=stackoverflow. If it changes from 33 new active users to 34, I'd want to be able to detect that change. Once it does change, I would like for it to immediately notify the user (by notify I just mean a Bootstrap alert div that's pushed to the user without them having to refresh the page).

The only way I currently know of doing this would be with cURL and cron jobs, but I feel like there's a better way to accomplish this, and I'd like to avoid using crons because I feel as if the one minute interval behind checks wouldn't be fast enough. In addition, multiple users might be doing this at once — I've noticed that crons can be pretty taxing on my small VPS.

Feel free to tell me that I'm being an idiot by doing this in PHP. If there is a language/framework that is better designed for this type of work (maybe Node?), I'd love to hear about it. I can put this project on hold until I learn what you guys recommend and choose a completely different project to do in PHP.

  • 写回答

1条回答 默认 最新

  • doutao1171 2014-07-18 04:33
    关注

    well, in PHP you're pretty much stuck with cURL/cron... but you can do it in the client with some ajax, i'll use jQuery as it's easy ;)

    in the footer of your app (i'm assumming you'll be including one footer across all pages..)

    <!-- make sure jQuery is included before this!! -->
    <script type="text/javascript">
      jQuery(document).ready(function($){
        //we'll create an interval to poll the API..
        setInterval(function(){
          //do a POST to your cURL script...
          $.post('http://website.com/curl.php',{ doCurl:true },function(resp){
             //resp is an object ... { numUsers:SOMENUMBER }
             //say you have a div somewhere on the page to display this number...could be hidden.......
             if(resp.numUsers != parseInt($('#api-users').html())){
               //display your popup..or whatever..
             }
             //load the div with the returned number..
             $('#api-users').html(resp.numUsers);
    
          },'json');
        },1000); //runs every second...
      });
    </script>
    

    curl.php

     <?php
    if(isset($_POST['doCurl'])){
      //do the curl request to get the number...
      $ch = curl_init('http://api.stackexchange.com/2.2/info?site=stackoverflow');
      curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
      $curlout = curl_exec($ch);
      curl_close($ch);
      if($curlout){
        $out = json_decode($curlout,true); //decode into array
        $resp['numUsers'] = $out['items'][0]['new_active_users'];
      }else{
        $resp['numUsers'] = 0;
      }
      exit(json_encode($resp));
    
    }
    ?>
    

    That should do the trick! keep in mind 1 second is pretty frequent, would probably be better at 5 seconds, but this should get you started, otherwise yeah node, or using a tomcat(java) server would be more suited for server to client communication...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R