PhpDelicious: Wrapper for del.icio.us API

Here’s a cool little php class for managing a delicious feed; PhpDelicious. According to the official site:

PhpDelicious is a PHP 5 library for accessing the del.icio.us API. It combines data from the main REST and JSON APIs and presents a consolidated interface. It also implements a file based caching system which eliminates the need to query on every request and ensures access to the API won’t be throttled due to excessive requests.

delicous

delicous

The class is really well written for php5 only but it could be rewritten in php4 with a bit of work and third-party modules. phpDelicious does have some requirements though:

  • PHP 5
  • CURL (or can use normal file reading functions if suitable URL wrappers installed)
  • json_decode function for JSON API based methods (native in PHP 5.2 and above)
  • XML Parser Functions

phpDelicious started in 2006 and has quite a few updates up to July of 2008 (I don’t know if it’s dying or if there’s nothing left to do…).

The class includes an example script that shows how to pull everything and add a single item that should get anyone up and running ASAP.

Usage Examples

Here are a couple examples to get you started:

Instantiate Session

<?php
require('php-delicious.inc.php');
define('DELICIOUS_USER', 'YOUR_USER');
define('DELICIOUS_PASS', 'YOUR_PASS');
$oDelicious = new PhpDelicious(DELICIOUS_USER, DELICIOUS_PASS);
?>

The above needs to be done before the below will work…

Add an Item to Bookmarks

<?php
$aPost = array();
$aPost['url'] = 'http://www.yahoo.com';
$aPost['description'] = 'Yahoo! home page';
$aPost['notes'] = 'Lame search engine'; 
$aPost['updated'] = date('Y-m-d H:i:s'); //mysql timestamp
$aTags = array('lame','dumb','search engine'); //must be array
$oDelicious->AddPost($aPost['url'], $aPost['description'], $aPost['notes'], $aTags, $aPost['updated'], true);
?>

Delete a Bookmark

<?php
$sUrl = 'http://www.yahoo.com';
$oDelicious->DeletePost($sUrl);
?>

Grab All Bookmarks

<?php
if ($aPosts = $oDelicious->GetAllPosts()) {
    foreach ($aPosts as $aPost) {
        echo '<a href="'.$aPost['url'].'">'.$aPost['desc'].'</a>';
        echo $aPost['notes'];
        echo $aPost['updated'];
    }
} else {
    echo $oDelicious->LastErrorString();
}
?>

Grab Some Bookmarks

<?php
$sTag = '', // filter by tag
$sDate = '', // filter by date - format YYYY-MM-DD HH:MM:SS
$sUrl = '' // filter by URL
if ($aPosts = $oDelicious->GetPosts($sTag,$sDate,$sUrl)) {
    foreach ($aPosts as $aPost) {
        echo '<a href="'.$aPost['url'].'">'.$aPost['desc'].'</a>';
        echo $aPost['notes'];
        echo $aPost['updated'];
    }
} else {
    echo $oDelicious->LastErrorString();
}
?>

Grab Recent Bookmarks

<?php
$sTag = '', // filter by tag
$iCount = 15 // number of posts to retrieve, min 15, max 100
if ($aPosts = $oDelicious->GetRecentPosts($sTag,$iCount)) {
    foreach ($aPosts as $aPost) {
        echo '<a href="'.$aPost['url'].'">'.$aPost['desc'].'</a>';
        echo $aPost['notes'];
        echo $aPost['updated'];
    }
} else {
    echo $oDelicious->LastErrorString();
}
?>

Grab All Tags

<?php
if ($aTags = $oDelicious->GetAllTags()) {
    foreach ($aTags as $Tag) {
        echo $Tag['tag'];
        echo $Tag['count'];
    }
} else {
    echo $oDelicious->LastErrorString();
}
?>

Rename a Tag

<?php
$sOld = 'foo';
$sNew = 'bar';
$oDelicious->RenameTag($sOld, $sNew);
?>

Grab All Dates

<?php
if ($dDates = $oDelicious->GetDates()) {
    foreach ($dDates AS $Date) {
        echo $Date['date'];
        echo $Date['count'];
    }
} else {
    echo $oDelicious->LastErrorString();
}
?>

Additional Functionality

After going through the class, it seems there’s some more functionality available. I have no idea what the purpose of the below calls are for though.

I tried looking through Delicious for references to “bundles”, “network” and “fans” but I couldn’t find anything about them though I was pretty lazy about it. Of course, I don’t really use Delicious too intensely so this could be standard, no brainer, stuff to the “real” users.

Please note though: I WAS NEVER ABLE TO GET THE BELOW TO RETURN ANYTHING BUT ERRORS. I only include them here for completeness.

Get Url Details

<?php
if ($dLinkDetails = $oDelicious->GetUrlDetails()) {
    //do something
} else {
    echo $oDelicious->LastErrorString();
}
?>

Get Network

<?php
$sUsername = 'USERNAME_TO_CHECK';
if ($dNetworkDetails = $oDelicious->GetNetwork($sUsername)) {
    //do something
} else {
    echo $oDelicious->LastErrorString();
}
?>

Get Your Network

<?php
if ($dMyNetworkDetails = $oDelicious->GetMyNetwork()) {
    //do something
} else {
    echo $oDelicious->LastErrorString();
}
?>

Get User Fans

<?php
$sUsername = 'USERNAME_TO_CHECK';
if ($dFansDetails = $oDelicious->GetFans($sUsername)) {
    //do something
} else {
    echo $oDelicious->LastErrorString();
}
?>

Get Your Fans

<?php
if ($dMyFansDetails = $oDelicious->GetMyFans()) {
    //do something
} else {
    echo $oDelicious->LastErrorString();
}
?>

Please keep in mind what I said above; I was never able to get the fans, bundle and URL details functionality to work properly.

Still, PhpDelicious makes it really easy to do minor jobs against your delicious data.

Bookmark and Share

Related Posts

5 Comments

  1. Rajat says:

    I could’nt get the GetRecentPosts function and several others to work.

    The only one that seems to work for me right now is GetMyNetwork.

    Also, I receive the following warning :

    Use of undefined constant CACHE_PATH – assumed ‘CACHE_PATH’

    Any pointers ?

  2. Eric Lamb says:

    Rajat,

    That’s weird man; did you verify the cache directory exists? According to the official site:

    By default it writes its cache files to /tmp/. If you don’t have access to this directory (perhaps because you’re using a shared hosting account) you’ll need to create a new temporary directory and update the corresponding constant in php-delicious.inc.php (line 63).

    If you want, hit me up on email later and we can try and get the setup working.
    eric@ericlamb.net

  3. Randle says:

    I also could not get anything to work, and I’m sure cache directory exists.

  4. sreeja says:

    I could’nt get the functions work….getting warning……is the yahoo username and password delicious username and password are same?

  5. Teena says:

    error showing tat incorrect username and password whn i put my yahoo username and password…..wot to do?

Leave a Reply