首先到 bit.ly 官方网站(bit.ly)申请一个账号,然后获取API KEY。注册过后,通过该账号进行的域名缩短服务都会有记录,可以帮助用户进行分析等。
注册账号后,只要进入Setting,可以看到你的API key。
API 说明:
REST API¶
/v3/shorten¶
For a long URL, /v3/shorten encodes a URL and returns a short one.
Parameters
- format (optional) indicates the requested response format. supported formats: json (default), xml, txt.
- longUrl is a long URL to be shortened (example: http://betaworks.com/).
- domain (optional) refers to a preferred domain; either bit.ly default or j.mp. This affects the output value of url.
- x_login (optional) is the end-user's login when make requests on behalf of another bit.ly user. This allows application developers to pass along an end user's bit.ly login.
- x_apiKey (optional) is the end-user's apiKey when making requests on behalf of another bit.ly user. This allows application developers to pass along an end user's bit.ly apiKey.
Notes
- Long URLs should be URL-encoded. You can not include a longUrl in the request that has '&', '?', '#', ' ', or other reserved parameters without first encoding it.
- Long URLs should not contain spaces: any longUrl with spaces will be rejected. All spaces should be either percent encoded (%20) or plus encoded (+). Note that tabs, newlines and trailing spaces are all indications of errors. Please remember to strip leading and trailing whitespace from any user input before shortening.
- Long URLs must have a slash between the domain and the path component. For example, http://example.com?query=parameter is invalid, and instead should be formatted as http://example.com/?query=parameter
- When including x_login and x_apiKey, the shortened URL will be inserted into the history for the user specified by x_login. it will not be inserted into the history for the user specified by login.
参考URL: http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/shorten
http://code.google.com/p/bitly-api/wiki/ApiBestPractices#Rate_Limiting
使用方法:
t通过HTTP请求的方式。
GET方式:
http://api.bit.ly/v3/shorten?login=XXX&apiKey=R_a2dc759....&longUrl=http%3A%2F%2Fwww.baidu.com%2F&format=xml
其中:
Login : 表示用户名
Apikey: 表示API key (注册账号后即分配)
longUrl: 表示要被转换的长URL (注释格式必须正确)
format: 表示指定返回值类型,bit.ly支持三种形式: json、xml、txt,默认为json。
还可以加其他的参数,具体参照上面的Parameters 说明。
请求后返回结果:
上图中,url是缩短后的网址。
总结:
1.
需要到bit.ly注册一个账号,需要用到 用户名、API key
2.
注意选择返回值类型,json、xml、txt (系统默认json)
3.
注意选择域名缩短的形式: bit.ly 、 m.jp
4.
另外在进行域名缩短前,可能还得考虑bit.ly支持哪些国家等等信息。避免软件在其他国家使用时异常终止等事件的发生。
5.
有访问频率限制。单个IP最多允许5个连接。访问频率每小时会刷新。