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...

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

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line