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

查看:322
本文介绍了仅使用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天全站免登陆