Where do I find the Instagram media ID of a image

后端 未结 15 1377

I\'m am looking for the MediaID of an Instagram image which has been uploaded. It should look like

1234567894561231236_33215652

15条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-22 19:54

    So the most up-voted "Better Way" is a little deprecated so here is my edit and other solutions:

    Javascript + jQuery

    $.ajax({
        type: 'GET',
        url: 'http://api.instagram.com/oembed?callback=&url='+Url, //You must define 'Url' for yourself
        cache: false,
        dataType: 'json',
        jsonp: false,
        success: function (data) {
           var MediaID = data.media_id;
       }
    });
    

    PHP

    $your_url = "" //Input your url
    $api = file_get_contents("http://api.instagram.com/oembed?callback=&url=" . your_url);      
    $media_id = json_decode($api,true)['media_id'];
    

    So, this is just an updated version of @George's code and is currently working. However, I made other solutions, and some even avoid an ajax request:


    Shortcode Ajax Solution

    Certain Instagram urls use a shortened url syntax. This allows the client to just use the shortcode in place of the media id if requested properly.

    An example shortcode url looks like this: https://www.instagram.com/p/Y7GF-5vftL/

    The Y7GF-5vftL is your shortcode for the picture.

    Using Regexp:

    var url = "https://www.instagram.com/p/Y7GF-5vftL/"; //Define this yourself
    var Key = /p\/(.*?)\/$/.exec(url)[1];
    

    In the same scope, Key will contain your shortcode. Now to request, let's say, a low res picture using this shortcode, you would do something like the following:

        $.ajax({
            type: "GET",
            dataType: "json",
            url: "https://api.instagram.com/v1/media/shortcode/" + Key + "?access_token=" + access_token, //Define your 'access_token'
            success: function (RawData) {
                var LowResURL = RawData.data.images.low_resolution.url;
            }
        });
    

    There is lots of other useful information, including the media id, in the returned RawData structure. Log it or look up the api documentation to see.


    Shortcode Conversion Solution

    You can actually convert your shortcode to the id fairly easily! Here's a simple way to do it in javascript:

    function shortcodeToInstaID(Shortcode) {
          var char;
          var id = 0;
          var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
          for (var i = 0; i < Shortcode.length; i++) {
              char = Shortcode[i];
              id = (id * 64) + alphabet.indexOf(char);
          }
          return id;
      }
    

    Note: If you want a more robust node.js solution, or want to see how you would convert it back, check out @Slang's module on npm.


    Full Page Solution

    So what if you have the URL to a full Instagram page like: https://www.instagram.com/p/BAYYJBwi0Tssh605CJP2bmSuRpm_Jt7V_S8q9A0/

    Well, you can actually read the HTML to find a meta property that contains the Media ID. There are also a couple other algorithms you can perform on the URL itself to get it, but I believe that requires too much effort so we will keep it simple. Either query the meta tag al:ios:url or iterate through the html. Since reading metatags is posted all over, I'll show you how to iterate.

    NOTE: This is a little unstable and is vulnerable to being patched. This method does NOT work on a page that uses a preview box. So if you give it the current HTML when you click on a picture in someone's profile, this WILL break and return a bad Media ID.

    function getMediaId(HTML_String) {
      var MediaID = "";
      var e = HTML_String.indexOf("al:ios:url") + 42; //HTML_String is a variable that contains all of the HTML text as a string for the current document. There are many different ways to retrieve this so look one up. 
      for (var i = e; i <= e + 100; i++) { //100 should never come close to being reached
          if (request.source.charAt(i) == "\"")
             break;
          MediaID += request.source.charAt(i);
      }
      return MediaID;
    }
    

    And there you go, a bunch of different ways to use Instagram's api to get a Media ID. Hope one fixes your struggles.

提交回复
热议问题