$instagram_redirect_url = $http_location . '/index.php?action=instagram_login';
 //'http://dev.cartworks.com/product_stages/index.php?action=instagram_login'; //path to your script
 //include instagram api files
 cw_include('addons/instagram_login/include/src/Instagram.php');
 $instagram = new MetzWeb\Instagram\Instagram(array('apiKey' => $instagram_client_id, 'apiSecret' => $instagram_client_secret, 'apiCallback' => $instagram_redirect_url));
 $instagram_login_info =& cw_session_register('instagram_login_info');
 //If code is empty, redirect user to instagram authentication page for code.
 //Code is required to aquire Access Token from instagram
 //Once we have access token, assign token to session variable
 //and we can redirect user back to page and login.
 if (isset($_GET['code'])) {
     if ($_GET['action'] == 'instagram_login') {
         // receive OAuth token object
         $data = $instagram->getOAuthToken($_GET['code']);
         // store user access token
         $instagram->setAccessToken($data);
         // now you have access to all authenticated user methods
         $result = $instagram->getUserMedia();
         $instagram_login_info['data'] = $data;
         $instagram_login_info['result'] = $result;
     }
 }
 if ($instagram_login_info['data']->user->id) {
     //For logged in user, get details from instagram using access token
     $instagram_user_id = $instagram_login_info['data']->user->id;
     $instagram_uname = $instagram_login_info['data']->user->username;
     $instagram_user_name = $instagram_login_info['data']->user->full_name;
     if (empty($instagram_user_name)) {
         $instagram_user_name = $instagram_uname;
     }
     $instagram_email = "instagram-" . $instagram_user_id . "-tmp-email";