Short URL with Classic ASP
Posted on 15.05.09 03:36:20 by Ariel G. Saputra in Classic ASP, Tutorials
I have been playing a bit with Classic ASP (again) in back couple days, also playing with flickr api and twitter api, but then pop out an idea to create a very simple library tom implement Twitter API in Classic ASP programming.
I have created some function to achieve this goal and one of them is shorten url class, Shorten URL is common in twitter to shortenize url because of characters limitation, I have posted sample of creating short url via tinyURL, here i have encapsulated the codes in a class and adding more shorten url providers (is.gd & bit.ly).
shorten url with TinyURL & Classic ASP
function asp_short_url(strUrl)
Dim oXml,strTinyUrl
strTinyUrl = "http://tinyurl.com/api-create.php?url=" & strUrl
set oXml = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
oXml.Open "GET", strTinyUrl, false
oXml.Send null
asp_short_url = oXml.responseText
Set oXml = nothing
End Function
shorten url with is.gd & Classic ASP
is.gd API is just as simple as tinyURL so here we simply use the sampe functions, just change the API url
http://is.gd/api.php?longurl=your_url_here
shorten url with bit.ly & Classic ASP
bit.ly is a bit more complicated, to use bit.ly you need to signup and get the API KEY or also possible to use login name and password with BASIC HTTP AUTHENTICATION, the return format available is JSON & XML. Here I will stick with Login Name, API KEY, and XML data format.
bit.ly api target
http://api.bit.ly/shorten?version=2.0.1&format=xml&longUrl=your_url&login=your_login&apiKey=your_api_key
Return Data in XML format
Below is sample of bit.ly return data in XML format
<bitly> <errorCode>0</errorCode> <errorMessage></errorMessage> <results> <nodeKeyVal> <userHash>VMR4M</userHash> <shortKeywordUrl></shortKeywordUrl> <hash>17ffBi</hash> <nodeKey><![CDATA[your_url]]></nodeKey> <shortUrl>http://bit.ly/VMR4M</shortUrl> </nodeKeyVal> </results> <statusCode>OK</statusCode> </bitly>
So here we will need to query the XML to get
Query XML with Classic ASP (XPATH)
function asp_grab_short(strXml)
Dim oXmlDom,strGrabUrl
set oXmlDom = Server.CreateObject("Microsoft.XMLDOM")
oXmlDom.async = false
oXmlDom.setProperty "SelectionLanguage", "XPath"
oXmlDom.loadxml(strXml)
strGrabUrl = oXmlDom.selectSingleNode("/bitly/results/nodeKeyVal/shortUrl").Text
Set oXmlDom = Nothing
asp_grab_short = strGrabUrl
function
function bit.ly api query
function asp_short_url(strUrl)
Dim oXml,strUrl
strUrl = "http://api.bit.ly/shorten?version=2.0.1&format=xml&longUrl="&strUrl&"&login=your_login&apiKey=your_api_key"
set oXml = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
oXml.Open "GET", strUrl, false
oXml.Send null
asp_short_url = oXml.responseText 'return is xml data
Set oXml = nothing
End Function
Easy heh? now lets wrap them up in a class.
'************************************************************************
'* @TITLE: Shorten URL Class
'* @PACKAGE: Simple Classic ASP Twitter API
'* @AUTHOR: Ariel G. Saputra <webmaster@asp.web.id>
'* @DESCRIPTION: Class to create short redirection url
'* @SCOPE: TinyURL,is.gd,bit.ly
'* @DATE: May 15 2009
'* @TODO: Adding more free url redirection providers
'************************************************************************
class UrlBawak
private m_StrProvider,m_StrApiUrl,m_StrUserName,m_StrApiKey
public property let aspSetProvider(strProvider)
m_StrProvider = Lcase(strProvider)
select case m_StrProvider
case "bitly"
m_StrApiUrl = "http://api.bit.ly/shorten?version=2.0.1&format=xml&longUrl=[:URL:]&login=[:LOGIN:]&apiKey=[:API:]"
case "isgd"
m_StrApiUrl = "http://is.gd/api.php?longurl=[:URL:]"
case else
m_StrApiUrl = "http://tinyurl.com/api-create.php?url=[:URL:]"
end select
end property
public property let aspSetUser(strUser)
m_StrUserName = strUser
end property
public property let aspSetApi(strApi)
m_StrApiKey = strApi
end property
sub class_initialize()
me.aspSetProvider = "tinyurl"
m_StrUserName = m_StrApiKey = ""
end sub
sub class_terminate()
end sub
private function aspGrabUrl(strUrl)
select case m_StrProvider
case "bitly"
Dim oXmlDom,strGrabUrl
set oXmlDom = Server.CreateObject("Microsoft.XMLDOM")
oXmlDom.async = false
oXmlDom.setProperty "SelectionLanguage", "XPath"
oXmlDom.loadxml(strUrl)
strGrabUrl = oXmlDom.selectSingleNode("/bitly/results/nodeKeyVal/shortUrl").Text
Set oXmlDom = Nothing
aspGrabUrl = strGrabUrl
case else
aspGrabUrl = strUrl
end select
end function
public function aspShortUrlExec(strUrl)
if Len(m_StrApiUrl) > 0 then
Dim oXml,strRealUrl,strShortUrl
strRealUrl = Replace(m_StrApiUrl, "[:URL:]", strUrl, 1, -1, 1)
strRealUrl = Replace(strRealUrl, "[:LOGIN:]", m_StrUserName, 1, -1, 1)
strRealUrl = Replace(strRealUrl, "[:API:]", m_StrApiKey, 1, -1, 1)
set oXml = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
oXml.Open "GET", strRealUrl, false
oXml.Send null
strShortUrl = oXml.responseText
Set oXml = nothing
aspShortUrlExec = aspGrabUrl(strShortUrl)
else
aspShortUrlExec = false
end if
end function
end class
Sample of usage
Dim obj,tinyUrl,isGdUrl,bitlyUrl
Set obj = new UrlBawak
' get tinyUrl
tinyUrl = obj.aspShortUrlExec("http://asp.web.id/update-twitter-status-with-classic-asp-vbscript.html")
' get is.gd
obj.aspSetProvider = "isgd"
isGdUrl = obj.aspShortUrlExec("http://asp.web.id/update-twitter-status-with-classic-asp-vbscript.html")
' get bit.ly
obj.aspSetProvider = "bitly"
obj.aspSetUser = "username"
obj.aspSetApi = "API_KEY"
bitlyUrl = obj.aspShortUrlExec("http://asp.web.id/create-tinyurl-with-classic-asp-vbscript.html")
Response.Write(tinyUrl&"<hr />"&isGdUrl&"<hr />"&bitlyUrl)
Set obj = nothing
Thats it, Classic ASP Rocks!

0 Comments For This Post
2 Trackbacks For This Post
Classic ASP (VBscript) class create short url Says:
[...] Classic ASP class to shortenize URL via TinyURL, bit.ly, is.gd service. Here you will learn also how to use microsoft XMLHTTP object, access remote page via Classic ASP script. Share this article: [...]
Create Shorten URL on the fly with API, VBScript & PHP « Bali Web Design Studio Says:
[...] both version shortening URL via PHP and shortening URL via Classic ASP/VBScript, it is fully OOP approach and it will be easy to add more shortening URL services to the [...]