Display your recent delicious bookmarks with PHP

Reading time: About 1 minute

This tutorial uses PHP5 to download and cache your recent bookmarks in RSS format from the Delicious API, then displays them in a HTML unordered list.

See this in action on my “Recommended” page under “Recently bookmarked links.”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function get_delicious()
{
	$cache = dirname(__FILE__) . '/caches/delicious';
	if(filemtime($cache) < (time() - 300))
	{
		@mkdir(dirname(__FILE__) . '/caches', 0777);
		$url = 'https://api.del.icio.us/v1/posts/recent?count=10';
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
		curl_setopt($ch, CURLOPT_TIMEOUT, 5);
		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		// add delicious.com username and password below
		curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
		$data = curl_exec($ch);
		curl_close($ch);
		$cachefile = fopen($cache, 'wb');
		fwrite($cachefile, $data);
		fclose($cachefile);
	}
	else
	{
		$data = file_get_contents($cache);
	}
	$xml = simplexml_load_string($data);
 
	$html = '<ul>';
	foreach($xml as $item)
	{
		$html .= '<li><a href="' . $item['href'] . '">' . $item['description'] . '</a> ' . $item['extended'] . '</li>';
	}
	$html .= '<li><a href="http://delicious.com/briancray">More of Brian Cray\'s delicious bookmarks&hellip;</a></li>';
	$html .= '</ul>';
	echo $html;
}
 
// display them
get_delicious();

This tutorial uses SimpleXML. SimpleXML has been included in PHP5 to make reading XML files into PHP variables much, well, simpler.

9 comments skip to comment form

  1. brain pray said— 3 hours later

    wow… i saw your blog get torn up the other week on hacker news… and now you post this without any explanation of the content. nice

    #1
  2. brain pray said— 3 hours later

    wait i just realized you are in columbus ohio, sweet me too

    #2
  3. Bruno Correia said— 1 day later

    Awesome, thanks.
    One possible correction that you may have overlooked:

    line 4 should be:
    if(filemtime($cache) < (time() – 300))

    #3
  4. Brian Cray said— 1 day later

    @Bruno: Doh! Great eye. I corrected that in my code and forgot to correct the post ;p

    #4
  5. Bruno Correia said— 1 day later

    Brian,
    Right on, I would suppress the mkdir warning using @mkdir (this would help users who have no experience customizing their php.ini file).

    Just a suggestion – and thanks again for the tutorial.

    #5
  6. Brian Cray said— 1 day later

    Right on.

    #6
  7. Guarp said— 2 months later

    cant we display recent bookbarks with using yahoo acount?

    #7
  8. Kai Chan Vong said— 4 months later

    This was awesome, thanks. Just two questions…

    It works perfectly on my hosting, but not my localhost using latest version of MAMP… do you have any suggestions for how I could either blind guess the problem right on my local or extra lines that’ll echo back what the problem could be.

    Also, how secure is this? One of my friends mentioned to add some extra lines to encript the data and store the password somewhere else. Any other thoughts?

    Thanks!

    #8
  9. juan villegas said— 9 months later

    To use yahoo account you should simply modify v1 to v2 in the url used. More information on this in delicious api site

    #9
  10. Respond to this post—

Return to navigation
1102