将url转换为bit.ly

时间:2014-06-21 09:55:42

标签: javascript bit.ly

出于某种原因,我似乎无法将所有网址转换为bit.ly' s。请保留它,以便它仍然将url转换为超链接。谢谢,请帮助!!!

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

var login = "[mylogin]";
var api_key = "[my api]";
var long_url = text;

get_short_url(long_url, login, api_key, function(short_url) {
    console.log(short_url);
});

  urlify = function (text) {
    var urlRegex = /(https?:\/\/[^\s]+)/g;
    return text.replace(urlRegex, function(short_url) {
      return '<a href="' + short_url + '" target="_blank">' + short_url + '</a>';
    })
    // or alternatively
    // return text.replace(urlRegex, '<a href="$1">$1</a>')
  }

2 个答案:

答案 0 :(得分:1)

以下是如何使用Bitly API和jQuery获取缩短的URL的示例:

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

以下代码可用于获取简短的网址:

  

/ *在sign_up

注册Bitly帐户      

完成后访问your_api_key /获取   “login”和“api_key”值   * / var login =“LOGIN_HERE”; var api_key =“API_KEY_HERE”; var long_url =“url”;

     

get_short_url(long_url,login,api_key,function(short_url){       的console.log(SHORT_URL); });

其他信息:http://www.cinetwit.com/converting-url-to-bit-ly-format

答案 1 :(得分:0)

function make_bitly_url($url,$format = 'xml',$version = '2.0.1')
    {

        $login="urlogin";
        $appkey="ur_api_key";   

        $bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;
            $response = file_get_contents($bitly);

            $xml = simplexml_load_string($response);

        return $response;

    }