Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instagram Real time updates tag - getting empty data, why?

Heloo,

I am working on one project and I need real time updates from Instagram for certain tag.

This is my code for Create a Subscription

<?php
    $client_id = 'MOJID';
    $client_secret = 'MOJIDSECRET';
    $redirect_uri = 'http://example.net/instagram/callback.php';
    $apiData = array(
        'client_id' => $client_id,
        'client_secret' => $client_secret,
        'redirect_uri' => $redirect_uri,
        'aspect' => "media",
        'object' => "tag",
        'object_id' => "winter",
        'callback_url' => $redirect_uri
    );

    $apiHost = 'https://api.instagram.com/v1/subscriptions/';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $apiHost);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiData));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $jsonData = curl_exec($ch);
    curl_close($ch);
    var_dump($jsonData);
?>

And output is:

 [meta] => stdClass Object
        (
            [code] => 200
        )

    [data] => stdClass Object
        (
            [object] => tag
            [object_id] => winter
            [aspect] => media
            [callback_url] => http://example.net/instagram/callback.php
            [type] => subscription
            [id] => 3932963
        )

)

This is code for callback.php:

<?php
    if (isset ($_GET['hub_challenge'])){
        echo $_GET['hub_challenge'];
    }
    else{
        $myString = file_get_contents('php://input');
        $ALL = $myString."\r\n";
        file_put_contents('activity.log', $ALL, FILE_APPEND | LOCK_EX);
    }
?>

And a line from activity.log is:

[{"changed_aspect": "media", "object": "tag", "object_id": "winter", "time": 1385411793, "subscription_id": 3932963, "data": {}}]

I am getting the right subscription_id but the data is empty. Also Acces log looks like:

54.209.52.224 - - [25/Nov/2013:20:59:20 +0100] "POST /instagram/callback.php HTTP/1.0" 200 231 "-" "Python-httplib2/0.7.4 (gzip)"

And this is good, status code 200 but once again data is empty. Instagram is 'comming' to my callback file but the data is empty.

What am I doing wrong?

like image 528
consigliere Avatar asked Nov 25 '13 21:11

consigliere


2 Answers

Instagram Realtime API subscription will only let you know when there has been an update to your subscribed object, not what the update is. Once you receive a notification, it is up to you to make a call to the API (in your case probably https://api.instagram.com/v1/tags/winter/media/recent ) and to see what the new content is.

Depending on the volume of changes, you will probably want to batch these calls up at certain time intervals, but the below should be a good start. You'll also probably only want to retrieve items you haven't already retrieved.

<?php
if (isset ($_GET['hub_challenge'])){
    echo $_GET['hub_challenge'];
}
else{
    $myString = file_get_contents('php://input');
    $sub_update = json_decode($myString);

    $access_token = '{ previously saved, get this from DB or flatfile }';

    foreach($sub_update as $k => $v) // can be multiple updates per call
    {
        $recent_data = file_get_contents('https://api.instagram.com/v1/tags/'.$sub_update->object_id.'/media/recent?access_token='.$access_token);
        file_put_contents('activity.log', serialize($recent_data->data), FILE_APPEND | LOCK_EX);
    }

}
?>
like image 177
chrisboustead Avatar answered Oct 05 '22 22:10

chrisboustead


I get the $_POST to say there is an update from one of authenticated users, problem is there are 153 of them subscribed to the feed. The only way to get check if a user has an update is to get their latest.

I also can post a photo on IG myself. The $_POST is received triggering the loop of users being polled but the newest item won't show up in the api at the same time as the $_POST trigger arrives from the real-time api.

like image 39
Philip Avatar answered Oct 05 '22 23:10

Philip