Reputation: 15
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.
Upvotes: 1
Views: 1347
Reputation: 342
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...
Upvotes: 2