Sina API request latest sample into shorter connection length of the connecting

Sina api short URL is long links Sina official publicly into short links API, lengthy short link address can be shortened links generated t.cn/xxx format.

 

API available in two formats

http://lnurl.cn/sina/short-api?url_long=http://www.baidu.com

http://kndwz.com/api-tcn.php?link=http://www.baidu.com

 

Request parameter

Interface authorization key assigned when the application calls the interface, on behalf of the user when the call interface unique. url_long and rear link url is long link you want to convert, if your url with parameters or special symbols in need URLencoded, this may result in the loss parameters.

 

Application of the key steps

1, enter  http://lnur.cn/api  , select the API interface.

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

3, the interface obtaining authorization key, key that uniquely identifies the interface requests.

4. Copy the API interface address, you can call generate short links.

 

Interface instructions

1, online use

Two interfaces mentioned above for the public interface, without the authorization key to the normal call. For online use, just need to url_long and rear link url link into long they need to convert, and then copy the entire browser interface address Go open to generate short links.

2, the request interface

High demand friend, would have a direct access interface, interface to generate a short link request.

 

Request Interface Example

1, PHP sample request:

1 $url = 'http://www.baidu.com';
2 $api_url = 'http://lnurl.cn/sina/short-api?url_long=http://www.baidu.com;
3 $short_url = file_get_contents($api_url);
4 echo $short_url;

 

2、Java请求示例:

 1 public static void main(String path[]) throws Exception {
 2 URL u = new URL("http://lnurl.cn/sina/short-api?url_long=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 }
11finally {
12 if ( in != null) {
13 in .close();
14 }
15 }
16 byte b[] = out.toByteArray();
17 System.out.println(new String(b, "utf-8"));
18 }

 

3、Python请求示例:

 1 import urllib, urllib2, sys
 2 host = 'http://lnurl.cn'
 3 path = '/sina/short-api'
 4 method = 'GET'
 5 querys = 'url_long=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)

 

注意事项:

1、调用API接口时,只需将 “http://www.baidu.com”换成需要缩短的长链接即可。

2、接口支持链接中带参数,但要注意的是当链接中出现 & 符号时,请用 %26 代替(或者使用url编码),否则参数可能会丢失。

3、更换链接时,必须要以http(s)://开头,否则可能会导致短网址生成失败或者生成的短网址无法跳转访问原网站。

 

常见问题:

1、长链接转换后,为什么结尾的参数丢失了?

答:因为长链接中含有特殊字符,需要将url编码后再使用接口生成。

2、接口没有返回结果,是什么情况?

答:有些时候接口返回数据会有延迟,超时未返回即生成失败,也就不会返回结果;或者是因为原链接被封了。

3、生成的短链接有效期是多久?有没有访问次数限制?

答:生成的新浪短链接是永久有效的,没有点击次数限制,可以放心使用。

Guess you like

Origin www.cnblogs.com/yyqxwa/p/12077676.html