出于某种原因,我似乎无法让我的函数将所有url转换为bit.ly。请保留它,以便它仍然将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>')
}
下面是一个如何使用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);
> }
> ); }
下面的代码可以用来获得一个简短的URL:
/*在sign_up注册Bitly帐户
完成后访问your_api_key/获取"login"one_answers"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
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;
}