Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of HTTP short URL Generation Interface

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

What this article shares with you is an example analysis of the interface for generating HTTP short URLs. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

Brief description

A short URL, as its name implies, is a short address composed of a shorter domain name and dynamic parameters, similar to http://t.cn/xxxx journal url.cnbig xxx. It is the conversion of long URLs from the short URL interfaces released by major platforms such as Sina, Tencent and Baidu.

Application scenario

Short URLs are used in a wide range of application scenarios, such as SMS marketing, email promotion, Wechat marketing, QQ marketing, self-media promotion, channel promotion and so on.

The reason is that short URL can reduce promotion cost, user memory cost, improve user click rate; promotion in specific scenarios can also avoid keywords, prevent domain names from being intercepted, hide real addresses, and so on.

Short URL generation interface

1. The latest official interface of Sina

Http://lnurl.cn/tcn-api.json?key=hd3j2ryt&url=http://www.baidu.com

2. The latest official interface of Tencent

Http://lnurl.cn/url-api.json?key=a1yl3piu&url=http://www.baidu.com

Interface document

1. Request method

POST

2. Request parameters

Parameter required type indicates that KEY is obtained by string login.

3. Parameter description

The API API mentioned above is an official published public interface, which can be used directly without obtaining an authorized Key. However, there is a limit on the number of requests. If the limit is exceeded, the API will return "user dose not have resource to register long term short url".

Unlimited interface is the only identity that needs to be registered to obtain authorization. Key,Key is the only identity used to identify an enterprise or individual when requesting an interface. It is used for authentication, please do not disclose it at will.

4. Obtain the authorized KEY

1. Enter http://lnurl.cn/ and select to activate the API immediately.

2. Go to the API management console after registering to create a unique user id.

3. Get the authorization KEY and request address of the interface. KEY is the only identification of the request interface.

4. Copy the whole string of API addresses and you can request to generate them.

5. Request description

① is used online

Just replace the "http://www.baidu.com"" with a long URL that needs to be shortened (with http (s): / /), and then copy the whole string of links to the browser to open it.

② request interface

The request API is generated automatically and needs to be connected to the program. Examples of requests related to PHP, Java and Python are as follows.

6. Request example

Example of PHP request:

$url = 'http://www.baidu.com'; $api\ _ url =' http://lnurl.cn/tcn-api.json?key=hd3j2ryt&url=http://www.baidu.com; $short\ _ url = file\ _ get\ _ contents ($api\ _ url); echo $short\ _ url

Example of Java request:

Public static void main (String path\ [\]) throws Exception {URL u = new URL ("http://lnurl.cn/tcn-api.json?key=hd3j2ryt&url=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"));}

Example of Python request:

Import urllib, urllib2, sys host = 'http://lnurl.cn' path =' / tcn-api.json?key=hd3j2ryt' 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. When you request an interface, you need to do urlencode processing, otherwise it may result in parameter loss, short URL failure and so on.

2. The long URL of the request interface must start with http (s): / /.

3. The frequency of API requests should not be too fast, which is normal once per second.

Frequently asked questions:

1. Why are the parameters lost after long URL conversion?

A: because there is no urlencode processing, some special characters are not recognized. You need to encode the url before requesting the interface.

2. Why did the request API not return a result?

A: sometimes there is a delay in the return of data from the API, which will result in a timeout. Or because the original link is blocked.

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

A: the short URLs generated by the shared interface are valid, and all the short URLs generated by the official version of the interface are permanent.

4. Are there any restrictions on access to short URLs?

This API is the official interface between Sina and Tencent. There is no limit on the number of visits, so you can rest assured that you can use it.

The above is an example analysis of the HTTP short URL generation interface. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report