Extract images with a certain tag from Instagram

MultiformeIngegno

  • Talking Head
  • I'm pretty sure someone already mentioned this, anyway: on Chrome when I mouseover on thoughts (in home page), the div starts flickering.. it's strange
  • Posts: 1,124
Extract images with a certain tag from Instagram
« on April 18th, 2012, 08:59 PM »
Hi guys! :)
I'm trying to display a number of recent photos from Instagram that have a certain tag (for example: "snow").

I have never done this before (and I have PHP and JSON knowledge equal to 0,1.. :P ) + saying that Instagram API's documentation is poor is a gift!!

Here's the code I'm trying..

Code: [Select]
<?php 
$json 
file_get_contents('https://api.instagram.com/v1/tags/snow/media/recent?client_id=XXX'); 
$decode json_decode($jsontrue); 
var_dump($decode); 
?>

<?php 
    
foreach ($response['data'] as $data) { 
        
$thumbnail $data['images']['low_resolution']['url']; 
        
$author $data['caption']['from']['username']; 
        
$date $data['created_time']; 
        
$text $data['caption']['text']; 
        
$link $data['link']; }
    
?>

    <div>
        <a href="<?php $link ?>"><span></span><img src="<?php $thumbnail ?>" title="<?php $text ?>" alt="<?php $text ?>" /></a>

        <div>via <?php $author ?> at <?php echo date("h:i:s A \o\\n d/m/Y",$date); ?></div>
        <?php $text ?>
</div>

Ehm.. (of course?) it doesn't work.. do you have an idea of what I need to change?

Thankssss ;)

EDIT: XXX is the client ID I have.

Arantor

  • With a Box
  • As long as the planets are turning, as long as the stars are burning, as long as your dreams are coming true...
  • Posts: 13,627
Re: Extract images with a certain tag from Instagram
« Reply #1 on April 18th, 2012, 09:03 PM »
First of all, what do you mean by 'it doesn't work'. Secondly, where is their API documentation, what's the structure of the API that this is supposed to interact with?
  When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest.


MultiformeIngegno

  • Talking Head
  • I'm pretty sure someone already mentioned this, anyway: on Chrome when I mouseover on thoughts (in home page), the div starts flickering.. it's strange
  • Posts: 1,124
