Search code examples
instagraminstagram-apiinstagram-graph-api

Getting User Media with Instagram Basic Display API


I would like to retrieve photos from an account with the Instagram Display API but I can't find anything on the documentation.

The closest thing to that is this https://developers.facebook.com/docs/instagram-basic-display-api/guides/getting-profiles-and-media?locale=en_US but it only allows you to recover photos of the person connected to the API. However, I would like to be able to recover photos of anyone from their username, or from a hashtag.

GET /me?fields={media}&access_token={access-token}

I also found this https://developers.facebook.com/docs/instagram-basic-display-api/reference/user/media but it is nowhere explained how to get someone's user-id from a username.

GET https://graph.instagram.com/{user-id}/media?access_token={access-token}

I find a way to do it without Instagram API (Sources https://stackoverflow.com/a/33783840/12739212) but I'm afraid they will change their code and therefore this solution will no longer work so I would like to do it with the Instagram API

        $html = file_get_contents('https://instagram.com/' . $username . '/');
        preg_match('/_sharedData = ({.*);<\/script>/', $html, $matches);
        $profil_data = json_decode($matches[1])->entry_data->ProfilePage[0]->graphql->user;
        $data_edges = $profil_data->edge_owner_to_timeline_media->edges;

        foreach ($data_edges as $key => $value) {
            $img_url = $value->node->display_url;
            echo "<img src=" . $img_url . ">";
        }

Thanks in advance.


Solution

  • So I finally gave up using the API and used this library instead https://github.com/postaddictme/instagram-php-scraper