Advertisement

t.cn 短网址怎么生成?新浪短网址api接口

阅读量:

t.cn 短网址怎么生成?新浪短网址api接口

项目中将采用新浪短网址的API接口,在此前专门对相关技术进行了深入研究的过程中,全面梳理了相关信息,并通过多轮测试与优化最终确定了四款最优方案。最终结果如下呈现。

http://sina-t.cn/api?link=http://baidu.com

http://tttool.cn/sina_api?url=http://baidu.com

http://knurl.cn/tcnapi?url_long=http://baidu.com

http://migourl.cn/sina_shorturl.html?text=http://baidu.com

使用方法

将最后的http://baidu.com替换成较短的链接即可;可以通过程序调用或者直接访问获得结果。

这四个接口返回格式相对而言比较简单,直接返回结果

调用demo

PHP调用代码:

复制代码
 $url = 'http://www.baidu.com';

    
 $api_url = 'http://sina-t.cn/api?link='.urlencode($url);
    
 $short_url = file_get_contents($api_url);
    
 echo $short_url;

JAVA调用代码:

复制代码
 public static void main(String path[]) throws Exception {

    
 URL u = new URL("http://sina-t.cn/api?link=http%3A%2F%2Fwww.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://sina-t.cn'
    
 path = 'api'
    
 method = 'GET'
    
 querys = 'link=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)

注意事项

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

在本接口设计中允许传递 URL 参数。当 URL 中出现 & 符号字符时,请建议将该符号表示为 %26 或者采用 URL 编码方法。否则可能导致参数信息无法正确传递。

在填写链接时,必须使用http(s)://开头;否则可能导致生成的简短链接无法访问原网站

全部评论 (0)

还没有任何评论哟~