This is a companion article to my previous article "How To Get Delicious Bookmark Total Using JQuery And JSON". I recently wanted to add the total number of Diggs and Delicious bookmarks next to my "post to..." links at the bottom of my articles. While Digg does offer a free Digg counter that you can embed in your website I was unsatisfied with the styling they automatically applied to it. Really all I wanted was a text node with a number in it. The solution was to use the Digg API.
I first wanted to mimic what I did with my Delicious bookmark total retrieval but didn't have any luck with Diggs JSON API variation so I turned to server side coding to accomplish my task.
ASP File Code For Getting Total Diggs
You'll want to place the following code in a separate file called "digg.asp". You'll also need to replace the following with your own domain name "Server.URLEncode("http://www.example.com/")". This is how Digg tracks their API usage.
Set objXML = Server.CreateObject("Microsoft.XMLDOM")
objXML.Async = False
objXML.SetProperty "ServerHTTPRequest", True
objXML.ResolveExternals = false
objXML.ValidateOnParse = false
objXML.preserveWhiteSpace = false
objXML.Load("http://services.digg.com/stories/?&appkey=" & Server.URLEncode("http://www.example.com/") & "&type=xml&link=" & request.QueryString("url"))
Set objRoot = objXML.documentElement
Set objItems = objRoot.getElementsByTagName("story")
For Each objItem in objItems
temp = objItem.getAttribute("diggs")
Next
Set objRoot = Nothing
Set objItems = Nothing
response.Write(temp)
Getting The Total Number Of Diggs With JQuery
Next we use JQuery to get the response stream from our ASP file. The code is placed inside of $(document).ready(function() to ensure that we don't access our #digg SPAN until the DOM is fully loaded. I pass the URL we are checking on via a query string to our ASP file.
<script type="text/javascript">
$(document).ready(function(){
$.get("digg.asp?url=http://www.example.com/", function(data){
$("#digg").text(data);
});
});
</script>
<p>Digg: <span id="digg"></span></p>
Putting It All Together
Set objXML = Server.CreateObject("Microsoft.XMLDOM")
objXML.Async = False
objXML.SetProperty "ServerHTTPRequest", True
objXML.ResolveExternals = false
objXML.ValidateOnParse = false
objXML.preserveWhiteSpace = false
objXML.Load("http://services.digg.com/stories/?&appkey=" & Server.URLEncode("http://www.example.com/") & "&type=xml&link=" & request.QueryString("url"))
Set objRoot = objXML.documentElement
Set objItems = objRoot.getElementsByTagName("story")
For Each objItem in objItems
temp = objItem.getAttribute("diggs")
Next
Set objRoot = Nothing
Set objItems = Nothing
response.Write(temp)
<script type="text/javascript">
$(document).ready(function(){
$.get("digg.asp?url=http://www.example.com/", function(data){
$("#digg").text(data);
});
});
</script>
<p>Digg: <span id="digg"></span></p>
Conclusion
I realize that not everyone uses classic ASP anymore but the same principles can be easily applied in PHP as well. For an example of this in action check out the source of the page you're currently on.