How to call Sina short link api interface from php, java and Python
This article mainly explains "php, java, Python how to call Sina short link api interface", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "php, java, Python how to call Sina short link api interface"!
1. APIKEY Get:
http://url-t.cn/api
2. calling code
PHP call code
$url = 'http://www.baidu.com'; $api\_url = 'http://url-t.cn/tcn/api? key=APIKEY/tcn\_url? url='.urlencode($url); $short\_url = file\_get\_contents($api\_url); echo $short\_url;
Java call code
public static void main(String path\[\]) throws Exception { URL u = new URL("http://url-t.cn/tcn/api? key=APIKEY&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 call code
import urllib, urllib2, sys host = 'http://url-t.cn' path = '/tcn/api' method = 'GET' querys = 'key=APIKEY&url=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)3. Docking prompt:
a. Long links need url encoding, especially those with parameters after the link, which must be encoded first.
b. Long links need to have a protocol header, http://or https://
c.APIKEY is a unique identification code, which needs to be replaced with your own KEY before docking.
d. It is recommended to add exception handling to the code. Because of network patency, timeout exceptions may occur.
At this point, I believe that everyone has a deeper understanding of "php, java, Python how to call Sina short link api interface", may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!