Get the App
SLTechnology News&Howtos  ›  Development  › 

How to call the API interface of Sina short URL by PHP and Python

Shulou Source: shulou.com Published: 2022-06-02 10:44:45 10月01日 Update

This article mainly introduces "how PHP and Python call Sina short URL API interface". In daily operation, I believe many people have doubts about how PHP and Python call Sina short URL API interface. Xiaobian consulted all kinds of materials and sorted out simple and easy to use operation methods. I hope to answer your doubts about "how PHP and Python call Sina short URL API interface"! Next, please follow the small series to learn together!

Instructions for use:

Replace "http://www.baidu.com" in the api interface address with a shortened URL, and then copy it directly to the browser to open it.

PHP call demo:

$url = 'http://www.baidu.com';$api_url = 'http://qingmeidwz.cn/shorten.php? url_long=http://www.baidu.com';$short_url = file_get_contents($api_url);echo $short_url;

JAVA call demo:

public static void main(String path[]) throws Exception {URL u = new URL("http://qingmeidwz.cn/shorten.php? url_long=http://www.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 calls demo:

import urllib, urllib2, syshost = 'http://qingmeidwz.cn/'path = 'shorten.php? url_long='method = 'GET'querys = 'url=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)

Notes:

① When calling the api interface, just replace "http://www.baidu.com" with a long URL that needs to be shortened.

② The interface supports url parameters. When the & symbol appears in url, please replace it with %26, otherwise the parameters may be lost.

③ When filling in the url, it must start with http(s)://, otherwise it may cause the short URL generated to be unable to access the original website.

④ The t.cn short URL api interface above is very stable after testing. I think it's good to remember to collect it so as not to lose it.

Frequently Asked Questions:

① Long link conversion, why are the parameters at the end lost?

Answer: Because url contains special characters, you need to use UTF8 encoding format to encode url.

② Interface does not return results, what is the situation?

A: Sometimes there is a delay in the data returned by the interface. If the delay does not return, it will prompt that the generation fails; or the original link is blocked.

3 How long is the generated t.cn short URL valid? Is there a limit on the number of visits?

A: The generated t.cn short URL is permanently valid, and there is no limit on the number of clicks. It can be used arbitrarily.

At this point, the study of "how PHP and Python call Sina short URL API interface" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Tags: Interfaces URLs Sina learning parameters demos generation valid more times coding websites links questions help limitations utility special next above Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei MySQL Redmi NVidia Docker