Re: Extract images with a certain tag from Instagram
« Reply #2 on April 18th, 2012, 09:48 PM »
This is a piece of the very long code I get if I visit the file_get_contents URL (after adding the Client ID/token):
Code: [Select]
{"pagination":{"next_max_tag_id":"1334803018782","deprecation_warning":"next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead","next_max_id":"1334803018782","next_min_id":"1334803397349","min_tag_id":"1334803397349","next_url":"https:\/\/api.instagram.com\/v1\/tags\/snow\/media\/recent?client_id=3d61d317425440a1aee6c9b795b0fe92&max_tag_id=1334803018782"},"meta":{"code":200},"data":[{"tags":["horses","centreparcs","snow","santa","winter"],"location":{"latitude":51.1865,"longitude":-2.2355},"comments":{"count":1,"data":[{"created_time":"1334778197","text":"#winter #centreparcs #horses #santa #snow","from":{"username":"mattpeno","profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_22685403_75sq_1334421562.jpg","id":"22685403","full_name":"Matt Penistone"},"id":"172454476287201505"}]},"filter":"Lord Kelvin","created_time":"1334778119","link":"http:\/\/instagr.am\/p\/JkrdZ7BlKe\/","likes":{"count":0,"data":[]},"images":{"low_resolution":{"url":"http:\/\/distilleryimage6.instagram.com\/90185cf0898e11e1a87612313804ec91_6.jpg","width":306,"height":306},"thumbnail":{"url":"http:\/\/distilleryimage6.instagram.com\/90185cf0898e11e1a87612313804ec91_5.jpg","width":150,"height":150},"standard_resolution":{"url":"http:\/\/distilleryimage6.instagram.com\/90185cf0898e11e1a87612313804ec91_7.jpg","width":612,"height":612}},"caption":{"created_time":"1334778134","text":"Snow horse","from":{"username":"mattpeno","profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_22685403_75sq_1334421562.jpg","id":"22685403","full_name":"Matt Penistone"},"id":"172453947268026590"},"type":"image","id":"172453822445539998_22685403","user":{"username":"mattpeno","website":"","bio":"\ud83d\udc7ffun loving\ud83d\udc9a\r\n\ud83d\ude32punk rocking\ud83d\udc7d\r\n\ud83d\udc38spaceship flying\ud83d\ude80\r\n\ud83d\ude1ccider drinking\ud83c\udf7a\r\n27 year old something..","profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_22685403_75sq_1334421562.jpg","full_name":"Matt Penistone","id":"22685403"}},{"tags":["switzerland","snow","winter","forest"],"location":null,"comments":{"count":0,"data":[]},"filter":"Amaro","created_time":"1334775900","link":"http:\/\/instagr.am\/p\/JknOf2u8TC\/","likes":{"count":1,"data":[{"username":"martinboygaard","profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_22343546_75sq_1333840303.jpg","id":"22343546","full_name":"Martin B\u00f8ygaard"}]},"images":{"low_resolution":{"url":"http:\/\/distilleryimage10.instagram.com\/654e02c2898911e1af7612313813f8e8_6.jpg","width":306,"height":306},"thumbnail":{"url":"http:\/\/distilleryimage10.instagram.com\/654e02c2898911e1af7612313813f8e8_5.jpg","width":150,"height":150},"standard_resolution":{"url":"http:\/\/distilleryimage10.instagram.com\/654e02c2898911e1af7612313813f8e8_7.jpg","width":612,"height":612}},"caption":{"created_time":"1334778184","text":"#forest #snow #winter #switzerland","from":{"username":"sharkypowaaa","profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_37503824_75sq_1334745578.jpg","id":"37503824","full_name":"sharkypowaaa"},"id":"172454365510747828"},"type":"image","id":"172435205837800642_37503824","user":{"username":"sharkypowaaa","website":"","bio":"","profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_37503824_75sq_1334745578.jpg","full_name":"sharkypowaaa","id":"37503824"}},{"tags":["snow"],"location":null,"comments":{"count":1,"data":[{"created_time":"1334778181","text":"#snow","from":{"username":"mosodal","profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_32258055_75sq_1333988855.jpg","id":"32258055","full_name":"Monika"},"id":"172454342583894661"}]},"filter":"Valencia","created_time":"1334778146","link":"http:\/\/instagr.am\/p\/JkrgqML8BM\/","likes":{"count":0,"data":[]},"images":{"low_resolution":{"url":"http:\/\/distilleryimage0.instagram.com\/9ffbeb50898e11e19e4a12313813ffc0_6.jpg","width":306,"height":306},"thumbnail":{"url":"http:\/\/distilleryimage0.instagram.com\/9ffbeb50898e11e19e4a12313813ffc0_5.jpg","width":150,"height":150},"standard_resolution":{"url":"http:\/\/distilleryimage0.instagram.com\/9ffbeb50898e11e19e4a12313813ffc0_7.jpg","width":612,"height":612}},"caption":{"created_time":"1334778159","text":"Ja, jeg hadde s\u00e5 lyst p\u00e5 sn\u00f8 n\u00e5.","from":{"username":"mosodal","profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_32258055_75sq_1333988855.jpg","id":"32258055","full_name":"Monika"},"id":"172454157078217347"},"type":"image","id":"172454046071767116_32258055","user":{"username":"mosodal","website":"","bio":"jente, 17 \u00e5r, NORGE :-) ","profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_32258055_75sq_1333988855.jpg","full_name":"Monika","id":"32258055"}},{"tags":["bradford","igerseurope","igersportugal","snow","statigram"],"location":null,"comments":{"count":0,"data":[]},"filter":"Lord Kelvin","created_time":"1334778177","link":"http:\/\/instagr.am\/p\/JkrkgxvQZ8\/","likes":{"count":0,"data":[]},"images":{"low_resolution":{"url":"http:\/\/distilleryimage8.instagram.com\/b2cc04ea898e11e1989612313815112c_6.jpg","width":306,"height":306},"thumbnail":{"url":"http:\/\/distilleryimage8.instagram.com\/b2cc04ea898e11e1989612313815112c_5.jpg","width":150,"height":150},"standard_resolution":{"url":"http:\/\/distilleryimage8.instagram.com\/b2cc04ea898e11e1989612313815112c_7.jpg","width":612,"height":612}},"caption":{"created_time":"1334778178","text":"#igersportugal #snow #igerseurope #bradford #statigram","from":

I said it doesn't work because with the code I mentioned in the previous post I get this:
multiformeingegno.it/wp-content/themes/multiformeingegno/test.php

Arantor

  • With a Box
  • As long as the planets are turning, as long as the stars are burning, as long as your dreams are coming true...
  • Posts: 13,627
Re: Extract images with a certain tag from Instagram
« Reply #3 on April 18th, 2012, 09:51 PM »
If you visit the file_get_contents URL, you should expect to get some JSON. The code you get is some JSON.

So, again, what is it supposed to do that it is not doing? Or what is it doing that it is not supposed to?

Showing me a bunch of code, showing me another bunch of code, I have no idea what you're supposed to be getting, but hey, I'm sure we'll figure it out when you tell me these things...
  When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest.


MultiformeIngegno

  • Talking Head
  • I'm pretty sure someone already mentioned this, anyway: on Chrome when I mouseover on thoughts (in home page), the div starts flickering.. it's strange
  • Posts: 1,124
Re: Extract images with a certain tag from Instagram
« Reply #4 on April 18th, 2012, 10:06 PM »
Quote from Arantor on April 18th, 2012, 09:51 PM
Showing me a bunch of code, showing me another bunch of code, I have no idea what you're supposed to be getting, but hey, I'm sure we'll figure it out when you tell me these things...
Ok, let's try. :P
I want to extract data from the JSON file (the one in file_get_contents). It "outputs" new stuff every time that users post images with the "snow" tag (in this case), always with the same structure, for example:
{"username":"XXX","profile_picture":"XXX","id":"XXX","full_name":"XXX"},"id":"XXX"}]},,"images":{"low_resolution":{"url":"http:XXX","width":XXX,"height":XXX},"thumbnail":{"url":"http:XXX","width":XXX,"height":XXX:" etc...

I need to extract some of these information. For example I may want to extract and display username and low res URL only for every entry.

P.S.: Posts preview is broken.. :whistle:

Arantor

  • With a Box
  • As long as the planets are turning, as long as the stars are burning, as long as your dreams are coming true...
  • Posts: 13,627
Re: Extract images with a certain tag from Instagram
« Reply #5 on April 18th, 2012, 10:14 PM »
Quote
I want to extract data from the JSON file (the one in file_get_contents). It "outputs" new stuff every time that users post images with the "snow" tag (in this case), always with the same structure, for example:
Without being funny, am I not making myself clear? I know what you're doing. I know what you're trying to do. You want me to help you with your code. The code is not acting as it should. What it is doing that is different to what you want?

Right now, we're at the equivalent of you going to the doctor, and explaining your life history and goals. You haven't yet described your problem or its symptoms, merely that you're ill.
Quote
P.S.: Posts preview is broken..
Define broken.
  When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest.


billy2

  • We-Gen
  • Trying to earn brownie points for a lads trip to the Red Sea. Minus 1 already - just for asking!!
  • Posts: 348
Re: Extract images with a certain tag from Instagram
« Reply #6 on April 18th, 2012, 10:17 PM »
Agree, post preview doesn't display
<br /><br />cough, cough.

MultiformeIngegno

  • Talking Head
  • I'm pretty sure someone already mentioned this, anyway: on Chrome when I mouseover on thoughts (in home page), the div starts flickering.. it's strange
  • Posts: 1,124
Re: Extract images with a certain tag from Instagram
« Reply #7 on April 18th, 2012, 10:51 PM » Last edited on April 19th, 2012, 12:36 AM
Quote from Arantor on April 18th, 2012, 10:14 PM
The code is not acting as it should. What it is doing that is different to what you want?
Well, this is the code (simplifed):
Code: [Select]
<?php 
$json 
file_get_contents('https://api.instagram.com/v1/tags/snow/media/recent?client_id=XXX'); 
$decode json_decode($jsontrue); 
var_dump($decode); 
?>

<?php 
    
foreach ($decode['data'] as $data) { 
        
$thumbnail $data['images']['low_resolution']['url']; 
}
    
?>

    <div>
        <img src="<?php $thumbnail ?>" alt="" />
</div>

It outputs ALL the JSON file (because of the file_get_contents). I want just the $thumbnail variable for each entry. :)
There's something wrong with the foreach(). At the end of the file I get: Warning: Invalid argument supplied for foreach()

MultiformeIngegno

  • Talking Head
  • I'm pretty sure someone already mentioned this, anyway: on Chrome when I mouseover on thoughts (in home page), the div starts flickering.. it's strange
  • Posts: 1,124
Re: Extract images with a certain tag from Instagram
« Reply #8 on April 19th, 2012, 12:55 AM »
Uhm.. look at this other piece of code:
Code: [Select]
<?php
$instagramClientID 
'9110e8c268384cb79901a96e3a16f588';
$api 'https://api.instagram.com/v1/tags/snow/media/recent?client_id='.$instagramClientID//api request (edit this to reflect tags)
$response file_get_contents($api,0,null,null);

$instagrams json_decode($response)->data;

$features = array();
foreach ( 
$instagrams as $instagram ) {
    
$features[] = array(
        
'properties' => array(
            
'image' => $instagram->images->standard_resolution->url,
            
'description' => $instagram->caption $instagram->caption->text null,
        )
    );
}

$results = array(
    
'features' => $features,
);

print_r($results);

?>


Instead of print_r($results); I'd like to have the ability to output something like:
<img src="<?php $image ?>" alt="<?php $description ?>" />

The problem is how can I specify which parameter of which entry needs to be shown..?
The result should be something like <img src="IMAGE 1" alt="DESCRIPTION1" /><img src="IMAGE 2" alt="DESCRIPTION2" /><img src="IMAGE 3" alt="DESCRIPTION3" />

MultiformeIngegno

  • Talking Head
  • I'm pretty sure someone already mentioned this, anyway: on Chrome when I mouseover on thoughts (in home page), the div starts flickering.. it's strange
  • Posts: 1,124
Re: Extract images with a certain tag from Instagram
« Reply #9 on April 19th, 2012, 03:18 AM »
I studied JSON a bit and managed to create a "twitter widget" (okay I copied the part for($i=0;$i<$count;$i++), the rest is mine tough :P):
Code: [Select]
<?php
$json file_get_contents("http://twitter.com/status/user_timeline/lorenzoraffio.json?count=10"true);
$decode json_decode($jsontrue);
echo "
<ul style='color:#6E6E6E'>"
;
$count count($decode); //counting the number of status
$date $tweets['created_at']; // created date
for($i=0;$i<$count;$i++){ echo '
<li>'
.$decode[$i]['text'].'<br><a href='; echo '"http://twitter.com/#!/'.$decode[0][user][name].'/status/'.$decode[$i][id].'"><small>'date('j F',strtotime($decode[$i]['created_at'])) ."</small></a> - <a href='http://twitter.com/intent/tweet?in_reply_to=".$decode[$i][id]."'><small>rispondi</small></a> - <a href='http://twitter.com/intent/retweet?tweet_id=".$decode[$i][id]."'><small>retweet</small></a> - <a href='http://twitter.com/intent/favorite?tweet_id=".$decode[$i][id]."'><small>preferito</small></a></li>";} echo "
</ul>
"
;
?>


And this works!! ;)

The problem is that I tried to do the same for Instagram (replacing the parameters) but seems more complicated...!

Arantor

  • With a Box
  • As long as the planets are turning, as long as the stars are burning, as long as your dreams are coming true...
  • Posts: 13,627
Re: Extract images with a certain tag from Instagram
« Reply #10 on April 19th, 2012, 01:00 PM »
Well it WILL output everything, since you have the var_dump in there...
  When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest.


MultiformeIngegno

  • Talking Head
  • I'm pretty sure someone already mentioned this, anyway: on Chrome when I mouseover on thoughts (in home page), the div starts flickering.. it's strange
  • Posts: 1,124
Re: Extract images with a certain tag from Instagram
« Reply #11 on April 19th, 2012, 04:40 PM » Last edited on April 19th, 2012, 05:50 PM
This should be better, no?
Code: [Select]
<?php
function get_instagram($user_id,$count)
{
$user_id '3';
$count '3';
$url 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token=13137.f59def8.1a759775695548999504c219ce7b2ecf&count='.$count;
$jsonData $json_decode((file_get_contents($url)));
$data $jsonData->data;
$result '<ul>';
foreach ($data as $key => $value) {
$result .= '<li><a href='.$value->link.' ><img src="'.$value->images->standard_resolution->url.'" width="70" height="70" /></a></li> ';
}
$result .= '</ul>';
echo $result;
}

Result is blank page though... :blush:

MultiformeIngegno

  • Talking Head
  • I'm pretty sure someone already mentioned this, anyway: on Chrome when I mouseover on thoughts (in home page), the div starts flickering.. it's strange
  • Posts: 1,124
Re: Extract images with a certain tag from Instagram
« Reply #12 on April 19th, 2012, 06:00 PM »
Yayyyy!! This works!!!! ;) ;)

