Simple Url shortener script using Sql and TinyURL

Simple Url shortener script using Sql and TinyURL

Posted On: April 16, 2019 Published By: Rachel Jaya Prabha S

A URL shortener is very useful script to shorten your large URL. In this post, I explain you how to code for

URL shortener.

URL sharpeners are being used more now than ever because people need to post short links on Twi!er,

Facebook, SMS etc. Long links can some%mes take up the en%re 140-character limit. So, the solu%on is

to shrink a long URL into a short one.

Example:

Declare @InputURL varchar(200),@CreateURL varchar(500),@Obj Int,@TinyURL

varchar(500)

Set @InputURL='https://en.wikipedia.org/wiki/Search_engine_marketing'

--//Create Tiny URL

set @CreateURL = 'http://tinyurl.com/api-create.php?url='+ @InputURL

exec sp_OACreate 'MSXML2.ServerXMLHttp', @Obj OUT

exec sp_OAMethod @Obj, 'Open', NULL, 'POST', @CreateURL, false

exec sp_OAMethod @Obj, 'send'

exec sp_OAGetProperty @Obj, 'responseText', @TinyURL OUT

exec sp_OADestroy @Obj

Select @InputURL AS [Original URL]

Select @CreateURL AS [Original URL passed to php page of Tiny]

Select @TinyURL AS TinyURL

Output:

Original URL

https://en.wikipedia.org/wiki/Search_engine_marketing

Original URL passed to php page of Tiny

http://tinyurl.com/api-create.php?url=https://en.wikipedia.org/wiki/Search_engine_marketing

TinyURL

http://tinyurl.com/gkqaq7a

Comments

Popular posts from this blog

Power BI Beginner