为 SQL 数据库动态生成短 URL?

发布于 2024-10-24 08:36:49 字数 163 浏览 0 评论 0原文

我的客户拥有超过 400,000 名客户的数据库。每个客户都分配有一个 GUID。他希望我选择所有记录,创建一个动态“短 URL”,其中包含此 GUID 作为参数。然后将此短网址保存到每个客户记录的字段中。

我的第一个问题是,是否有任何 URL 缩短网站允许您像这样以编程方式动态创建短 URL?

My client has database of over 400,000 customers. Each customer is assigned a GUID. He wants me to select all the records, create a dynamic "short URL" which includes this GUID as a parameter. Then save this short url to a field on each clients record.

The first question I have is do any of the URL shortening sites allow you to programatically create short urls on the fly like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

攒一口袋星星 2024-10-31 08:36:50

TinyUrl 允许您执行此操作(未广泛记录),例如:

http://tinyurl.com/api-create.php?url=http://www.stackoverflow.com/

变为 http://tinyurl.com/6fqmtu

所以你可以有

http://tinyurl.com/api-create.php?url=http://mysite.com/user/xxxx-xxxx-xxxx-xxxx

http://tinyurl.com/64dva66

guid 最终不会那么清晰,但 URL 应该是唯一的。

请注意,您必须通过 HTTPWebRequest 传递它并获取响应。

TinyUrl allow you to do it (not widely documented), for example:

http://tinyurl.com/api-create.php?url=http://www.stackoverflow.com/

becomes http://tinyurl.com/6fqmtu

So you could have

http://tinyurl.com/api-create.php?url=http://mysite.com/user/xxxx-xxxx-xxxx-xxxx

to http://tinyurl.com/64dva66.

The guid doesn't end up being that clear, but the URLs should be unique

Note that you'd have to pass this through an HTTPWebRequest and get the response.

凶凌 2024-10-31 08:36:50

您可以使用 Google 的 URL 缩短程序,他们有一个 API。

以下是相关文档:http://code.google.com/apis/ urlshortener/v1/getting_started.html

You can use Google's URL shortner, they have an API.

Here is the docs for that: http://code.google.com/apis/urlshortener/v1/getting_started.html

怎樣才叫好 2024-10-31 08:36:50

该 URL 不够短:?

http://www.clientsdomain.com/?customer=267E7DDD-8D01 -4F38-A3D8-DCBAA2179609

注意:我个人认为你的客户要求一些奇怪的东西。通过要求您在每个客户记录上创建一个 URL 字段(这将通过确定性算法基于客户的 GUID),他实际上是在要求您对数据库进行非规范化。

This URL is not sufficiently short:?

http://www.clientsdomain.com/?customer=267E7DDD-8D01-4F38-A3D8-DCBAA2179609

NOTE: Personally I think your client is asking for something strange. By asking you to create a URL field on each customer record (which will be based on the Customer's GUID through a deterministic algorithm) he is in fact essentially asking you to denormalize the database.

徒留西风 2024-10-31 08:36:50

URL 缩短网站使用的算法非常简单:

  1. 存储 URL 并将其映射到它的序列号。
  2. 将序列号 (id) 转换为固定长度的字符串。

第二步仅使用六个小写字母将为您提供当前应用程序所需的更多 (24^6) 组合,并且没有什么可以阻止在某个时间点使用更大的序列。如果允许使用数字和/或大写字母,则可以使用较短的序列。

转换的算法是基数转换(就像转换为十六进制时一样),用任何代表零的符号填充。这是一些用于转换的 Python 代码:

LOWER = [chr(x + ord('a')) for x in range(25)]
DIGITS = [chr(x + ord('0')) for x in range(10)]
MAP = DIGITS + LOWER

def i2text(i, l):
        n = len(MAP)
        result = ''
        while i != 0:
                c = i % n
                result += MAP[c]
                i //= n
        padding = MAP[0]*l
        return (padding+result)[-l:]

print i2text(0,4)
print i2text(1,4)
print i2text(12,4)
print i2text(36,4)
print i2text(400000,4)
print i2text(1600000,4)

结果:

0000
0001
000c
0011
kib9
4b21

您的 URL 将采用 http://mydomain 的形式。 com/myapp/short/kib9

The algorithm URL shortening sites use is very simple:

  1. Store the URL and map it to it's sequence number.
  2. Convert the sequence number (id) to a fixed-length string.

Using just six lowercase letter for the second step will give you many more (24^6) combinations that the current application needs, and there's nothing preventing the use of a larger sequence at some point in time. You can use shorter sequences if you allow for numbers and/or uppercase letters.

The algorithm for the conversion is a base conversion (like when converting to hex), padding with whatever symbol represents zero. This is some Python code for the conversion:

LOWER = [chr(x + ord('a')) for x in range(25)]
DIGITS = [chr(x + ord('0')) for x in range(10)]
MAP = DIGITS + LOWER

def i2text(i, l):
        n = len(MAP)
        result = ''
        while i != 0:
                c = i % n
                result += MAP[c]
                i //= n
        padding = MAP[0]*l
        return (padding+result)[-l:]

print i2text(0,4)
print i2text(1,4)
print i2text(12,4)
print i2text(36,4)
print i2text(400000,4)
print i2text(1600000,4)

Results:

0000
0001
000c
0011
kib9
4b21

Your URLs would then be of the form http://mydomain.com/myapp/short/kib9.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文