Instagram

From Wikichris
Jump to: navigation, search

Usage

Show Images from your own Instagram Account

Steps

Register your application

First the application must be Registered at this address : http://instagram.com/developer/

CLIENT ID	abf0a80f0699484e3cfb6a833adc2
CLIENT SECRET	abf0a80f0699484e3cfb6a833adc2
WEBSITE URL	http://dev.company.com
REDIRECT URI	http://dev.company.com/instagram/

Authenticate

You must to go the following URL:

$instagramClientID = 'abf0a80f0699484e3cfb6a833adc2';
$instagramClientSECRET = 'abf0a80f0699484e3cfb6a833adc2';
$instagramRedirect = 'http://lavilla.dev.brand-new.fr/instagram/'; //must be the same as entered during Registration
$url_token_request = 'https://api.instagram.com/oauth/authorize/?client_id='.$instagramClientID.'&redirect_uri='.urlencode($instagramRedirect).'&response_type=code';

echo "request your <a href='$url_token_request1' target='_blank'>token id</a> method #1 (Server-side (Explicit) Flow)
";

which will redirect you to your REDIRECT URI adding the $_GET['code'] we'll need from now

example: http://dev.company.com/instagram/?code=1a43473da98646634ec9fe867b232dcbd

Request the access_token

On http://dev.company.com/instagram/ my code is the following:

 if( isset($_REQUEST['code']) ){
 echo "Code received: {$_REQUEST['code']}, now use CURL:";
 $curl_code = "
 curl \
 -F 'client_id=$instagramClientID' \
 -F 'client_secret=$instagramClientSECRET' \
 -F 'grant_type=authorization_code' \
 -F 'redirect_uri=$instagramRedirect' \
 -F 'code={$_REQUEST['code']} ' \
 https://api.instagram.com/oauth/access_token
 ";
 
 echo nl2br($curl_code);
}


I use the curl code in Linux and get the following answer:

{"access_token":"060061234.b70f03b.45fe8dfbf65e989a3b2a3cfc11234567",
"user":{"username":"chris","bio":"","website":"http:\/\/wiki.gonzofamily.com","profile_picture":"http:\/\/images.ak.instagram.com\/profiles\/profile_profile_12345678_75sq_123456.jpg","full_name":"Chris",
"id":"123456789012"}}

From this answer, I take the ID and access_token

Get the images

$access_token = '060061234.b70f03b.45fe8dfbf65e989a3b2a3cfc11234567';
$user_id" = "123456789012";
  function get_instagram($user_id,$access_token, $count=6,$width=190,$height=190){
     
     $url = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token='.$access_token.'&count='.$count; 
 
     // Also Perhaps you should cache the results as the instagram API is slow
     $cache = './'.sha1($url).'.json';
     if(file_exists($cache) && filemtime($cache) > time() - 60*60){
         // If a cache file exists, and it is newer than 1 hour, use it
         $jsonData = json_decode(file_get_contents($cache));
     } else {
         $jsonData = json_decode((file_get_contents($url)));
         file_put_contents($cache,json_encode($jsonData));
     }
 
     $result = '<ul>';
     foreach ($jsonData->data as $key=>$value) {
         $result .= '
                         <li><a href="'.$value->images->standard_resolution->url.'"
                           	><img src="'.$value->images->low_resolution->url.'" alt="" width="'.$width.'" height="'.$height.'" />
                         </a></li>';
     }
     $result .= '</ul>';
     return $result;
 }
 echo get_instagram($user_id, $access_token);