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;
    }

  }

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 »

7 Responses

  1. exhuma.twn Says:

    Why not calling it “Overkillr”? ;)

  2. doc.twn Says:

    because i also got a OverkillPicasa and OverkillDB class.

    i’ll publish the rest of my Overkill classes in the near future ;)

  3. CharelB Says:

    Oh nice one! Did you have more classes? show us them!

  4. abernier Says:

    Hi,

    What is this Kernel class in the constructor?

    Thank you.

  5. doc.twn Says:

    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

  6. abernier Says:

    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.

  7. doc.twn Says:

    the class is now available on github

    http://github.com/wickeddoc/OverkillFlickr

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Pages

Recent Posts

Categories

Links


Archives

Meta