Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Quick-and-dirty Instagram web scrape, just in case you don't think you should have to make your users log in to deliver them public photos.
//returns a big old hunk of JSON from a non-private IG account page.
function scrape_insta($username) {
$insta_source = file_get_contents(''.$username);
$shards = explode('window._sharedData = ', $insta_source);
$insta_json = explode(';</script>', $shards[1]);
$insta_array = json_decode($insta_json[0], TRUE);
return $insta_array;
//Supply a username
$my_account = 'cosmocatalano';
//Do the deed
$results_array = scrape_insta($my_account);
//An example of where to go from there
$latest_array = $results_array['entry_data']['ProfilePage'][0]['user']['media']['nodes'][0];
echo 'Latest Photo:<br/>';
echo '<a href="'.$latest_array['code'].'"><img src="'.$latest_array['display_src'].'"></a></br>';
echo 'Likes: '.$latest_array['likes']['count'].' - Comments: '.$latest_array['comments']['count'].'<br/>';
/* BAH! An Instagram site redesign in June 2015 broke quick retrieval of captions, locations and some other stuff.
echo 'Taken at '.$latest_array['location']['name'].'<br/>';
//Heck, lets compare it to a useful API, just for kicks.
echo '<img src="'.$latest_array['location']['latitude'].','.$latest_array['location']['longitude'].'&zoom=13&size=300x150&sensor=false">';

Vheissu commented Feb 17, 2014

Amazingly simple and effective. Thank you for sharing this.

How can one go about running this script?

made a similar tool over here, to get a user's entire history:

Here's a quick class to only get the image from an Instagram image url:


class InstagramScraper
    protected $content;
    function __construct($url)
        $this->content = @file_get_contents($url);

    public function image(){
        preg_match('#<meta +property=\\"og:image\\" +content=\\"(http.+?\.jpg)\\"#', $this->content, $result);
        return $result[1];

Still working given the recent changes to Instagram?

Nimoi commented Dec 8, 2015

Thanks! I needed to scrape some individual posts. This still works.

lmj0011 commented Dec 16, 2015

if you want to get back a certain number of images, just increment the $results_array like so:

 for($cnt=0; $cnt < 20; $cnt++)
 $latest_array = $results_array['entry_data']['ProfilePage'][0]['user']['media']['nodes'][$cnt];

 echo 'Latest Photo:<br/>';
 echo '<a href="'.$latest_array['code'].'"><img src="'.$latest_array['display_src'].'"></a></br>';
 echo 'Likes: '.$latest_array['likes']['count'].' - Comments: '.$latest_array['comments']['count'].'<br/>';

Thanks for sharing this gist

Thank you so much for this, love the quick and dirty-ness of it! I have a couple of questions if you can help.

  • How can I set the number of posts to display? I'd like to show 2 or 4 of the latest
  • Is it possible to get the images by location ID using a slightly modified method? That's what I was initially trying to do, if it's not possible the userID works ok as well.

Thanks again.

raiym commented Apr 22, 2016

ravij28 commented Jun 2, 2016

It's showing below error:

Warning: file_get_contents( failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\wamp\www\Trial\curl.php on line 4

Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\www\Trial\curl.php on line 4

pistell commented Jun 2, 2016

@cosmocatalano I seriously owe you a beer or twelve for this. The Instagram API migration ruined my app and this saved me.

Awesome script, anyway to get more than 15 photos though? Or get the ajax from the load more button?

Script works fine but can you please guide me to get more then 15 posts ?

I've made web scrapper to get user X's followers using javascript that stores followers and exports in csv files. All you need to do is to keep scrolling.

 * Instagram Follower Web Scrapper 
 * Steps to use.
 * 1. Open instagram user's profile in browser
 * 2. Open console ( press F12 in chrome ) and paste all the code below
 * 3. Click on followers button and load all the followers
 * 4. Call function downloadAsCsv() by writing "downloadAsCsv()" in console to download csv file containing user's all the followers .
 * @author : Hardik Sondagar <>

var followers = [];

(function(XHR) {
    "use strict";

    var stats = [];
    var timeoutId = null;

    var open =;
    var send = XHR.prototype.send; = function(method, url, async, user, pass) {
        this._url = url;, method, url, async, user, pass);

    XHR.prototype.send = function(data) {
        var self = this;
        var start;
        var oldOnReadyStateChange;
        var url = this._url;

        function onReadyStateChange() {
            if(self.readyState == 4 && url == '') {

              var response = JSON.parse(self.response);
              followers = followers.concat(response.followed_by.nodes);


            if(oldOnReadyStateChange) {

        if(!this.noIntercept) {
            start = new Date();

            if(this.addEventListener) {
                this.addEventListener("readystatechange", onReadyStateChange, false);
            } else {
                oldOnReadyStateChange = this.onreadystatechange; 
                this.onreadystatechange = onReadyStateChange;
        }, data);

function downloadAsCsv() {

    var csvContent = "data:text/csv;charset=utf-8,";

    var header = "Username,Requested_by_viewer,Followed_by_viewer,Profile_pic_url,Full Name,is_verified,Id\n";
    csvContent += header;

    followers.forEach(function(infoArray, index){

        var data = $.map(infoArray, function(value) {
        return [value];

        dataString = data.join(",");
        csvContent += index < followers.length ? dataString+ "\n" : dataString;


    var encodedUri = encodeURI(csvContent);
    var link = document.createElement("a");
    link.setAttribute("href", encodedUri);

    var pathArray = window.location.pathname.split('/');

    var milliseconds = (new Date).getTime();
    var filename = 'followers.'+ milliseconds +'.csv';

    if(pathArray && pathArray.length > 1) {
        filename = pathArray[1] + '.' + milliseconds +'.csv';
    document.body.appendChild(link); // Required for FF; // This will download the data file named "my_data.csv".


sripadks commented Jul 1, 2016

@hardiksondagar when I try to use your script, chrome just says "undefined" when I both enter the code into the console as well as run the function. The downloaded CSV is empty.

@hardiksondagar same result.

pipep commented Sep 20, 2016

Sorry for my ignorance, i'm starting with this.. how could I get more than 1 username to be processed on one page ? I get an error all the time.. I need ig-A + ig-B + ig-C latest pictures on one page.. any ideas ?

Thank you !

Does anyone know if this is still working? My get_file_contents() is returning the Instagram signup page. Have then since done some sort of scrape prevention?

How to scrape register emails with Instagram accounts ?

Thanks, this worked great! I modified it to get the 12 most recent photos:

//returns a big old hunk of JSON from a non-private IG account page.
function scrape_insta($username) {
	$insta_source = file_get_contents(''.$username);
	$shards = explode('window._sharedData = ', $insta_source);
	$insta_json = explode(';</script>', $shards[1]);
	$insta_array = json_decode($insta_json[0], TRUE);
	return $insta_array;
//Supply a username
$my_account = 'my_instagram_username';
$results_array = scrape_insta($my_account);

// Create a comma-separated list of the first 12 image source URLs.
$url_list = $results_array['entry_data']['ProfilePage'][0]['user']['media']['nodes'][0]['display_src'];
for ($i=1; $i < 12; $i++) {
	$url_list .= ',' . $results_array['entry_data']['ProfilePage'][0]['user']['media']['nodes'][$i]['display_src'];

// Print out the list. Use Ajax.get() or something to call this script, then parse it on client: list.split(',').
echo $url_list;

BHFR commented Mar 11, 2017

Thanks for sharing bro !

just add "?__a=1" at the end of url, it's already JSON
"" for username
"" for hashtag

Spandan-Madan commented Apr 3, 2017

Is this still working? Planning to use it for a research project!

edogaafx commented Apr 10, 2017

Thank You for sharing code! Helped a lot with possibility to avoid API.
@adi64bit thanks, that made things way more easier! Maybe you could share link, where can get more this kind of useful links?

If any of you guys need an instagram nodejs bot, check out my profile repo :)

TIP: If you want square images (1x1 ratio), replace ['display_src'] with ['thumbnail_src'].

Does anyone know what you add to the script to grab the captions from all post in the account. when I added @lmj0011 edits it just gave me the same post over and over again instead of jumping to the next post.

jakim commented Jun 5, 2017

Nice find @adi64bit, thanks!

aehacker commented Jul 6, 2017

**can we get all image from instagram account ? (detect html code and get image urls ? )

Help me to create it!!**

I am scraping hashtag data but its showing me only limited number of records, Please advise me how to scrap all data.

If you are scraping a hashtag or username with @adi64bit his method, it will return a media attribute with some properties.
This media attribute contains the nodes that you are probably using to display the images. There also is a property called "page_info". This property contains information about the obtained nodes like if more nodes exists and the cursor to obtain those next nodes.

"media": { "count": 23, "nodes": [ ... ], "page_info": { "has_next_page": true, "end_cursor": "some_identifier" } }

Note: There also is an attribute called "count", this is the total amount of nodes.

After obtaining that end_cursor you might be able to obtain the next few nodes. Adding an additional parameter to the URL given by @adi64bit called "max_id" will give you the next few nodes like so:

"" for username
"" for hashtag

Thanks a ton @Voskuijlen & @adi64bit
Any info on how can we get the username for a post and the time?

Like I'm using "" for hashtag
I'm getting owner id in the results. Any way to fetch username from owner id?

Excellent. Thanks!

@sumanyu-soniwal yes if you want a username for a post and a time, it's simple, just give a get call to

Replace URL parameter value with your post URL. It will provide you brief info regarding that post. [Mandatory - post should be public]
So by this simple method, you can fulfill your basic need as you mentioned regarding "username for a post and a time"

johnperricruz commented Jan 24, 2018

As of today, ?__a=1 for instagram does not work anymore.

Update : Working as of now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment