Get the App
SLTechnology News&Howtos  ›  Development  › 

How to call short URL to generate api Interface between php and python

Shulou Source: shulou.com Published: 2022-06-02 10:05:59 10月05日 Update

This article mainly introduces "how php and python call short URL to generate api interface". In daily operation, I believe many people have doubts about how php and python call short URL to generate api interface. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "how php and python call short URL to generate api interface". Next, please follow the editor to study!

Call method (take Sina t.cn short link api API as an example):

Demo of PHP call:

$url = 'http://www.baidu.com';$api_url =' http://qingmeidwz.cn/shorten.json?url_long=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://qingmeidwz.cn/shorten.json?url_long=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://qingmeidwz.cn/'path =' shorten.json?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)

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 url, replace it with% 26, 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.

④ above Wechat url.cn, Sina t.cn short URL api interface, after testing is very stable, feel good to remember to collect, so as not to lose.

Frequently asked questions:

① long link conversion, why is the parameter at the end lost?

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

The ② interface does not return a result. What happens?

A: sometimes there is a delay in the return of data from the API. If the delay is not returned, it will indicate that the generation failed, or because the original link is blocked.

How long is the validity period of the short URL generated by ③? Is there a limit on the number of visits?

A: the generated short URLs are permanent, and there is no limit on the number of clicks, so you can use them at will.

At this point, the study on "how php and python call short URLs to generate api interface" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Interfaces URLs generation learning parameters links demonstrations valid methods more times coding websites problems Sina help restrictions practical special next Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Linux Huawei NVidia OPPO Reno