Awesome! The latest short URL Generator Interface (Sina, Tencent official api)

A brief description

Short URL, as the name suggests is a kind of a short domain name plus dynamic parameters consisting of short address, similar to t.cn/xxxx,url.cn/xxx. Short URL is published by the major platforms such as Sina, Tencent, Baidu interface converted from long URLs.

Scenarios

Short URL scenarios is very broad, such as SMS marketing, email marketing, marketing micro letter, QQ marketing, media self-promotion, channel promotion, will be used short URL. The reason is that short URL can reduce promotion costs, user memory costs, improve user click-through rate; promotion also avoid keywords in a particular scene, was blocked to prevent the domain name, hide the real address.

Short URL Generator Interface

1, Sina short URL Interface (latest official api)

http://lnurl.cn/tcn-api.json?key=hd3j2ryt&url=http://www.baidu.com

2, Tencent short URL Interface (latest official api)

http://lnurl.cn/url-api.json?key=a1yl3piu&url=http://www.baidu.com

Document Interface

1, request method

  • POST 

2, request parameters

parameter required Types of Explanation
key Yes string Sign in to get registered

3 Parameter Description

The above-mentioned interface (API) is the official publication of the public interface, it can be used directly, without having to obtain authorization Key. But there are number of requests limit, the limit is exceeded, the interface will return "user dose not have resource to register long term short url".

Unlimited interfaces are required to register to obtain authorization Key, Key is used to identify unique business or personal request interface. Used for authentication use, not at liberty to reveal.

4, obtain authorization KEY

1, enter  http://lnurl.cn/ , immediately opened to select the interface.

2, after registration to create a unique user id go interface management console.

3, and obtaining interface KEY authorization request address, the request is uniquely identified KEY interface.

4, the whole string API copy address, the request can be generated.

5, the request described

① online use

Simply "http://www.baidu.com" need to shorten long URLs into (with the http (s): //), then copy the whole string browser to open links go to generate.

② request interface

Request interface automatically generated, the program needs access, PHP, Java, Python related requests exemplified below.

6, sample request

PHP request example:

1 $url = 'http://www.baidu.com';
2 $api_url = 'http://lnurl.cn/tcn-api.json?key=hd3j2ryt&url=http://www.baidu.com;
3 $short_url = file_get_contents($api_url);
4 echo $short_url;

 

Java request example:

 1 public static void main(String path[]) throws Exception {
 2 URL u = new URL("http://lnurl.cn/tcn-api.json?key=hd3j2ryt&url=http://www.baidu.com");
 3 InputStream in = u.openStream();
 4 ByteArrayOutputStream out = new ByteArrayOutputStream();
 5 try {
 6 byte buf[] = new byte[1024];
 7 int read = 0;
 8 while ((read = in .read(buf)) > 0) {
 9 out.write(buf, 0, read);
10 }
11 } finally {
12 if ( in != null) {
13 in .close();
14 }
15 }
16 byte b[] = out.toByteArray();
17 System.out.println(new String(b, "utf-8"));
18 }

 

Python sample request:

 1 import urllib, urllib2, sys
 2 host = 'http://lnurl.cn'
 3 path = '/tcn-api.json?key=hd3j2ryt'
 4 method = 'GET'
 5 querys = 'link=http%3A%2F%2Fwww.baidu.com'
 6 bodys = {}
 7 url = host + path + '?' + querys
 8 request = urllib2.Request(url)
 9 response = urllib2.urlopen(request)
10 content = response.read()
11 if (content):
12 print(content)

 

Additional information:

1, a request interface, you need to do urlencode treatment, or may result in the loss of parameters, such as short URL failed.

2, the long URL request interface must be to http (s): // at the beginning.

3, interface request frequency can not be too fast, normal 1 / s.

 

common problem:

1, after long URLs into why the argument is lost?

A: Because not do urlencode process, resulting in some special characters not recognized. The encoder then needs to request interface url.

2, request interface Why did not return a result?

A: Sometimes there is a delay data interface to return, it will lead to time-out did not return. Or because the original link was closed.

3, valid for generating the short URL is how long? There is no limit visits?

A: Share short URL generated interface is valid for all short URLs official version of the interface generation are permanently valid.

4, there is no short URL access restrictions?

This interface is Sina and Tencent official interface is not to limit the number of visits, is safe to use.

Original reprint to: http://lnurl.cn/1txjc5.html

Guess you like

Origin www.cnblogs.com/alsd/p/12149099.html