仅使用 Javascript 使用 Bit.ly API 缩小 URL [英] Using Only Javascript To Shrink URLs Using The Bit.ly API

查看:25
本文介绍了仅使用 Javascript 使用 Bit.ly API 缩小 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些天我在玩 Javascript ……我使用 bit.ly 缩小了一些 URL 来发推文,然后我开始考虑可以使用他们的 API 来缩小我想要的 URL 的自动化过程,然后我查看了他们的文档,我看到他们只支持 PHP(带有一些 Javascript),但无论如何我可以只使用 Javascript 来实现它?

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?

推荐答案

这里是一个示例,如何使用 Bitly API 和 jQuery 获取缩短的 URL,无需服务器端代码.

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);
});

这篇关于仅使用 Javascript 使用 Bit.ly API 缩小 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