Get instagram's video media source using oembed endpoints

后端 未结 3 1302
一向
一向 2021-02-03 15:04

THE CONTEXT

I have a piece of (jQuery) ajax code that has been happily working for about 9 months until the last couple of weeks or so.

This code uses Instagr

3条回答
  •  故里飘歌
    2021-02-03 15:24

    UPDATE [March 2015] : For an extended and updated version of this solution, please visit http://www.picssel.com/build-a-simple-instagram-api-case-study/


    @ProllyGeek's answer provided a good workaround to scrape the Instagram video page (well deserved bounty), however it relies on the whateverorigin.org third-party service, which will work fine unless the service eventually becomes unavailable.

    Since the latest already happened to me in a production environment, I had to look for a more reliable alternative so I decided to use php's file_get_contents to scrape the video link from an own-hosted PHP module.

    I basically followed the same logic proposed by @ProllyGeek but translated to PHP so:

    The getVideoLink.php module :

    
    

    Of course, you may need to analyze the response manually to know what you are looking for.

    Then the AJAX call to the PHP module from my main page :

    var instaLink = "http://instagram.com/p/mOFsFhAp4f/"; // the Coca Cola video link
    jQuery(document).ready(function ($) {
        $.ajax({
            url: "getVideoLink.php?instalink="+instaLink,
            dataType : "html",
            cache : false,
            success : function (data) {
                console.log(data); // returns http://distilleryvesper3-15.ak.instagram.com/b0ce80e6b91111e3a16a122b8b9af17f_101.mp4
            },
            error : function () {
                console.log("error in ajax");
            }
        });
    }); // ready 
    

    It's assumed your host supports php to use this method.


    EDIT [November 19, 2014]

    I have modified the getVideoLink.php module (now getInstaLinkJSON.php) to actually get the JSON information from an specific Instagram media link like http://instagram.com/p/mOFsFhAp4f/

    This is much more useful than just scraping the video's URL and can be used for images too.

    The new getInstaLinkJSON.php code :

    
    

    I am sanitizing both the user's input and the file_get_contents() response, however I am not stripping slashes or HTML characters from the last since I will be returning a JSON response.

    Then the AJAX call:

    var instaLink = "http://instagram.com/p/mOFsFhAp4f/"; // demo
    jQuery.ajax({
        url: "getInstaLinkJSON.php?instalink=" + instaLink,
        dataType : "json", // important!!!
        cache : false,
        success : function ( response ) {
            console.log( response ); // returns json
            var media = response.entry_data.DesktopPPage[0].media;
    
            // get the video URL
            // media.is_video : returns true/false
    
            if( media.is_video ){
                console.log( media.video_url ); // returns http://distilleryvesper3-15.ak.instagram.com/b0ce80e6b91111e3a16a122b8b9af17f_101.mp4
            }
        },
        error : function () {
            console.log("error in ajax");
        }
    });
    

    EDIT [May 20, 2020]

    currently working PHP

    
    

提交回复
热议问题