I'm playing a bit with Javascript these days... I was shrinking some URLs using bit.ly to tweet them, then I started to think on a automated process that could use their API to shrink the URLs I wanted, then I looked up on their documentation, and I saw that they only support PHP(with some Javascript), but there is anyway that I could make this using only Javascript?
Here is an example how to get a shortened URL with Bitly API and jQuery, no server side code required.
function get_short_url(long_url, login, api_key, func)
{
$.getJSON(
"http://api.bitly.com/v3/shorten?callback=?",
{
"format": "json",
"apiKey": api_key,
"login": login,
"longUrl": long_url
},
function(response)
{
func(response.data.url);
}
);
}
The following code could be used to get a short URL:
/*
Sign up for Bitly account at
https://bitly.com/a/sign_up
and upon completion visit
https://bitly.com/a/your_api_key/
to get "login" and "api_key" values
*/
var login = "LOGIN_HERE";
var api_key = "API_KEY_HERE";
var long_url = "http://www.kozlenko.info";
get_short_url(long_url, login, api_key, function(short_url) {
console.log(short_url);
});
Depending on where the JavaScript is executing, you could always use the bit.ly REST API:
http://code.google.com/p/bitly-api/wiki/ApiDocumentation
via XmlHttpRequest, for example:
http://api.bit.ly/v3/shorten?login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&longUrl=http%3A%2F%2Fbetaworks.com%2F&format=json
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With