Instagram how to get my user id from username?

前端 未结 24 1072
傲寒
傲寒 2020-11-30 17:14

I\'m in the process of embedding my image feed in my website using JSON, the URL needs my user id so I can retrieve this feed.

So, where can I find/get my user id?

24条回答
  •  一生所求
    2020-11-30 17:54

    Instead of using the API, one can examine the Instagram userpage to get the id. Example code in PHP:

    $html = file_get_contents("http://instagram.com/");
    $doc = new DOMDocument();
    $doc->loadHTML($html);
    
    $xpath = new DOMXPath($doc);
    $js = $xpath->query('//body/script[@type="text/javascript"]')->item(1)->nodeValue;
    
    $start = strpos($js, '{');
    $end = strrpos($js, ';');
    $json = substr($js, $start, $end - $start);
    
    $data = json_decode($json, true);
    $data = $data["entry_data"]["UserProfile"][0];
    # The "userMedia" entry of $data now has the same structure as the "data" field
    # in Instagram API responses to user endpoints queries
    
    echo $data["user"]["id"];
    

    Of course, this code has to be adapted if Instagram changes its page format.

提交回复
热议问题