I have set up a cron job to track who liked my particular media on instagram.
I am saving media id from real time subscription and then query api to get who liked it and saving the userid after every 1 hour, working fine but not optimized as it requires too many request to server.
Any better solution to this. Below a little snippet of my code.
<?php
while($row = mysqli_fetch_assoc($r)){
$media_id=$row['media_id'];
$url='https://api.instagram.com/v1/media/'.$media_id.'/likes?access_token='.$access.'';
$inst_stream = callInstagram($url);
$results = json_decode($inst_stream, true);
foreach($results['data'] as $item){
$insta_id=$item['id'];
// insert & update code
}}
?>
I assume you are looking to stay within some API rate limits.
It seems that instagram does not allow passing multiple media_id's so you are stuck with one request per media id.
If you have more items (media_id's) to track than the hourly limit, then I suggest to split the request in hourly batches.
Ex. If the hourly rate limitation is 5000 requests and you have 10000 images that you need to track,
then split your media id's in 2 batches and
you will run two scripts or the same script twice using some parameters.
Instagram provides some information on the rate limit quota,
see here https://instagram.com/developer/limits/
Information regarding the global rate limits is included in the HTTP header on the response to each of your calls, which enables your app to determine its current status with respect to these rate limits.
It all depends on how many items you need to track,
But all you can do is work around the confines of the rate limit of 5000 requests per hour per access token.
You will still send one request per media id.