Instagram的实时更新标签 - 让空数据,为什么? [英] Instagram Real time updates tag - getting empty data, why?

查看:209
本文介绍了Instagram的实时更新标签 - 让空数据,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Heloo,

我工作的一个项目,我需要从Instagram的实时更新某些标记。

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

这是我的code为创建订阅

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);
?>

和输出是:

 [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
        )

)

这是code为callback.php:

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);
    }
?>

和来自activity.log一行是:

And a line from activity.log is:

[{changed_aspect:传媒,对象:标签,OBJECT_ID:冬天,时间:1385411793,subscription_id:3932963,数据:{}}]

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

我得到正确的subscription_id但数据是空的。
此外ACCES日志如下:

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

54.209.52.224 - - [25 /十一月/下一篇:20:59:20 +0100]POST /instagram/callback.php HTTP / 1.0200 231 - 巨蟒的httplib2 / 0.7.4(gzip的)

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)"

这是不错的,状态code 200,但再一次的数据是空的。 Instagram的是正在添加我的回调文件,但该数据是空的。

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.

我在做什么错了?

推荐答案

Instagram的API实时认购只会让你知道什么时候出现了一个更新到你的订阅对象,而不是更新的是什么。一旦你收到通知,这是给你做出的API的调用(你的情况可能是 https://api.instagram.com/v1/tags/winter/media/recent ),并看到新的内容是什么。

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);
    }

}
?>

这篇关于Instagram的实时更新标签 - 让空数据,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