How to shorten the link is too long? Stable short link api interface to share with usage examples

Micro-channel Internet Marketing, SMS, microblogging and promotion of soft paper in a very long links affect marketing results, then how will shorten long links into short links it? Today to explain URL shortening under way, Sina and Tencent http://t.cn/xxx short URL short URL http://url.cn/xxx two styles, for example.

Sina short link api interfaces:

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

Tencent short link api interfaces:

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

Instructions for use:

The api interface address in the " http://www.baidu.com " replaced the need to shorten the URL, then copy direct access to the browser to open.

PHP call demo:

$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 call demo:

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 calls the demo:

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)

Precautions:

① When calling api interface simply " http://www.baidu.com " replaced the need to shorten the long URL.

② interface supports url parameters, when the & symbol appears in the url, use instead of 26%, otherwise the parameters may be lost.

When ③ fill url, must be based on http (s): // at the beginning, it may cause a short URL to give birth can not access the original site.

t.cn short URL ④ above and url.cn of api interface has been tested and is very stable, it felt good to remember collection, to avoid loss.

common problem:

① long link translation parameters why the end is lost?

A: Because the url contains special characters, you need to use UTF8 encoding format, the encoded url

② interface does not return a result, what is the situation?

A: Sometimes the interface returns the data will be delayed, the delay did not return will be prompted to generate a failure; or because the original link was closed.

How long ③ generated t.cn short URL and url.cn valid for? There is no limit visits?

A: t.cn generated short URL and url.cn permanent and effective, and there is no limit clicks, you can use any

Guess you like

Origin blog.51cto.com/14634816/2457230