首先到 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個串連。訪問頻率每小時會重新整理。