我们可能会收到类似于这样的短信,发现其中的链接并不是常规的网址链接,而是个短小精悍的短链接,产品中经常需要这样的需求,如果在给用户下发的短信中是一个很长的连接,用户体验肯定很差,因此我们需要实现长链接转换成短链接。但是前段时间新浪前段时间关闭了生成的入口!导致很多小伙伴无法使用了
今天我就给打分享一个利用调用新浪短链接/腾讯短链接API接口的方法。分享给大家供大家参考使用。
短网址接口地址:
1.接口地址: http://www.wx-url.cn/sina.php?url_long=http://www.baidu.com
2.接口地址: http://dogdwz.cn/tcnjson?url=http://www.baidu.com
3.接口地址: http://www.sina-url.cn/xinlang?url=http://www.baidu.com
4.接口地址:http://www.wx-dwz.cn/tcnurl?url=https://www.baidu.com
5.接口地址:http://mhdwz.cn/mhsina?url=http://www.baidu.com
6.接口地址:http://maiyurl.cn/weibourl?url=http://www.baidu.com
说明:将上面短网址api接口的标红部分的链接替换成需要缩短的长连接即可!
PHP调用代码:
$url = 'http://; $api_url = ''.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%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 = ''
path = ''
method = 'GET'
querys = 'http%3A%2F%2F
bodys = {}
url = host + path + '?' + querys
request = urllib2.Request(url)
response = urllib2.urlopen(request)
content = response.read()
if (content):
print(content)
1、使用api接口时,只需将 URL=后的链接替换成自己这边需要缩短的长链接即可。
2、接口支持参数传递,当链接中出现 & 符号时,请用 %26 代替(或者使用url编码),否则携带的参数会丢失。
3、填写链接时,必须使用http(s)://协议,否则API接口将会无法生成短链接!
本文出自浪狐短网址: http://www.sinadwz.cn/pvoibz.html 转载请注明出处
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69960687/viewspace-2675007/ ,如需转载,请注明出处,否则将追究法律责任。