Random Image From Select Flickr Groups via PHP

Yesterday I wrote about Cache’ing CURL requests, today I had to write a small script that uses it for a web app I’m working on. It uses serialized PHP data instead of JSON but everything’s pretty much the same.

The app I’m creating needed a dynamic background image, something professional, new, and interesting. I wanted a pool of images (so each request has a new image) but I didn’t want the pool to be too big. I also wanted the pool of images (Flickr Groups) to change on once in a while, so it wasn’t all too similar (landscapes, or all portraits, etc.).

The script below outputs a random image from a stream of 20 images from a Flickr Group (though it can be set to output just one image per $refresh). The Flickr Group is random too and comes from a predefined array of Groups I selected.

https://gist.github.com/davidsword/7c32c7d2d77928eeac9ecf629167b7fd

The above will output

https://gist.github.com/davidsword/e091b78457f812571745e711203f1fbe

Where {group_name} will change every $refresh seconds, and {image_url} will be 1 of 20 images on rotation every request and change with the $refresh group change.

To get the Groups ID’s I used idgettr.com and I got the Flickr API Key $flickrapi from Flickr API.