Getting Instagram subscription JSON data from post in PHP

笑着哭i 提交于 2019-12-01 11:18:06
Evaldo Junior

If you are using PHP, I guess the simplest way to access input data is using $_GET and $_POST superglobals. In this case, try to var_dump($_POST) and see what you get.

If you get some content from $_POST, you can use json_decode to decode JSON into an array.

You can also try some PHP implementations of the Instagram API, like this one: https://github.com/macuenca/Instagram-PHP-API It will to the work you need.

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!