Charl van Niekerk » Blog

Main

Latest

Archives

Powered by Blogger

oEmbed, flickr and starstar

I have been playing with Flickr's oEmbed implementation just now and wrote the following script.

<?php
require_once 'db.php';
$rows = mysql_query('SELECT `post_content` FROM `post` WHERE `post_content` REGEXP "http://flickr.com/photos/[a-zA-Z0-9]+/[0-9]+"');
while ($row = mysql_fetch_assoc($rows)) {
  preg_match_all('#http://flickr.com/photos/[a-zA-Z0-9]+/[0-9]+#', $row['post_content'], $images);
  foreach ($images[0] as $image) {
    $data = @file_get_contents('http://flickr.com/services/oembed?url=' . urlencode($image));
    if ($data) {
      $xml = @new SimpleXMLElement($data);
      if ($xml && $xml->url) {
        $jpg = str_replace('.jpg', '_m.jpg', $xml->url);
        $alt = "$xml->title by $xml->author_name";
        echo "<a href='$image'><img src='$jpg' alt='$alt' title='$alt'></a>";
      }
    }
  }
}

This should work together with starstar. The API calls need to be cached though so will make a plan there before I commit it to subversion. Oh yes and the regular expression for the flickr usernames also probably needs checking. This is essentially WIP.

0 Comments

Post a Comment

Copyright © 2004-2009 Charl van Niekerk. All articles are released under the Creative Commons Attribution 2.5 South Africa licence, unless where otherwise stated.