简体   繁体   English

实时监控API更改的最佳方法?

[英]Best approach to monitor API changes in real-time?

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. 我学习PHP已有大约一个月的时间,并且通过创建我的第一个实际项目,将已经学到的技能用于测试。 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). 我计划在MVC结构中从头开始做所有事情(我知道使用Laravel之类的框架会是更好的选择,但我想在进入框架之前先了解框架在多大程度上简化了事情)。

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? 监视API更改的最佳方法是什么? For example, say I want to monitor the Stackoverflow info API: http://api.stackexchange.com/2.2/info?site=stackoverflow . 例如,假设我要监视Stackoverflow信息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. 如果它从33个新的活动用户变为34,我希望能够检测到该变化。 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). 更改后,我希望它立即通知用户(通过通知,我的意思是将Bootstrap警报div推送给用户而无需刷新页面)。

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. 我目前知道的唯一方法是使用cURL和cron作业,但是我觉得有一种更好的方法可以完成此操作,而且我想避免使用crons,因为我觉得检查后的一分钟间隔似乎不会不够快。 In addition, multiple users might be doing this at once — I've noticed that crons can be pretty taxing on my small VPS. 此外,可能有多个用户同时执行此操作-我注意到,对我的小型VPS而言,克朗可能会增加很多负担。

Feel free to tell me that I'm being an idiot by doing this in PHP. 随意告诉我,通过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. 如果有一种语言/框架更适合此类工作(例如Node?),我很想听听它。 I can put this project on hold until I learn what you guys recommend and choose a completely different project to do in PHP. 我可以搁置该项目,直到我了解你们的建议,然后选择一个完全不同的项目来用PHP进行。

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 ;) 好吧,在PHP中,您几乎卡在cURL / cron中...但是您可以在客户端中使用一些ajax来完成它,因为简单,我将使用jQuery;)

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 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... 请记住,非常频繁地使用1秒,可能5秒会更好,但这应该会让您入门,否则,是的,是的,或者使用tomcat(java)服务器更适合服务器到客户端的通信...

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

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