OverkillFlickr: a Flickr API interface for PHP5
July 9th, 2007 by wickeddoc
I create a small Flickr API interface for PHP5 which takes advantage of the Overloading feature of PHP5.
Using the __call method we can dynamically create an interface to all the Flickr API functions using only a very small script.
The name of the class is purely ironic as it is a very simple and easy-to-use class and far from Overkill.
class OverkillFlickr {
# the API key we got from flickr
private $API = "";
static private $instance = false;
# singleton constructor
static function instance($api_key = "") {
if(!OverkillFlickr::$instance) {
OverkillFlickr::$instance = new OverkillFlickr($api_key);
}
return OverkillFlickr::$instance;
}
function __construct($api_key = "") {
$this->API = $api_key;
}
# the __call method allows us to dynamically create any
# flickr api function we want.
# takes an array of arguments
# example: to call the flickr.people.findByEmail service
# $flickr = OverkillFlickr::instance("my API key"); // API key only needed on the first call
# $result = $flickr->people_findByEmail(array("find_email" => "my.pattern@domain.com"));
function __call($method, $arguments) {
$method = str_replace("_", ".", $method);
$argument = $arguments[0];
if (empty($method) || empty($argument) || !is_array($argument)) {
return false;
}
$params = array_merge($params = array(), $argument);
$params['api_key'] = $this->API;
$params['method'] = "flickr." . $method;
$params['format'] = "php_serial";
$encoded_params = array();
foreach ($params as $k => $v){
$encoded_params[] = urlencode($k).'='.urlencode($v);
}
# call the API and decode the response
$url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);
$rsp = file_get_contents($url);
$rsp_obj = unserialize($rsp);
#
# return the response from flickr
#
return $rsp_obj;
}
}
# the API key we got from flickr
private $API = "";
static private $instance = false;
# singleton constructor
static function instance($api_key = "") {
if(!OverkillFlickr::$instance) {
OverkillFlickr::$instance = new OverkillFlickr($api_key);
}
return OverkillFlickr::$instance;
}
function __construct($api_key = "") {
$this->API = $api_key;
}
# the __call method allows us to dynamically create any
# flickr api function we want.
# takes an array of arguments
# example: to call the flickr.people.findByEmail service
# $flickr = OverkillFlickr::instance("my API key"); // API key only needed on the first call
# $result = $flickr->people_findByEmail(array("find_email" => "my.pattern@domain.com"));
function __call($method, $arguments) {
$method = str_replace("_", ".", $method);
$argument = $arguments[0];
if (empty($method) || empty($argument) || !is_array($argument)) {
return false;
}
$params = array_merge($params = array(), $argument);
$params['api_key'] = $this->API;
$params['method'] = "flickr." . $method;
$params['format'] = "php_serial";
$encoded_params = array();
foreach ($params as $k => $v){
$encoded_params[] = urlencode($k).'='.urlencode($v);
}
# call the API and decode the response
$url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);
$rsp = file_get_contents($url);
$rsp_obj = unserialize($rsp);
#
# return the response from flickr
#
return $rsp_obj;
}
}
I suggest that you add some caching of the results you get from Flickr if you don’t want to hammer their servers too much.
Posted in PHP | 7 Comments »

July 12th, 2007 at 2:01 pm
Why not calling it “Overkillr”?
July 12th, 2007 at 2:04 pm
because i also got a OverkillPicasa and OverkillDB class.
i’ll publish the rest of my Overkill classes in the near future
June 24th, 2008 at 1:17 pm
Oh nice one! Did you have more classes? show us them!
August 31st, 2009 at 10:47 pm
Hi,
What is this Kernel class in the constructor?
Thank you.
September 1st, 2009 at 8:56 am
ups, i forgot to remove that line.
initially i created this class to be used in a rather large project, and that project had a class called Kernel. you can ignore that.
EDIT: removed line
September 1st, 2009 at 10:32 am
Hi doc.twn,
Your class is really the smartest I’ve found!
I’ve enhanced/cleaned it a bit: what about creating a github repo to host and share it? Then I can commit my changes.
Cheers.
December 21st, 2009 at 7:40 pm
the class is now available on github
http://github.com/wickeddoc/OverkillFlickr