Early in 2007, Digg.com introduced its API which allows developers to access all of Digg's data back to 2004. Requests are sent to the Digg servers with a simple call to http://services.digg.com and responses are sent back in an XML, JSON, JavaScript or PHP type. For this tutorial, we're going to focus on PHP responses.

I'm going to ignore the Services_Digg Pear package for now, since it has not yet been accepted as an official Pear package. That said, the best way to access the API in PHP is through the cURL library and since I've always been a fan of the wheel and I see no reason to reinvent it, I'm going to use JasLab's PHP Digg toolkit. The toolkit is class that you'll need to unzip and upload to your server. Then simply add it as an include in the PHP file you'll be working in and your ready to go. The toolkit will handle connecting to the Digg server with curl_setopt, building the request and parsing the response. It will return a nice, clean array of arrays containing all of the diggs and/or comments you've requested. Let's get started.

The first thing we're going to do is create a new instance of diggclass; the main class in the digg toolkit. Then, to get all diggs from a single user, just call the getUserDiggs function on your class instance and store the result in a new array. getUserDiggs takes several optional parameters, but the main values that you'll want to pass are $user, $count and $offset; where $count is the number of diggs to return and $offset is the starting point for which to get diggs (0 means start with the most recent digg). You can also pass start and ending time values; check out the toolkit documentation for more information.
include("diggclass.php");
$diggobj = new diggclass();
$user = "jspegele";
$diggArray = $diggobj->getUserDiggs($user,$count=10,$offset=0);
The getUserDiggs function will return an array containing individual arrays for each digg retrieved. The inidivual arrays contain the date of submission, story id, story number, username of the submitter, the popular or upcoming status. Story id (returned as id) can be used to retreive further story information from the getStories function. Simply pass the id to the function and an array of arrays will be returned with the story link, Digg story link, number of comments, status, title, description, submitter, submitter's icon, topic and diggs. Of course, since you sent a story id to the function, only a single array will be in the returned array. Here's how you would get all of the diggs data and the stories data and merge them into all of that data into a single array containing one array for each digg:
function latestDiggs($user,$limit) {
  $diggs = "";
  for($i=0; $i<$limit; $i++) {
    $tempArray = $diggArray[$i];
    $storyId = $tempArray[story];	// Story id - used to get story title,link,etc.
    $date = $tempArray[date];	// Date and time of digg
    // Get the story information based on storyId
    $storiesArray = $diggobj->getStories("",$storyId,"","","","");
    // getStories also returns an array of arrays.  There should only be 1 element returned.
    $storyArray = $storiesArray[0];
    $title = $storyArray[title];
    $digg_link = $storyArray[digg_link];
    $diggs[$i] = array("date"=>$date, "title"=>$title, "digg_link"=>$digg_link, "content"=>"");
  }
}
And here's all of the code you would need to get a single array containing all comments, with the accompanying story data from a single user:
function latestDiggComments($user,$limit) {
  // Create a new instance of diggclass()
  $diggobj = new diggclass();
  // Get the last $limit comments from $user starting with the latest
  // getUserComments will return an array of arrays.  Each sub array
  // describes a comment.
  $commentArray = $diggobj->getUserComments($user,$count=$limit,$offset=0);
  $comments = "";
  for($i=0; $i<$limit; $i++) {	
    $tempArray = $commentArray[$i];
    $storyId = $tempArray[story];	// Story id - used to get story title,link,etc.
    $date = $tempArray[date];	// Date and time of digg
    $comment = $tempArray[content];	// Text of the comment
    // Get the story information based on storyId
    $storiesArray = $diggobj->getStories("",$storyId,"","","","");
    // getStories also returns an array of arrays.  There should only be 1 element returned.
    $storyArray = $storiesArray[0];
    $title = $storyArray[title];
    $digg_link = $storyArray[digg_link];
    $comments[$i] = array("date"=>$date, "title"=>$title, "digg_link"=>$digg_link, "content"=>$comment);
  }
}
And finally we can bring it all together with a function that will call each of the above functions, merge the two arrays that are returned, order them based on a date comparison function (see the code linked below for cmp()) and prints all of the activity in the array in chronological order, starting with the most recent digg or comment.
function printDiggFeed($user,$limit) {
  $diggs = latestDiggs($user,$limit);		// Get the latest diggs
  $comments = latestDiggComments($user,$limit);	// Get the latest comments
  $diggsAndComments = array_merge($diggs,$comments);	// Combine into 1 array
  usort($diggsAndComments,"cmp");	// Sort based on the cmp() function
	
  for($i=0; $i<10; $i++) {
    $tempArray = $diggsAndComments[$i];	// Since diggsAndComments is an array of arrays...
    $date = $tempArray[date];
    $digg_link = $tempArray[digg_link];
    $title = $tempArray[title];
    $content = $tempArray[content];
    echo "

$title"; // if this is a comment... if($content != "") {echo"
"$content"";} echo "

"; } }
View all of the code to get Digg activity for a single user »

View a live demo »

And there you have the basics of utilizing the PHP Digg toolkit to access digg data and display it on your site or in your widget. As always, if you have a technique that you think is better or a tweak that improves on this code, please share it in the comments. If you have a tip, a trick or an idea for my next post, please don't hesitate to contact me.