Use PHP to get the number of items for a domain on Digg.com

Whilst working on my semi-automated SEO scan I wanted to get the number of listings for a given domain on some social bookmarking websites. Of course Digg.com is a very well-known and widely-used bookmarking website but sadly they don’t offer a nice XML or JSON API to check the number of listings. That’s why I wrote the following code to retrieve this number.

Whilst working on my semi-automated SEO scan I wanted to get the number of listings for a given domain on some social bookmarking websites. Of course Digg.com is a very well-known and widely-used bookmarking website but sadly they don’t offer a nice XML or JSON API to check the number of listings. That’s why I wrote the following code to retrieve this number.

function digg_bookmarks($domain)
{
	$html = $this->snoopy->snoopURL('http://digg.com/search?s=' . $domain);
	$regexp = '/\<h2\>(\d+)\<span\>\<strong\>total\<\/strong\>/ismU';
	if (preg_match_all($regexp, $html, $matches)) {
		return $matches[1][0];
	} else {
		return 0;
	}
}

In the code above I’ve used the great Snoopy class ( although somewhat edited ) but you could fall back on PHP’s own file_get_contents (or cURL). All you have to do is change line 3 to this.

	$html = file_get_contents('http://digg.com/search?s=' . $domain);

You can now easily retrieve the number of listings by using the following function.

$number = digg_bookmarks('http://www.example.com');
echo $number;

This will echo the number of dugg items for http://www.example.com. If no items have been found this function will return 0. As long as Digg doesn’t offer an API for this the code mentioned should work just fine, unless they dramatically change their lay-out.

Author: Luc De Brouwer

Mild-mannered software engineer, Nine Inch Nails lover, gin drinker, cat person, and somewhat of a geek. Ron Swanson is my spirit animal.