Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Stylize Your Digg Count

By Leon Chevalier
2008-04-22


A class to query the API

There is a PEAR package available, but for the purpose of this tutorial I’m going to show to how to create a class yourself. We’ll keep everything as basic as possible, and make the code PHP4 compatible.

The first thing we’re going to need is a way to download the data from the API urls that we’ll be accessing. A great class you can use to do this is Snoopy. If you have Wordpress, the class is included in the wp-includes directory. If not, it’s a small download. So, the first thing we need to do is include the Snoopy class, and create a new Snoopy object.Make sure you include the correct path when you include Snoopy.

require(”class-snoopy.php”);
$snoopy = new Snoopy();

Ok, now let’s see how we’ll be getting the data from Digg. All we need to do is instantiate the class we’ll be making, and include some options as a parameter. Like this:

$proxy = new digg_proxy(array(’snoopy’=>$snoopy,
‘func’=>’return_php_object’,
‘url’=>’http://stylizedweb.com’,
‘type’=>’php’
)
);

The first option we already know about, it’s snoopy. The second (func) is the function inside the class we want to run. Url is the URL we want to query Digg about, and type is the format we want Digg to return the data in.

OK, so let’s see the full code:

require(”class-snoopy.php”);
$snoopy = new Snoopy();

$proxy = new digg_proxy(array(’snoopy’=>$snoopy,
‘func’=>’return_php_object’,
‘url’=>’http://stylizedweb.com’,
‘type’=>’php’
)
);

/**
* Connects to DIGG API and returns data
**/
class digg_proxy {

/**
* Constructor, sets up default vars
**/
function digg_proxy($array=null) {

if(is_array($array)) {
foreach($array AS $key=>$value) {
$this->$key = $value;
}
}

//Default vars. $_GET overrides defaults
$input = array(’url’=>”,’ids’=>”,’type’=>’php’,'func’=>false);
foreach($input AS $name=>$default) {
if(empty($this->$name)) {
if(!empty($_GET[$name])) {
$this->$name = $_GET[$name];
} else {
$this->$name = $default;
}
}
}

$func = $this->func;
if(method_exists($this,$func)) {
$this->$func();
}

}

/**
* Gets DIGG info on a story URL
**/
function get_story_info() {

if(empty($this->url)) {
return false;
}

$this->url = ‘http://services.digg.com/stories/?link=’ . urlencode($this->url) . ‘&appkey=http%3A%2F%2Fexample.com%2Fapplication&type=’.$this->type;
$this->snoopy->fetch($this->url);
return $this->snoopy->results;

}

/**
* Gets DIGG info on multiple story IDs
**/
function get_multiple_stories_info() {

$ids = $this->ids;
if(empty($this->ids)) {
return false;
}

$this->url = ‘http://services.digg.com/stories/’ . $ids . ‘/’ . ‘?appkey=http%3A%2F%2Fexample.com%2Fapplication&type=’.$this->type;
$this->snoopy->fetch($this->url);
return $this->snoopy->results;

}

/**
* Echos json for use with Ajax
**/
function echo_story_json() {

$this->type = “json”;
echo $this->get_story_info();

}

/**
* Returns PHP object
**/
function return_php_object() {

$this->type = “php”;
$this->data = unserialize($this->get_story_info());

}

}

I’ve tried to keep it as simple as possible, but there may be a few little bits you don’t understand. If you’re interested in getting and storing digg counts for your site, I suggest you download the full class here and run it yourself. The data will come back as an object, ready for use in PHP. Also included in the download is an example of how to retrieve the digg data on a group of URLs at the same time. For this you have to know the digg_story_id which I mentioned earlier. Once you know the digg_story_id you can get info on up to 100 urls with one query.



Tutorial Pages:
» What’s a digg count?
» The Example
» The Digg API
» A PHP Example
» A class to query the API
» An AJAX Example
» Which links get a digg count?
» Starting the digg count
» Downloads


Related Tutorials:
» Drupal CMS e-Commerce Module Basics
» Creating an Online Newsletter with Drupal
» Setting Up Subversion for Development on Windows
» Installing Apache on Windows
» Ecommerce Imagery: Persuading with Pictures
» Customizable Websites - The Definitive Guide