鏈接太長如何縮短?穩定的短鏈接api接口分享與用法實例

網絡營銷中的微信,短信,微博和軟文推廣中鏈接太長非常影響營銷效果,那麼如何將長鏈接縮短成短鏈接呢?今天給大家講解下網址縮短方式,以新浪短網址http://t.cn/xxx 和騰訊短網址http://url.cn/xxx 兩種樣式爲例。

新浪短鏈接api接口:

http://qingmeidwz.cn/shorten.json?url_long=http://www.baidu.com

騰訊短鏈接api接口:

http://qingmeidwz.cn/wxshorturl.php?url_long=http://www.baidu.com

使用說明:

將api接口地址中 "http://www.baidu.com" 換成需要縮短的網址,然後直接複製前往瀏覽器中打開即可。

PHP調用演示:

$url = 'http://www.baidu.com';
$api_url = 'http://qingmeidwz.cn/shorten.json?url_long=http://www.baidu.com';
$short_url = file_get_contents($api_url);
echo $short_url;

JAVA調用演示:

public static void main(String path[]) throws Exception {
URL u = new URL("http://qingmeidwz.cn/shorten.json?url_long=http://www.baidu.com");
InputStream in = u.openStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
byte buf[] = new byte[1024];
int read = 0;
while ((read = in .read(buf)) > 0) {
out.write(buf, 0, read);
}
} finally {
if ( in != null) {
in .close();
}
}
byte b[] = out.toByteArray();
System.out.println(new String(b, "utf-8"));
}

Python調用演示:

import urllib, urllib2, sys
host = 'http://qingmeidwz.cn/'
path = 'shorten.json?url_long='
method = 'GET'
querys = 'url=http%3A%2F%2Fwww.baidu.com'
bodys = {}
url = host + path + '?' + querys
request = urllib2.Request(url)
response = urllib2.urlopen(request)
content = response.read()
if (content):
print(content)

注意事項:

① 調用api接口時,只需將 “http://www.baidu.com” 換成需要縮短的長網址即可。

② 接口支持url參數,當url中出現 & 符號時,請用 %26 代替,否則參數可能會丟失。

③ 填寫url時,必須要以http(s)://開頭,否則可能會導致生出的短網址無法訪問原網站。

④ 上文的t.cn短網址和url.cn的api接口,經測試非常穩定,覺得好記得收藏一下,以免丟失。

常見問題:

① 長鏈接轉換,爲什麼結尾的參數丟失了?

答:因爲url中含有特殊字符,需要使用UTF8編碼格式,將url編碼

② 接口沒有返回結果,是什麼情況?

答:有些時候接口返回數據會有延遲,延時未返回則會提示生成失敗;或者是因爲原鏈接被封了。

③ 生成的t.cn短網址和url.cn有效期是多久?有沒有訪問次數限制?

答:生成的t.cn和url.cn短網址永久有效的,而且沒有點擊次數限制,可以任意使用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章