Flickr API Basics – Pulling a Group Feed

results of Flickr api group request

results of Flickr api group request

Someone at work mentioned that the Flickr WordPress widget didn’t work with group photos. We use this group as part of our (sort of just getting started) Friday photo walks. Since I’m trying to learn stuff I figured I’d play a bit with the Flickr API and see how that worked.

After getting an API key, my first stop was at URLs. I thought that’d mean endpoints like on Instagram but it was the URL structures for photos. Useful and needed but not what I thought it’d be.

I then saw in the right hand sidebar groups and browse. That turned out to be a list of groups. I ended up finding what I wanted at groups.pools get photos. I planned to get the information in json if I could given I’d just spent some time figuring out how that worked so I went back to the main index thinking the json response format would show me how to ask for json data given the variables I’d seen as options in groups.pools.getphotos. It didn’t. This section shows the structure of the json response. Which makes sense it just wasn’t what I was looking for.

I found what I was looking for under request.formats which makes sense. This shows the URL structure. https://api.flickr.com/services/rest/?method=flickr.test.echo&name=value
is the basic URL. Method is the thing you want to do and name is something I could ignore in my particular case.

With a bit more wandering, which I’ll save you from, I came up with the following URL structure. I’ve removed my API key and replaced it with the very clever THISISYOURAPIKEY. If you put your API key in there it’ll work.

https://api.flickr.com/services/rest/?method=flickr.groups.pools.getPhotos&api_key=THISISYOURAPIKEY&group_id=2799152@N22&per_page=14&format=json

It returns the json seen below.
Screen Shot 2015-07-25 at 10.47.56 AM

After constructing something very similar to what I’d built for Instagram to parse the json in php things still didn’t work. I felt like it had something to do with the jsonFlickrApi element that leads off this json structure but that was just guessing. I ran across this Stack Overflow post that solved everything. Turns out I needed to add &nojsoncallback=1 to my URL structure to get functioning json. From there everything started working nicely and I made a little php script to pull the 14 most recent images from a particular group. You can see the difference in the json structure below.
Screen Shot 2015-07-25 at 11.04.42 AM

I’ll comment up the code below to get it nicely engraved in my head and in case any other amateurs wander upon this post. That’s all there is to it. It took about 30 minutes of playing around. It probably should have taken closer to 10 but the original json issue stymied me for a bit.

I’m still doing all this on the virtual box on my machine. I like that in lots of ways but I need to figure out a clean way to push it a live server. I am sure there is one and likely a very easy one, I just haven’t gotten around to figuring it out yet.


<?php
//this URL gets the images I want- flickr.groups.pools.getPhotos is the method, group_id needs to be looked up, per_page is how many images, format is json but don't forget the nojsoncallback piece
$url = 'https://api.flickr.com/services/rest/?method=flickr.groups.pools.getPhotos&api_key=THISISYOURAPIKEY&group_id=2799152@N22&per_page=14&format=json&nojsoncallback=1';
$json = file_get_contents($url);
$obj = json_decode($json);

			$list = array();
//this parses out the json elements so I can rearrange the pieces in the URLs below
			foreach ($obj->photos->photo as $media) {
			 	$farmid = $media->farm;
			 	$serverid = $media->server;
			 	$photoid = $media->id;
			 	$photosecret = $media->secret;
			 	$photoid = $media->id;
			 	$ownername = $media->owner;

//url structures are in detail at https://www.flickr.com/services/api/misc.urls.html			 	
			 	$pageurl = 'https://flickr.com/photos/' . $ownername . '/' . $photoid;
				$image = 'https://farm' . $farmid . '.staticflickr.com/' . $serverid . '/' . $photoid . '_' . $photosecret . '_s.jpg' ;
//spits out the HTML
			    echo '<html>
			    <head>
			    <style>			    
			    .flickrphoto {
			    display: inline-block;
			    width: 55px;
			    height: auto;
			    margin: 0px;
			    padding: 10px;
			    }
			    </style>
			    </head>
			    <body><div class="flickrphoto"><a href="' . $pageurl .'"><img src="' . $image . '"></a></div>
			    </body>
			    </html>' ;
				
				}
?>