It takes one of two arguments, the URL or a MD5 hash of the URL. I used the URL variant as it seemed more fault tolerant:
The JSON returned has the following format:
PHP has several libraries for both encoding and decoding JSON. A native library is included by default in PHP 5.2 and this should be used wherever possible. Unfortunately my web host uses PHP 5.1.x so I've plumped for a PHP version which works really well (source code), if a touch slower. There's a good comparison of JSON libraries for PHP here.
My Server-Side Badge
My badge is very similar to the tall version of the del.icio.us tagometer badge. Visually, the only real difference is that I've rendered the various tags with font sizes relative to their popularity, just like a normal tag cloud. This was possible because, as you'll see from the sample above, the feed returned from del.icio.us includes additional data, not currently used in the del.icio.us badge, which details the number of times each tag is used. You can view an example here. The badge is also displayed on all pages of my site.
It's made up of three files as well as some additional support libraries:
- badge.inc.php - this requests data for the current page and sets up a PHP variable containing the various data components. You can include this in the header of your page or at the position that you want the badge template to appear but in either case you'll need to ensure this proceeds badge-html.inc.php in your source.
- badge-html.inc.php - renders the data against an HTML template. It should be located in your source at the position you want the badge to appear.
- badge.css - the rules in this file can be added to your main stylesheet or included separately. Either way you'll probably need to modify font, background and border properties to ensure the badge fits with your site styling. If you're using a reset file you may also want to remove the padding and margin resetting rules.
In addition http.inc.php takes care of requesting the data via either CURL or file_get_contents, depending on which is available, and JSON-PHP is used for parsing the returned data. You can optionally include my caching class which, if present, will cache the returned data for a specified time period. If using this you'll need to enter suitable values for the following constants:
define('CACHE_PATH', '/tmp/'); // include trailing slash
define('CACHE_TIME', 3600); // cache for 1 hour by default
Finally you can specify how many tag sizes the badge should display. You'll need to modify your CSS accordingly if you change this constant:
Including The Badge
An example for including the badge in your pages is shown below:
<?php require('badge.inc.php'); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Server-Side Delicious Badge Example</title>
<link type="text/css" rel="stylesheet" href="badge.css">
<div style="width: 230px;"> <!-- sample page container, badge width constrained by container -->
<?php require('badge-html.inc.php'); ?>
You can use the links to the various files above or for convenience I've bundled all files into a ZIP file you can download here. The ZIP excludes the JSON library which you can download here.