Working on an Instagram Photo Downloader

+3 Bucky Roberts · July 15, 2014
Here is what I have so far. Kind of tired so I'm going to bed. Feel free to make any improvements or suggestions :)

To use it, you need to make a Instagram developer account at http://instagram.com/developer/ (it's really fast and easy) Then just fill out lines 17-19 with your developer info and on line 22 you need to give it your Instagram username. 

It just displays the pictures now, I will write a script to download them and save them when I wake up. Night. 



<?php

set_time_limit(0); //script will run for an infinite amount of time
ini_set('default_socket_timeout', 300);
session_start(); //starts new or resume existing session

/*-------- MySQL Database Settings (for future development) -------- */
define("HOST" , 'localhost');
define("DATABASE" , '');
define("USERNAME" , '' );
define("PASSWORD" , '');
mysql_connect (HOST, USERNAME, PASSWORD)or die("Could not connect: ".mysql_error());
mysql_select_db(DATABASE) or die(mysql_error());


/*-------- Instagram API KEYS --------*/
define("clientID" , ''); //associated your developer account with this program
define("clientSecret" , ''); //password
define("redirectURI" , '' ); //after users choose whether to let you use access account or not (must match the one you registered)

//set the username of the images you would like to download
$userName = 'thenewboston';

//talk to instagram
function connectToInstagram($url){
$ch = curl_init();

curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2
));

$result = curl_exec($ch);

curl_close($ch);
return $result;
}

//get userID
function getUserID($userName){

$url = 'https://api.instagram.com/v1/users/search?q='. $userName .'&client_id='. clientID;
$inst_stream = connectToInstagram($url);
$results = json_decode($inst_stream, true);

return $results['data'][0]['id'];
}

//print out the images
function printImages($userID){
$url = 'https://api.instagram.com/v1/users/'. $userID .'/media/recent?client_id='. clientID .'&count=-1';
$inst_stream = connectToInstagram($url);
$results = json_decode($inst_stream, true);

//parse through results
foreach($results['data'] as $item){
$image_link = $item['images']['low_resolution']['url'];
echo '<img src="'.$image_link.'" />';
}
}


$userID = getUserID($userName);
printImages($userID);

//keep the URLS or have someway to make sure you don't download the same images twice

?>

Post a Reply

Replies

Oldest  Newest  Rating
0 David Tkachuk · July 15, 2014
It doesn't have to crash the server. You can make this into an automated CRON job that will periodically fetch instagram photos of users, then inserting it into a database. If you don't have the option to have Crons then caching the instagram photos would help out significantly. 
  • 1

PHP

106,942 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators