Technorati’s Apis + Ajax

7
November
2005

Logo Technorati ApisUpdate: a new version of the script is available here.

Inspired by the swiss example, one week ago Luca Conti launched the proposal to create an italian blogs ranking based on Technorati.
To the point, searching for a simple way to made it, I found and suggested Duck Soup, a PHP library that allow simple utilization of all the infos served by Technorati’s APIs.

Using its BlogInfoQuery support, I create an AJAX powered simple way that returns the datas that Technorati links to every blog.

Here’s the example: just enter the URL of your blog, click the button and wait two seconds.

Technorati’s Blog Info

How it works?

The PHP below (called bloginfo.php) asks to duckSoup library to return an XML that contain the informations related to an URL passed by parameter blog_url.
For example, if I want to know the info corresponding to the URL http://www.vlog.it (this blog), I can made the call (an HTTP GET) trought the URL http://bloginfo.php?blog_url=http://www.vlog.it: the script will return an XML
with the tags <name>Vlog..it – videoblog gallery</name>, <rank>15680</rank> and so on.

<?php header(“Content-type:text/xml; charset=UTF-8″);
echo ‘<?xml version=”1.0″ standalone=”yes”?>’; ?>
<?php
include “duckSoup.php”;
$api = new duckSoup;

$api->api_key = ” “; // insert your API key
$api->type = ‘bloginfo’; // select a Technorati API
$api->params = array(‘url’ => $_REQUEST[‘blog_url’]);
$blog_url=$api->get_content();
echo “
<root>”;
if(empty($blog_url[‘result’][‘weblog’][‘name’])){
echo “<name>not indexed</name>”;}
else{

echo “<name>{$blog_url[‘result’][‘weblog’][‘name’]}</name>”;
}
if(empty($blog_url[‘result’][‘weblog’][‘url’])){

echo “<url>not indexed</url>”;}
else{
echo “<url>{$blog_url[‘result’][‘url’]}</url>”;
}
if(empty($blog_url[‘result’][‘weblog’]
[‘inboundlinks’])){

echo “<inboundlinks>not indexed</inboundlinks>”;}
else{
echo
“<inboundlinks>{$blog_url[‘result’][‘weblog’]
[‘inboundlinks’]}</inboundlinks>”;
}
if(empty($blog_url[‘result’][‘weblog’]
[‘inboundblogs’])){
echo “<inboundblogs>not indexed</inboundblogs>”;}
else{
echo
“<inboundblogs>{$blog_url[‘result’][‘weblog’]
[‘inboundblogs’]}</inboundblogs>”;
}
if(empty($blog_url[‘result’][‘weblog’][‘rank’])){
echo “<rank>not indexed</rank>”;}
else{
echo “<rank>{$blog_url[‘result’][‘weblog’][‘rank’]}</rank>”;
}
if(empty($blog_url[‘result’][‘weblog’]
[‘rssurl’])){

echo “<rssurl>url not indexed</rssurl>”;}
else{
echo “<rssurl>{$blog_url[‘result’][‘weblog’]
[‘rssurl’]}</rssurl>”;
}
if(empty($blog_url[‘result’][‘weblog’]
[‘atomurl’])){

echo “<atomurl>url not indexed</atomurl>”;}
else{

echo “<atomurl>{$blog_url[‘result’][‘weblog’]
[‘atomurl’]}</atomurl>”;
}

if(empty($blog_url[‘result’][‘weblog’][‘lastupdate’])){

echo “<lastupdate>url not indexed</lastupdate>”;}
else {
echo “<lastupdate>{$blog_url[‘result’][‘weblog’]
[‘lastupdate’]}</lastupdate>”;
}
if(empty($blog_url[‘result’][‘weblog’][‘foafurl’])){
echo “<foafurl>url not indexed</foafurl>”;}
else{

echo “<foafurl>{$blog_url[‘result’][‘weblog’]
[‘foafurl’]}</foafurl>”;
}
echo “</root>\n”;
?>

After the server-side, the client.
In the HTML input I will insert the URL of my request: the event handler (In this example I choose an “onkeypress” and “onclick” in a button, but It could be an “onblur” in text input, an “onmouseover” in a div etc…) call a the javascript function getInfo() that will ask the infos to PHP.
“Loading” and “results” are two div where to insert a wait message and the results.

Blog Url:
input type=”text” size=”25″ id=”blog_url” value=”http://” />
<input type=”button” value=”Get Info” onclick=”getInfo();” onkeypress=”getInfo();”//>
<div id=”loading”></div>
<div id=”results”></div>

In the javascript, before call getInfo(), I must to create the XMLHttpRequest object, and then to handle the data from XML created by PHP with the function handleHttpResponse(), that creates an unordered list with infos inside.

var http = getHTTPObject();
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject(“Msxml2.XMLHTTP”);
} catch (e) {
try {
xmlhttp = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/

if (!xmlhttp && typeof XMLHttpRequest != ‘undefined’) {
try {
xmlhttp = new XMLHttpRequest();
xmlhttp.overrideMimeType(“text/xml”);

} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}

var isWorking = false;
var url = “bloginfo.php?blog_url=”;

function getInfo() {
document.getElementById(‘results’).innerHTML =’ ‘;
document.getElementById(“loading”).innerHTML = ‘Loading…’;
if (!isWorking && http) {
var urlValue = document.getElementById(“urlV”).value;
http.open(“GET”, url + escape(urlValue), true);
http.onreadystatechange = handleHttpResponse;
isWorking = true;
http.send(null);
}
}

function handleHttpResponse() {
if (http.readyState == 4) {
if (http.responseText.indexOf(‘invalid’) == -1) {
var xmlDocument = http.responseXML;
var name = xmlDocument.getElements
ByTagName(‘name’).item(0).firstChild.data;
var url = xmlDocument.getElements
ByTagName(‘url’).item(0).firstChild.data;
var inboundlinks =xmlDocument.getElements
ByTagName(‘inboundlinks’).item(0).firstChild.data;
var inboundblogs = xmlDocument.getElements
ByTagName(‘inboundblogs’).item(0).firstChild.data;
var rank = xmlDocument.getElements
ByTagName(‘rank’).item(0).firstChild.data;
var rssurl = xmlDocument.getElements
ByTagName(‘rssurl’).item(0).firstChild.data;
var atomurl = xmlDocument.getElements
ByTagName(‘atomurl’).item(0).firstChild.data;
var lastupdate = xmlDocument.getElements
ByTagName(‘lastupdate’).item(0).firstChild.data;
var foafurl = xmlDocument.getElements
ByTagName(‘foafurl’).item(0).firstChild.data;
var u= document.createElement(‘ul’);
document.getElementById(‘results’).appendChild(u);
u.innerHTML =
‘<li><strong>Blog name:</strong> ‘+name+’</li>’+
‘<li><strong>URL:</strong> ‘+url+’</li>’+
‘<li><strong>Inbound links:</strong> ‘+inboundlinks+’</li>’+
‘<li><strong>Inbound blogs:</strong> ‘+inboundblogs+’</li>’+
‘<li><strong>Technorati Rank:</strong> ‘+rank+’</li>’+
‘<li><strong>URL RSS:</strong> ‘+rssurl+’</li>’+
‘<li><strong>URL Atom:</strong> ‘+atomurl+’</li>’+
‘<li><strong>Last update:</strong> ‘+lastupdate+’</li>’+
‘<li><strong>URL FOAF:</strong> ‘+foafurl+’</li>’;
document.getElementById(“loading”).innerHTML = ‘The results:’;
isWorking = false;
}
}
}

You can download the three scripts from here, next time (I think tomorrow) another experiment about the same topic.
(ah, some links:
->Technorati API
->Duck Soup
->A guide to XMLHttpRequest
)
See you!

20 comments:

Leave a Reply


Here's the list of topics.

Here's the months' archive.