短地址转换
is a great URL shortening service. I love their reliability, shortness of the URL, and the information they provide about a given URL. Recently updated their API to version 3 so I thought I'd update my original post. Here's how you can create short URLs and expand short URLs using .
是一个很棒的URL缩短服务。 我喜欢它们的可靠性,URL的简短性以及它们提供的有关给定URL的信息。 最近将他们的API更新到了版本3,所以我想我应该更新我的原始帖子 。 这是使用创建短URL并扩展短URL的方法。
PHP (The PHP)
-
-
/* returns the shortened url */
-
function get_bitly_short_url($url,$login,$appkey,$format='txt') {
-
$connectURL = '/v3/shorten?login='.$login.'&apiKey='.$appkey.'&uri='.urlencode($url).'&format='.$format;
-
return curl_get_result($connectURL);
-
}
-
-
/* returns expanded url */
-
function get_bitly_long_url($url,$login,$appkey,$format='txt') {
-
$connectURL = '/v3/expand?login='.$login.'&apiKey='.$appkey.'&shortUrl='.urlencode($url).'&format='.$format;
-
return curl_get_result($connectURL);
-
}
-
-
/* returns a result form url */
-
function curl_get_result($url) {
-
$ch = curl_init();
-
$timeout = 5;
-
curl_setopt($ch,CURLOPT_URL,$url);
-
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
-
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
-
$data = curl_exec($ch);
-
curl_close($ch);
-
return $data;
-
}
-
-
/* get the short url */
-
$short_url = get_bitly_short_url('/','davidwalshblog','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
-
-
/* get the long url from the short one */
-
$long_url = get_bitly_long_url($short_url,'davidwalshblog','xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
All you really need to is pass your appkey and login (you must sign up for their API service), the long or short URL, and the format which you'd like the result to be returned in. If you just want a simple URL with no other information, use the default "txt" format. Retrieving the XML or JSON formats will provide you more information about the URL.
您真正需要做的就是传递您的appkey和登录名(您必须注册其API服务),长或短URL以及希望返回结果的格式。如果您只想使用简单的URL没有其他信息,请使用默认的“ txt”格式。 检索XML或JSON格式将为您提供有关URL的更多信息。
is awesome. I mean, Twitter uses them -- what more of an endorsement would you need.
很棒。 我的意思是,Twitter使用它们-您还需要更多认可。
翻译自: /bitly-api-php
短地址转换