Web APIs by Example, Part II: del.icio.us

Today we’ll be looking at applying what we’ve already learned from working with the Twitter API to the social bookmarking site del.icio.us. As you might expect, the del.icio.us API offers a much broader set of functionality for interacting with it’s data. Besides being able to add bookmarks, you can delete existing ones and use a number of different methods to selectively filter your bookmarks and return just the ones you’re looking for. It’s quite nice, and today we’ll be exposing some of these features with an app that lets us add bookmarks as well as filter our most recent ones.

$delicious_username = 'YOUR_USERNAME_HERE';
$delicious_password = 'YOUR_PASSWORD_HERE';

$errno = 0;
$errstr = '';
$response = '';

$links = array();

function httpRequest($host, $path = '/', $method = 'GET') {

	global $errno, $errstr, $response;
	global $delicious_username, $delicious_password;

	$port = 80;
	if(substr($host, 0, 6) == 'ssl://') $port = 443;

	$sock = @fsockopen($host, $port, $errno, $errstr, 30);
	if (!$sock) {
		if(!$errno) {
			die("

fsockopen() error:Unable to open socket for undefined reason; most likely the version of PHP you're running doesn't have SSL support compiled.");
		} else {
			die("

fsockopen() error:$errstr ($errno)

");
		}
	} else {

		$host = substr($host, 6);
		$header  = "$method $path HTTP/1.0\r\n";
		$header .= "Host: $host\r\n";
		$header .= "Accept-Encoding: none\r\n";
		$header .= "Authorization: Basic " . base64_encode("{$delicious_username}:{$delicious_password}") . "\r\n";
		$header .= "Connection: Close\r\n\r\n";

	    fwrite($sock, $header);
	    while (!feof($sock)) {
			$response .= fgets($sock, 128);
	    }
	    fclose($sock);

	    $response = str_replace("\r", '', $response);
	    $response = trim(substr($response, strpos($response, "\n\n")));
	    return true;
	}

}

if(isset($_POST['url']) && isset($_POST['description'])) {

	// Bookmark a URL

	$url = urlencode(trim($_POST['url']));
	$description = urlencode(trim($_POST['description']));
	httpRequest('ssl://api.del.icio.us', '/v1/posts/add?url=' . $url . '&description=' . $description);

}

// Fetch our recent bookmarks:

$count = 10;
$tag = '';

if(isset($_GET['count']) && strlen($_GET['count']) && is_numeric($_GET['count'])) $count = trim($_GET['count']);
if(isset($_GET['tag']) && strlen($_GET['tag'])) $tag = urlencode(trim($_GET['tag']));

$ret = httpRequest('ssl://api.del.icio.us', '/v1/posts/recent?count=' . $count . '&tag=' . $tag);

if($ret) {
	$xml_parser = xml_parser_create();
	xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
	xml_set_element_handler($xml_parser, 'parsePostOpen', 'parsePostClose');
	xml_parse($xml_parser, $response, true);
}

function parsePostClose($parser, $name){}
function parsePostOpen($parser, $name, $attrs)
{
	if(isset($attrs['HREF'])) {
		$attrs['TIME'] = strtotime(substr($attrs['TIME'], 0, -1));
		$attrs['TAG'] = explode(' ', trim($attrs['TAG']));
		global $links;
		$links[] = $attrs;
	}
}

?>

My Recent Bookmarks

Filter Results Display the <option selected="selected">10 <option selected="selected">50 <option selected="selected">100 most recent bookmarks, tagged with <input type="text" id="tag" name="tag" value="" />  
  • <a href=""> — Tagged with <a href="?tag=">
New Bookmark

That’s it! If everything’s gone right, you’ve got yourself a pretty slick little bookmark manager for del.icio.us. Hope you enjoyed this article, and please feel free to comment with any questions or additions. I have a few more additions planned to the series, so stay tuned.

Download the Source Code

Tags: , ,

View Comments

  1. Hi there,nice piece of code. it works.i altered this code by adding :if(isset($_POST['url']) && isset($_POST['description']) && isset($_POST['tags']) && isset($_POST['extended'])) { // Bookmark a URL $url = urlencode(trim($_POST['url'])); $description = urlencode(trim($_POST['description'])); $extended = urlencode(trim($_POST['extented'])); $tags = urlencode(trim($_POST['tags'])); httpRequest('ssl://api.del.icio.us', '/v1/posts/add?url=' . $url . '&description=' . $description . '&tags=' . $tags. '&extended=' . $extended); now the form also adds 'tags' to your account, BUT : the field “extended” still remains empty.any suggestions on how to fix this?

  2. Hi there,
    nice piece of code. it works.

    i altered this code by adding :

    if(isset($_POST['url']) && isset($_POST['description']) && isset($_POST['tags']) && isset($_POST['extended'])) {

    // Bookmark a URL

    $url = urlencode(trim($_POST['url']));
    $description = urlencode(trim($_POST['description']));
    $extended = urlencode(trim($_POST['extented']));
    $tags = urlencode(trim($_POST['tags']));
    httpRequest(‘ssl://api.del.icio.us’, ‘/v1/posts/add?url=’ . $url . ‘&description=’ . $description . ‘&tags=’ . $tags. ‘&extended=’ . $extended);

    now the form also adds ‘tags’ to your account,

    BUT : the field “extended” still remains empty.
    any suggestions on how to fix this?

Leave a comment

blog comments powered by Disqus