Simple use of DIGG API for your blog
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
I have recently put up a list of DIGG popular stories on my sidebar. The reason for this was the advent of the new DIGG API which gives access to a whole range of data from the DIGG database.
There are already some good PHP classes to access the API but I wanted some thing quick and simple that would not put a lot of requirements on my current hosting (PHP 4). The API gives three very good ways to receive the data (XML, JSON and serialized PHP).
I quickly discovered that XML decoding under PHP 4 is a pain in the (****) when you do not have access to install further modules. So quickly moved onto JSON, but the lovely function json_decode is PHP 5 only. But I did a bit of search and found several PHP json classes without any dependencies. I had also ruled out the PHP serialised format as it tries to unserialise into classes that I do not have defined (again dependencies.)
I then added some caching as I really didnt want to upset the lovely people at DIGG with constant requests when the data was not likely to change much.
You can download the project (its only 2 files.) from the code bank area. It is not pretty code, and it’s not encapsulated in a class or anything as I did it purely for speed (of coding, and execution.)
Below are a few of the important sections.
$response = file_get_contents("http://services.digg.com/stories/container/ $category/popular?appkey=$appkey&count=$count&type=json"); // create an instance of the JSON service $json = new Services_JSON(); // Decode the JSON we requested $decoded = $json->decode($response);
The URL is built up to retrieve only the popular stories from a specific category. (technology I have shown on my sidebar.) I then use the enclosed JSON decoder to convert that into a PHP array.
echo " <ul>"; // Output the stories foreach($decoded->stories as $story) { $title = $story->title; if($title != '') { if(strlen($title) > 30) { $title = substr($title,0,29).'...'; } echo $title; } } echo "</ul>";
This iterates through the array and displays each in an unordered list. There is a lot more information stored within each of the stories but all I am displaying is the link.
And before someone comments that this could all be done via RSS, I agree, but I can easily now adapt what I have got to retrieve a whole range of different interesting lists.
Some ideas for what else to do with the DIGG API
- Extract out the users who have posted the popular stories
- Show the time difference between posting and the story becoming popular
- build up trends of the most popular posters
- Create custom tag clouds from the words supplied in the stories
Comment by Phil on 24 April 2007:
Sounds cool. I wonder if there’s anything for Perl or RoR to play with the Digg API
Comment by admin on 24 April 2007:
I have not heard as yet of a specific Perl or Ruby API but anything that can perform an HTTP request and decode XML or JSON has the ability to work with the API. The fact that there is already a lot of PHP support is purely because DIGG is written in PHP and their developers I am sure just extracted their own internal code.
Trackback by PHPDeveloper.org on 25 April 2007:
Nick Halstead’s Blog: Simple use of DIGG API for your blog…
…
Pingback by developercast.com » Nick Halstead’s Blog: Simple use of DIGG API for your blog on 25 April 2007:
[…] started playing with it to integrate it with their own applications. On developer, Nick Halstead, decided to grab the latest stories and publish the headlines to his page. In the process, though, he learned a little something about […]
Pingback by 7 Awesome Things Built on the Digg API on 29 April 2007:
[…] Stories for your Sidebar – This app simply grabs popular stories from Digg and allows you to embed them on your blog or […]
Pingback by proxieslist.net on 29 April 2007:
[…] Stories for your Sidebar – This app simply grabs popular stories from Digg and allows you to embed them on your blog or […]
Pingback by Techasaur.us Rex - Encyclopedia of Technology News | Yesterday, Today & Tomorrow’s Tech News - » 7 Awesome Things Built on the Digg API on 29 April 2007:
[…] Stories for your Sidebar – This app simply grabs popular stories from Digg and allows you to embed them on your blog or […]
Comment by Brian on 15 May 2007:
Hey, I thought this was quite awesome, so I included it in my top ten list for digg apis. Nice work.
Comment by admin on 16 May 2007:
Thanks for including me in your list Brian!
Comment by Katie on 31 July 2007:
Thank you sooooo much!
Pingback by My Hobby is Programming » Blog Archive » Simple use of the new DIGG API for your Blog (and other uses) on 28 October 2007:
[…] read more | digg story […]
Pingback by 24 popular digg images in a slideshow » SERPland on 21 December 2007:
[…] There’s a good geek describtion about “using the Digg API”, which I partly used. As an alternative, JSON in PHP is also possible. […]