public function get_instagram() { // make sure user is logged in if (!Auth::user()) { return Redirect::to('/')->withInput()->withErrors('You must be logged in to access the Instagram panel.'); } // configure the API values $auth_config = array('client_id' => CLIENT_ID, 'client_secret' => CLIENT_SECRET, 'redirect_uri' => REDIRECT_URI); // create a new Auth object using the config values $auth = new Instagram\Auth($auth_config); // authorize app if not already authorized if (!Input::get('code')) { $auth->authorize(); } // get the code value returned from Instagram $code = Input::get('code'); $title = 'ImgHost - Instagram'; // save the access token if not already saved if (!Session::get('instagram_access_token')) { Session::put('instagram_access_token', $auth->getAccessToken($code)); } // create a new Instagram object $instagram = new Instagram\Instagram(); // set the access token on the newly created Instagram object $instagram->setAccessToken(Session::get('instagram_access_token')); // get the ID of the authorized user $current_user = $instagram->getCurrentUser(); // access the media of the authorized user $media = $current_user->getMedia(); $db_images = DB::table('images')->get(); return View::make('instagram')->with('title', $title)->with('instagram_images', $media)->with('db_images', $db_images); }
public function getUser() { $instagram = new \Instagram\Instagram(); $instagram->setAccessToken(Session::get('instagram_access_token')); $current_user = $instagram->getCurrentUser(); return $current_user; }
public function fetch($client_id, $tag) { global $wpdb; $instagram = new \Instagram\Instagram(); $instagram->setClientId($client_id); $min_tag_id = get_option("last_instagram_tag_{$tag}_id", 0); $tag = $instagram->getTag($tag); $media = $tag->getMedia(array('min_tag_id' => $min_tag_id)); update_option("last_instagram_tag_{$tag}_id", $media->getNextMaxTagId()); foreach ($media as $m) { $query = "SELECT posts.* FROM " . $wpdb->posts . " AS posts\n INNER JOIN " . $wpdb->postmeta . " AS wpostmeta ON wpostmeta.post_id = posts.ID\n AND wpostmeta.meta_key = 'degg_instagram_id'\n AND wpostmeta.meta_value = '{$m->getID()}'"; $posts = $wpdb->get_results($query, ARRAY_A); if (!$posts) { $id = wp_insert_post(array('post_title' => "{$m->getUser()} on {$m->getCreatedTime('M jS Y @ g:ia')}", 'post_content' => "<img src='{$m->getThumbnail()->url}' title='Posted by {$m->getUser()} on {$m->getCreatedTime('M jS Y @ g:ia')}'>", 'post_type' => 'degg_instagram')); add_post_meta($id, 'degg_instagram_id', "{$m->getID()}", true); add_post_meta($id, 'degg_instagram_title', "Posted by {$m->getUser()} on {$m->getCreatedTime('M jS Y @ g:ia')}", true); add_post_meta($id, 'degg_instagram_user', "{$m->getUser()}", true); add_post_meta($id, 'degg_instagram_caption', "{$m->getCaption()}", true); add_post_meta($id, 'degg_instagram_link', "{$m->getLink()}", true); add_post_meta($id, 'degg_instagram_thumbnail', $m->getThumbnail(), true); add_post_meta($id, 'degg_instagram_standard_res', $m->getStandardRes(), true); add_post_meta($id, 'degg_instagram_low_res', $m->getLowRes(), true); wp_publish_post($id); } } }
/** * Make an API request. * @param str $path * @param str $access_token * @param str $fields Comma-delimited list of fields to return from Instagram API * @return array Decoded JSON response */ public static function apiRequest($type, $id, $access_token, $params = array()) { $logger = Logger::getInstance(); $instagram = new Instagram\Instagram($access_token); if ($type == 'user') { return $instagram->getUser($id); } else { if ($type == 'friends') { $user = $instagram->getUser($id); return $user->getFollowers(); } else { if ($type == 'media') { $user = $instagram->getUser($id); $media = $user->getMedia($params); return $media; } } } }
<?php require '../vendor/autoload.php'; try { $instagram = new \Instagram\Instagram(); $instagram->login('username', 'password'); $session = $instagram->saveSession(); $instagram->initFromSession($session); } catch (\Exception $e) { echo $e->getMessage(); }
<?php require '../vendor/autoload.php'; try { $instagram = new \Instagram\Instagram(); $instagram->login('username', 'password'); var_dump($instagram->likeMedia('mediaId')); } catch (\Exception $e) { echo $e->getMessage(); }
<?php $media_ids = array('339955848751665483_11007611', '337879001662542678_11007611', '298633372268810782_11007611', '289221805769020118_11007611', '197183110736564300_11007611', '198817996966776929_11007611', '195861999348404763_11007611'); shuffle($media_ids); $instagram = new Instagram\Instagram(); $instagram->setAccessToken($_SESSION['instagram_access_token']); $media_id = isset($_GET['media']) ? $_GET['media'] : $media_ids[0]; $current_user = $instagram->getCurrentUser(); try { if (isset($_POST['action'])) { switch (strtolower($_POST['action'])) { case 'add_comment': $current_user->addMediaComment($media_id, $_POST['comment_text']); break; case 'delete_comment': $current_user->deleteMediaComment($media_id, $_POST['comment_id']); break; case 'like': $current_user->addLike($media_id); break; case 'unlike': $current_user->deleteLike($media_id); break; } } } catch (\Instagram\Core\ApiException $e) { $error = $e->getMessage(); } $media = $instagram->getMedia($media_id); $comments = $media->getComments(); $tags_closure = function ($m) {
<?php require "../vendor/autoload.php"; $instagram = new \Instagram\Instagram(); try { //Login $instagram->login("username", "password"); //Get TimelineFeed $timelineFeed = $instagram->getTimelineFeed(); echo "Page 1\n"; foreach ($timelineFeed->getItems() as $timelineFeedItem) { //Grab a list of Images for this Post (different sizes) $images = $timelineFeedItem->getImageVersions2()->getCandidates(); //Grab the URL of the first Photo in the list of Images for this Post $photoUrl = $images[0]->getUrl(); //Just echo it hey echo sprintf("Media Url: %s\n", $photoUrl); } //This will be null if there are no more pages. //The next page might be empty, but that request wont return a nextMaxId $nextMaxId = $timelineFeed->getNextMaxId(); //We have another page of Items if ($nextMaxId != null) { echo sprintf("Fetching next Page: %s\n", $nextMaxId); //Get the next page. $timelineFeed = $instagram->getTimelineFeed($nextMaxId); //Now you have the next page. //You could do the same as above, and echo the media url. //... or do something more exciting? :) echo "Page 2\n"; foreach ($timelineFeed->getItems() as $timelineFeedItem) {
/** * Get a single instagram. * * @param string $shortcode * The instagram shortcode. */ protected function fetchInstagram($shortcode) { $instagram =& drupal_static(__FUNCTION__); if (!isset($instagram)) { // Check for dependencies. // @todo There is perhaps a better way to do that. if (!class_exists('\\Instagram\\Instagram')) { drupal_set_message(t('Instagram library is not available. Consult the README.md for installation instructions.'), 'error'); return; } if (!isset($this->configuration['client_id'])) { drupal_set_message(t('The client ID is not available. Consult the README.md for installation instructions.'), 'error'); return; } if (empty($this->configuration['client_id'])) { drupal_set_message(t('The client ID is missing. Please add it in your Instagram settings.'), 'error'); return; } $instagram_object = new \Instagram\Instagram(); $instagram_object->setClientID($this->configuration['client_id']); $result = $instagram_object->getMediaByShortcode($shortcode)->getData(); if ($result) { return $result; } else { throw new MediaTypeException(NULL, 'The media could not be retrieved.'); } } }
<?php require "../vendor/autoload.php"; $instagram = new \Instagram\Instagram(); try { //Instead of logging in each time, you can init the session if you previously saved it somewhere. //Login $instagram->login("username", "password"); //Serialize the Session into Json $savedSession = $instagram->saveSession(); //Store the $savedSession json string in a database or file etc echo $savedSession . "\n"; //In a different script, Init from Saved Session json, instead of logging in again. //$savedSession would be from your database or a file etc $instagram->initFromSavedSession($savedSession); } catch (Exception $e) { //Something went wrong... echo $e->getMessage() . "\n"; }
public function testInvalidSearchTags() { $this->setExpectedException('\\Instagram\\Core\\ApiException'); $instagram = new Instagram\Instagram($this->access_token); $tags = $instagram->searchTags(''); $this->assertInstanceOf('\\Instagram\\Collection\\TagCollection', $tags); }
<?php require "../vendor/autoload.php"; $instagram = new \Instagram\Instagram(); try { //Set the Proxy and Port $instagram->setProxy("127.0.0.1:8888"); //Your Proxy might need Username and Password //$instagram->setProxy("127.0.0.1:8080", "username", "password"); //Enable/Disable SSL Verification (Testing with Charles Proxy etc) $instagram->setVerifyPeer(false); //Login $instagram->login("username", "password"); //todo: Do something! :) } catch (Exception $e) { //Something went wrong... echo $e->getMessage() . "\n"; }
<?php $instagram = new Instagram\Instagram(); $instagram->setAccessToken($_SESSION['instagram_access_token']); $media = $instagram->getPopularMedia(isset($_GET['max_id']) ? array('max_id' => $_GET['max_id']) : null); require 'views/_header.php'; require 'views/popular_media.php'; require 'views/_footer.php';
<?php $instagram = new Instagram\Instagram(); $instagram->setAccessToken($_SESSION['instagram_access_token']); if (isset($_GET['lat'], $_GET['lng'])) { $search = true; $lat = $_GET['lat']; $lng = $_GET['lng']; $params = array('distance' => 100, 'max_timestamp' => isset($_GET['max_timestamp']) ? $_GET['max_timestamp'] : null); $media = $instagram->searchMedia($lat, $lng, $params); } require 'views/_header.php'; require 'views/near.php'; require 'views/_footer.php';
<?php require '../vendor/autoload.php'; try { $instagram = new \Instagram\Instagram(); $instagram->login('username', 'password'); var_dump($instagram->getCurrentUser()); } catch (\Exception $e) { echo $e->getMessage(); }
<?php $instagram = new Instagram\Instagram(); $instagram->setAccessToken($_SESSION['instagram_access_token']); $tag = $instagram->getTag(isset($_GET['tag']) ? $_GET['tag'] : 'mariokart'); $media = $tag->getMedia(isset($_GET['max_tag_id']) ? array('max_tag_id' => $_GET['max_tag_id']) : null); require 'views/_header.php'; require 'views/tag.php'; require 'views/_footer.php';
<?php $instagram = new Instagram\Instagram(); $instagram->setAccessToken($_SESSION['instagram_access_token']); $current_user = $instagram->getCurrentUser(); $location = $instagram->getLocation(isset($_GET['location']) ? $_GET['location'] : 4774498); $media = $location->getMedia(isset($_GET['max_id']) ? array('max_id' => $_GET['max_id']) : null); require 'views/_header.php'; require 'views/location.php'; require 'views/_footer.php';
public function get_hashtag($tag = null, $user_id = null, $count = null) { if (!$tag || !$user_id) { return false; } else { if (!$count || !is_integer($count)) { $count = (int) $this->settings['number_of_photos']; } $token = get_user_meta($user_id, $this->fix_name('authorized'), true); if ($token) { $return_array = get_transient($this->fix_name('images_' . $tag . '_' . $count)); if (!$return_array) { $instagram = new Instagram\Instagram(); $instagram->setAccessToken($token); $hashtag = $instagram->getTag($tag); $images = $hashtag->getMedia(array('count' => $count)); $return_array = $this->convert_array($images); if (count($return_array)) { $short_term = (int) $this->settings['short_term_cache']; delete_transient($this->fix_name('images_' . $tag . '_' . $count)); delete_transient($this->fix_name('images_' . $tag . '_' . $count . '_long')); set_transient($this->fix_name('images_' . $tag . '_' . $count), $return_array, $short_term * 60 * 60); set_transient($this->fix_name('images_' . $tag . '_' . $count . '_long'), $return_array, 84600); } else { $return_array = get_transient($this->fix_name('images_' . $tag . '_' . $count . '_long')); if (!$return_array) { $return_array = false; } } } return $return_array; } else { return false; } } }
/** * Process actions based on $_GET parameters. Authorize Instagram user. * @param arr $options instagram plugin options * @param instagram $instagramAuth instagram object */ protected function processPageActions($options, Instagram\Auth $instagramAuth) { //authorize user if (isset($_GET["code"])) { $access_token = $instagramAuth->getAccessToken($_GET["code"]); if (isset($access_token)) { $instagram = new Instagram\Instagram($access_token); $instagram_user_profile = $instagram->getCurrentUser()->getData(); $instagram_username = $instagram_user_profile->username; $instagram_user_id = $instagram_user_profile->id; $this->saveAccessToken($instagram_user_id, $access_token, $instagram_username); } else { $error_msg = "Problem authorizing your instagram account! Please correct your plugin settings."; $this->addErrorMessage($error_msg, 'authorization', true); } } }
<?php require "../vendor/autoload.php"; $instagram = new \Instagram\Instagram(); try { //Login $instagram->login("username", "password"); //Upload and Post Photo $result = $instagram->postPhoto("/Users/liamcottle/Desktop/photo.jpg", "Caption"); //Media from Result $media = $result->getMedia(); //Get the Images on Instagram for this Uploaded Media $images = $media->getImageVersions2()->getCandidates(); foreach ($images as $image) { echo sprintf("Image Version: %sx%s %s\n", $image->getWidth(), $image->getHeight(), $image->getUrl()); } } catch (Exception $e) { //Something went wrong... echo $e->getMessage() . "\n"; }
public function testGetTagMedia() { $instagram = new Instagram\Instagram($this->access_token); $media = $instagram->getTagMedia($this->valid_tag); $this->assertInstanceOf('\\Instagram\\Collection\\TagMediaCollection', $media); $this->assertTrue((bool) strlen($media->getNextMaxTagId())); }
<?php $instagram = new Instagram\Instagram(); $instagram->setAccessToken($_SESSION['instagram_access_token']); $view = 'views/search.php'; $valid_searches = array('tags', 'users', 'media', 'locations'); if (isset($_GET['search_type']) && in_array($_GET['search_type'], $valid_searches)) { switch ($_GET['search_type']) { case 'users': $users = $instagram->searchUsers($_GET['user']); $results_view = 'search_users.php'; break; case 'tags': $tags = $instagram->searchTags($_GET['tag']); $results_view = 'search_tags.php'; break; case 'locations': $locations = $instagram->searchLocations($_GET['lat'], $_GET['lng']); $results_view = 'search_locations.php'; break; case 'media': $params['distance'] = isset($_GET['distance']) ? (int) $_GET['distance'] : 1000; $params['max_timestamp'] = isset($_GET['max_timestamp']) ? $_GET['max_timestamp'] : null; $media = $instagram->searchMedia($_GET['lat'], $_GET['lng'], $params); $results_view = 'search_media.php'; break; } } require 'views/_header.php'; require 'views/search.php'; require 'views/_footer.php';
<?php function return_json($mediaList = array(), $max_id = null, $error = null) { return json_encode(array('media' => $mediaList, 'error' => $error, 'max_id' => $max_id)); } $error = null; $max_id = !empty($_REQUEST['max_id']) ? $_REQUEST['max_id'] : null; $mediaList = array(); // Return empty JSON object if an access token session isnt present if (!isset($_SESSION['instagram_access_token'])) { exit(return_json(array(), null, 'You are not authenticated in Instagram!')); } $instagram = new Instagram\Instagram(); $instagram->setAccessToken($_SESSION['instagram_access_token']); $current_user = $instagram->getCurrentUser(); try { if (DEBUG) { $username = !empty($_REQUEST['user']) ? $_REQUEST['user'] : $current_user->getUserName(); } else { $username = $current_user->getUserName(); } $user = $instagram->getUserByUsername($username); $userId = $user->getId(); try { $params = array('max_id' => $max_id); $media = $user->getMedia($params); foreach ($media as $photo) { $location = array('id' => null, 'lat' => null, 'lon' => null); if ($photo->hasLocation()) { // Location ID (Instagram Proprietary - look like a FourSquare V1 venueId)
<?php require "../vendor/autoload.php"; $instagram = new \Instagram\Instagram(); try { //Login $instagram->login("username", "password"); //Find User by Username $user = $instagram->getUserByUsername("liamcarncottle"); //Get Feed of User by Id $userFeed = $instagram->getUserFeed($user); //Iterate over Items in Feed foreach ($userFeed->getItems() as $feedItem) { //User Object, (who posted this) $user = $feedItem->getUser(); //Caption Object $caption = $feedItem->getCaption(); //How many Likes? $likeCount = $feedItem->getLikeCount(); //How many Comments? $commentCount = $feedItem->getCommentCount(); //Get the Comments $comments = $feedItem->getComments(); //Which Filter did they use? $filterType = $feedItem->getFilterType(); //Grab a list of Images for this Post (different sizes) $images = $feedItem->getImageVersions2()->getCandidates(); //Grab the URL of the first Photo in the list of Images for this Post $photoUrl = $images[0]->getUrl(); //todo: Do something with it :) //Output the Photo URL