从PHP中的帖子获取Instagram订阅JSON数据 [英] Getting Instagram subscription JSON data from post in PHP

查看:102
本文介绍了从PHP中的帖子获取Instagram订阅JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Instagram API的整个订阅过程似乎不那么简单.

This whole process of subscriptions for the Instagram API seems to be less than straight forward.

我设置了一些代码来接收Instagram击中我时发送的帖子数据,并从我的一个订阅中发布帖子的通知.但是,当我尝试查看数据(原始JSON)时,它发布了我无法理解的信息.如果我print_rvar_dump我只得到数字1.

I have some code set up to receive the post data sent when Instagram hits me with a notification of a post from one of my subscriptions. However when I try to view the data, the raw JSON, it posts I can't get at it. If I print_r or var_dump I just get the number 1.

请参阅我的代码以访问数据:

See my code for accessing the data:

    // Catches realtime updates from Instagram
if ($_SERVER['REQUEST_METHOD']==='POST') {
     // Retrieves the POST data from Instagram
    $update = file_get_contents('php://input');
    $data = json_decode($update);

    var_dump($data); //Outputs 1
    print_r($data[0]); //Outputs 1

}   

如何获取JSON作为数组?

How can I get at the JSON as an array?

这是JSON的样子:

[
    {
        "subscription_id": "1",
        "object": "user",
        "object_id": "1234",
        "changed_aspect": "media",
        "time": 1297286541
    },
    {
        "subscription_id": "2",
        "object": "tag",
        "object_id": "nofilter",
        "changed_aspect": "media",
        "time": 1297286541
    },
    ...
]

感谢您的帮助.

更新1

我已经使用PHP打印HTTP标头.有内容是因为它显示的是长度.仍然无法做到这一点.我认为这排除了Instagram的问题

I've used PHP to print the HTTP headers. There's content because it show's it's length. Still unable to get at it though. This rules out it being an Instagram issue, I think

推荐答案

Woop找到了问题并解决了.调试起来并不容易,因为所有这些都是在Instagram到达您的页面时发生的,因此您实际上看不到输出.

Woop found the problem and solved it. It's not easy to debug because all of this happens when Instagram hit your page so you don't really see the output.

我需要做的是创建一个foreach循环来运行已解码的JSON.经过大量的调试和初步摸索之后,JSON并不是空的,它只是从JSON数组开始.

What I needed to do was create a foreach loop to run through the decoded JSON. After a lot of debugging and head scratching the JSON isn't empty, it just starts with a JSON array.

无论如何,这是现在可以生效的代码 :

Anyway here's the code now that works:

// Catches realtime updates from Instagram
    if ($_SERVER['REQUEST_METHOD']==='POST') {
         // Retrieves the POST data from Instagram
        $update = file_get_contents('php://input');
        $data = json_decode($update);

        foreach($data as $k => $v) // can be multiple updates per call
        {
            $sub_id = $v->subscription_id; //Contains the JSON values
            $user = $v->object_id;
        }
     }

例如,如果您想查看$ sub_id的输出,例如,建议您记录它们或通过电子邮件将其发送给自己.

If you want to see the outputs from $sub_id for example I suggest logging them or email them to yourself for example.

这篇关于从PHP中的帖子获取Instagram订阅JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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