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

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

问题描述

嘿嘿

我正在做一个项目,我需要 Instagram 为特定标签实时更新.

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

这是我创建订阅的代码

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

)

这是 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": "media", "object": "tag", "object_id": "winter", "time": 1385411793, "subscription_id": 3932963, "data": {}}]

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

我得到了正确的 subscription_id,但数据为空.另外访问日志看起来像:

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

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

这很好,状态码为 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 Realtime 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天全站免登陆