短网址(Short URL):是指形式上比较简短并具有自动重定向到指定链接功能的网址。

短网址算法(algorithm): 一般有长网址MD5和数字、字符组合法。

本文介绍的短网址也是本站自建所用,开放了部分功能,短网址域名是:satic.cn,目前可以在开放平台中创建和还原短网址。

平台服务地址https://open.saintic.com/openservice/Shorturl/ 【包含服务说明、用户使用协议等】

其短网址https://satic.cn/ 【目前直接跳转到上述平台服务地址】

特性

项目计划

目前开放的功能很简单,只是[自定义]缩短网址、还原网址,短网址直接跳转,一些统计、自定义等高级功能暂未开放。

项目流程:github projects for shorturl,Todo和Done

问题反馈或功能建议

按照优先级:QQ群【博客右侧联系方式】->邮箱【staugur@sainti.com 或者 反馈页面 】->GitHub->评论

接口示例

考虑到Api文档的简陋性,这里放几个示例(同时包含缩短和还原),不同语言,请具体更改。

1.1 shell

# curl -s https://open.saintic.com/openservice/shorturl/v1/?Action=shorten -XPOST -d long_url=https://greasyfork.org/zh-CN/scripts/368427-花瓣网下载  #使用-d jid=自定义URL
{"msg": null, "code": 0, "data": {"short_url": "https://satic.io/f", "shorten": "f"}}

# curl -s https://open.saintic.com/openservice/shorturl/v1/?Action=reduction -XPOST -d short_url=https://satic.io/f |jq
{
  "msg": null,
  "code": 0,
  "data": {
    "status": "1",
    "shorten": "f",
    "safe": "1",
    "long_url": "https://greasyfork.org/zh-CN/scripts/368427-花瓣网下载",
    "realname": "0"
  }
}

1.2 python2.7

>>> import requests
>>> requests.post("https://open.saintic.com/openservice/shorturl/v1/?Action=shorten", data=dict(long_url="https://greasyfork.org/zh-CN/scripts/368427-堆糖网下载")).json()
{u'msg': None, u'code': 0, u'data': {u'short_url': u'https://satic.io/g', u'shorten': u'g'}}
>>> requests.post("https://open.saintic.com/openservice/shorturl/v1/?Action=reduction", data=dict(short_url="https://satic.io/g")).json()
{u'msg': None, u'code': 0, u'data': {u'status': u'1', u'long_url': u'https://greasyfork.org/zh-CN/scripts/368427-\u5806\u7cd6\u7f51\u4e0b\u8f7d', u'safe': u'1', u'shorten': u'g', u'realname': u'0'}}

1.3 php7.2

# cat testapi.php 
<?php
/**
 * POST 请求
 * @param  [string] $url      请求链接
 * @param  [array] $post_data 携带的参数
 * @return [array] $result    返回获取的内容
 */
function send_post($url, $post_data) {
    $postdata = http_build_query($post_data);
    $options = array(
        'http' => array(
            'method' => 'POST', //注意要大写
            'header' => 'Content-type:application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return $result;
}

$s_post = array(
    'long_url' => 'https://open.saintic.com/CrawlHuaban/Register'
);
$res = send_post('https://open.saintic.com/openservice/shorturl/v1/?Action=shorten', $s_post);
echo "缩短接口\n";
var_dump($res);

$res = json_decode($res);
$short_url = $res->{"data"}->{"short_url"};

$r_post = array(
    'short_url' => $short_url
);

$res = send_post('https://open.saintic.com/openservice/shorturl/v1/?Action=reduction', $r_post);
echo "还原接口\n";
var_dump($res);

?>

# php testapi.php 
缩短接口
string(86) "{"msg": null, "code": 0, "data": {"short_url": "https://satic.io/n", "shorten": "n"}}
"
还原接口
string(157) "{"msg": null, "code": 0, "data": {"status": "1", "shorten": "n", "safe": "1", "long_url": "https://open.saintic.com/CrawlHuaban/Register", "realname": "0"}}
"

·End·