Code: [Select]
function get_instagram($user_id=3,$count=3,$width=70,$height=70){

    $url = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token=13137.f59def8.1a759775695548999504c219ce7b2ecf&count='.$count;
    $jsonData = json_decode((file_get_contents($url)));
    $result = '<ul>'.PHP_EOL;
    foreach ($jsonData->data as $key=>$value) {
        $result .= "\t".'<li><a href="'.$value->link.'"><img src="'.$value->images->standard_resolution->url.'" width="'.$width.'" height="'.$height.'" /></a></li>'.PHP_EOL;
    }
    $result .= '</ul>'.PHP_EOL;
    return $result;
}

echo get_instagram();


MultiformeIngegno

  • Talking Head
  • I'm pretty sure someone already mentioned this, anyway: on Chrome when I mouseover on thoughts (in home page), the div starts flickering.. it's strange
  • Posts: 1,124
Re: Extract images with a certain tag from Instagram
« Reply #13 on April 19th, 2012, 06:14 PM »
Added the cache:
Code: [Select]
function get_instagram($user_id=3,$count=3,$width=70,$height=70){

    $url = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token=13137.f59def8.1a759775695548999504c219ce7b2ecf&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>'.PHP_EOL;
    foreach ($jsonData->data as $key=>$value) {
        $result .= "\t".'<li><a href="'.$value->link.'"><img src="'.$value->images->standard_resolution->url.'" alt="" width="'.$width.'" height="'.$height.'" /></a></li>'.PHP_EOL;
    }
    $result .= '</ul>'.PHP_EOL;
    return $result;
}

echo get_instagram();

Nao

  • With a Box
  • If you say so.....
  • Posts: 12,914
Re: Extract images with a certain tag from Instagram
« Reply #14 on April 19th, 2012, 06:18 PM »
How come I never heard about Amstamgram before they got gobbled up by Facebook? :P
...« I say wedge wedge (in the butt) »
 « Everyone knows rock attained perfection in 1974. It's a scientific fact. » (Homer Simpson)