How to use the digg API
By Justin Silverton

This article will show you how to use the digg API using PHP. I have also written a library in PHP that maps all of the endpoints documented in the API to easily accessible functions (it is also compatible with PHP 4).
What can you do with the digg API?
- Get all popular stories submitted after January 1, 2007 sorted by promotion date
- Get the most recent 100 upcoming stories in the topic “Apple”
- Get the first 20 stories that were promoted on or after December 25, 2005
- Determine whether a story, identified by its URL, has been submitted to Digg and, if so, get details like the number of Diggs and comments it has received
- Get the details of a story, identified by its URL on Digg
- Get all stories submitted today from nytimes.com
- Get all stories submitted by Kevin Rose (or another Digger)
How it works
Requests are sent to the digg servers through URLS that are described in the API doc (here).
Here is an example:
http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com
This will return all stories. By default only 10 will be returned at a time (which can be increased to a maximum of 100). Also, you may notice an appkey=XXX argument. This is a unique identifier required by the digg servers. It is recommended to use the urlencoded refereral URL. Other return types are also available, which can make parsing easier depending on the language used.
response types:
- XML: Easily parsed in many languages on many platforms. It is particularly easy to use in Flash applications.
- JSON: May be directly eval’d in Javascript, and also can be parsed in many languages.
- Javascript: Useful as the source of a script tag, it passes JSON response to the Javascript callback function you specify.
- Serialized PHP: Easily unserialized in PHP to create objects, to which the programmer can attach custom methods. Or the programmer can directly access the response data through the public properties of the objects.
example:
http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com (will return XML by default)
http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com&type=json
http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com&type=php
http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com&type=javascript
Other basic arguments
- count
Number of events to retrieve.
Event count integer
Default: 10, Maximum: 100.
offset
Offset in complete events list.
Event offset integer.
Default: 0.
PHP code
The following is a function that will allow you to connect to the digg service and parse the results. It also will automatically generate the appid based on the current location of the script.
function connect($hostname) {
//this is an ID that is unique to your app. It is auto-generated, based on the calling URL
$appid = urlencode("http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]);
$host = $hostname."&appkey=".$appid;
echo $host;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL,$host);
curl_setopt ($ch,CURLOPT_USERAGENT,"Test library");
curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT,60);
$response = curl_exec ( $ch );
curl_close($ch);
return $response;
}
Download
The digg toolkit can be downloaded Here
1 Comment so far
Leave a reply






[…] No comments […]