Sina short URL is generated online, and the official api interface is obtained.
Recently, Sina Weibo Technology Center banned most of the keys used to generate short URLs: app_key, which led to the paralysis of the third-party short link generation service. The reason why Sina does this is to completely peel the short links from the Weibo platform, and to this end, it has specially set up a t.cn short chain generation platform for everyone to generate short chains free of charge.
Interface address: http://sina-t.cn/api?link=http://baidu.com
Instructions for use:
Replace the "http://baidu.com"" in the api interface address with the URL that needs to be shortened, and then copy it directly to the browser to open it.
Demo of PHP call:
$url = 'http://www.baidu.com';$api_url =' http://sina-t.cn/api?link=http://www.baidu.com;$short_url = file_get_contents ($api_url); echo $short_url
Demo of JAVA call:
Public static void main (String path []) throws Exception {URL u = new URL ("http://sina-t.cn/api?link=http://www.baidu.com");InputStream in = u.openStream (); ByteArrayOutputStream out = new ByteArrayOutputStream (); try {byte buf [] = new byte [1024]; int read = 0X 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"));}
Demo of Python call:
Import urllib, urllib2, syshost = 'http://sina-t.cn'path =' api'method = 'GET'querys =' l=http%3A%2F%2Fwww.baidu.com'bodys = {} url = host + path +'?'+ querysrequest = urllib2.Request (url) response = urllib2.urlopen (request) content = response.read () if (content): print (content)
Note:
When ① calls the api interface, simply replace "http://www.baidu.com"" with a long URL that needs to be shortened.
The ② interface supports the url parameter. When the & symbol appears in the url, use% 26 instead (or use the url encoding format), otherwise the parameter may be lost.
③ must start with http (s): / / when filling in url, otherwise the resulting short URL may not be able to access the original website.
④ mentioned above several url.cn short URL api interface, after testing is relatively stable, feel good to remember to collect, so as not to lose.