Adding delicious bookmarks with PHP

Social bookmarking websites appeared on the net some years ago. Before their arrival many link collections were introduced already in the early days of Internet. One of the largest link collections is Dmoz project, which is edited and maintained by dedicated people called editors. Social bookmarking takes the idea one step further. Now anyone can suggest a link - or bookmark - without first having moderated it. Own bookmark collection becomes public or in this case social. Again, anyone can see someone’s collection and enjoy the suggested link and the website behind it. No one is alone on the Internet. When someone discovers something new, it takes only minutes and the new idea is copied all over. Of course, there are great number of social bookmarking services out there and one of the most popular is perhaps Delicious social bookmarking service.

Delicious offers a handy set of calls to their API in order to add and delete bookmarks on the fly. This API is intended to programmers who want to manage their bookmark collections with their very own interface. Although the methods revealed are pretty easy to use some effort still needs to put to get the desired results. Programming Delicious API is very simple, but one should remember this is still their experimental version thus subject to change at any time. Programmers should add some extra code to catch any exceptions that might arise when calling these methods.

I have made a simple PHP script to add, update and delete a bookmark in Delicious. This code snippet uses curl library, which is excellent tool to make a conversation with the API. All calls to Delicious are requested through https, which requires authentication. Before rushing to code your own implementation, make a quick visit to Delicious website to sign up for a free account. You then get your user name and password needed in all calls. My code starts first composing the request url. The desired command with all parameters are passed in the url call as shown in code sample below, which sends an add command.

$username = 'enter your user name here'; $password = 'enter your password here'; $bookmarkUrl = '/'; $title = 'Matti's weblog'; $desc = 'Matti writes all about in Finnish'; $tags = 'blog weblog Finnish'; $date = '2008-11-25'; // base address $url = "https://$username:$"; // method to call $url .= "v1/posts/add?"; $url .= 'url=' . urlencode($bookmarkUrl); $url .= '&description=' . urlencode(utf8_encode($title)); $url .= '&extended=' . urlencode(utf8_encode($desc)); $url .= '&tags=' . urlencode(utf8_encode($tags)); $url .= '&dt=' . date('c', strtotime($date)); $url .= '&replace=yes'; $url .= '&shared=yes';

See the Delicious API documentation for a complete syntax of each method and their parameters. The code snippet above merely shows the idea on how to build a url for the call. You should notice that I have used utf8_encode() function. In case you have letters other than just ordinary characters, you should encode them somehow before adding to the string. I would also like to point out the date() function, which formats the time stamp as an ISO 8601 date as required. This format was first found in PHP version 5, so if you are still using older versions, you need to construct the format with other formatting options.

The next code snippet shows how to make a call to the API.

$s = curl_init(); curl_setopt($s, CURLOPT_URL, $url); curl_setopt($s, CURLOPT_BINARYTRANSFER, 1); curl_setopt($s, CURLOPT_RETURNTRANSFER, 1); curl_setopt($s, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($s, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($s, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($s, CURLOPT_USERPWD, "$username:$password"); $response = curl_exec($s); curl_close($s);

As you can see using curl is very straightforward and requires no complex coding. Calling other methods uses almost the same syntax. I encourage wrapping the curl part of the code in a private function. All various calls can then use the same function with different url strings as parameter.

Final Words

Adding a bookmark to Delicous social bookmarking service is an easy task with popular PHP scripting language. All you need is to have your favorite bookmark in hand, a descriptive title and some tags to describe the content. Tags are always passed separated with space. There is no option to combine multiple words in a single tag. These are although minor requirements. Delicious is an excellent service and their API makes it even more interesting, at least from the programmers point of view.

Julkaistu tiistaina 25.11.2008 klo 18:26 avainsanoilla harrastukset ja Internet.

Kolmella bussilla
Henkien portti