Search code examples
real-timeinstagramendpointsinstagram-api

Instagram Real-time API duplicate requests


I have an issue where when I create a real-time subscription I get duplicate notifications from different Instagram IP addresses. I have it set up so that when I get a notification, I send a request for latest updates using the min_tag_id setting. I store that in my db to use it for the next request. I don't always get duplicates, but when I do, everything about the notification is the same (time, object,changed_aspect), except I can tell they are different from my debugging output which lists two almost identical requests... the only differing info being a different IP address and the REQUEST_TIME_FLOAT is different by about 1/10th of a second. They even have the same HTTP_X_HUB_SIGNATURE value.

My general algorithm is:

process_subscription_update($data){
    # get old min_id
    $min_tag_id = mysqli_fetch_object(mysqli_query($dbconnecti,sprintf("SELECT instagram_min_id+0 as instaid FROM xxxx WHERE xxxx=%d",$_GET['xxxx'])));
    $min_id = $min_tag_id->instaid;
    # make api call
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, 'https://api.instagram.com/v1/tags/'.$_GET['tag'].'/media/recent?client_id=xxxx&min_tag_id='.$min_id.($min_id==0?'&count=1':''));
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    $result = curl_exec($ch);
    curl_close($ch);
    $i = json_decode($result);
    if ($min_id == $i->pagination->min_tag_id) { exit; }
    # write new min_id to db
    record_min_id($i->pagination->min_tag_id);
    $data2 = $i->data;
    foreach($data2 as $d) {
        process_instagram($d);
    }
    // debugging output: ****************
    $file = file_get_contents($_SERVER['DOCUMENT_ROOT'].'instagram/updates.txt');
    $foo = "\n";
    foreach($_SERVER as $key_name => $key_value) {
        $foo .= $key_name . " = " . $key_value . "\n";
    }
    $fulldata = $file . "\n\n\n" .  $result . "\n min_id = " . $min_id . $foo;
    $fulldata .= "\nTIME:".$data[0]->time;
    $fulldata .= "\nOBJECT:".$data[0]->object;
    $fulldata .= "\nCHANGED_ASPECT:".$data[0]->changed_aspect;
    file_put_contents($_SERVER['DOCUMENT_ROOT'].'instagram/updates.txt', $fulldata);    
    // end debugging output *************

}

I'd like to avoid checking if the instagram message id already exists in my db within the process_instagram function, and with the duplicates only coming 1/10th of a second apart, I don't know if that would work anyway.

Anybody else experience this and/or have a solution?


Solution

  • I solved this. I don't think there is anything I can do about receiving the duplicate notifications. So, when writing the Instagram to my db, I have a field for the Instagram id and put a unique constraint on the field. After doing the mysqli INSERT, I check to see if the errno = 1062, and if it does, I exit.

    mysqli_query($dbconnecti,"INSERT INTO xxx (foo, etc, instagram_id ...")
    if ($dbconnecti->errno==1062) { exit; }
    ...
    // more script runs here if we don't have a duplicate.