Skip to content

短网址生成

接口简介

将长链接转化为短链接,方便用户分享。

接口信息

  • 接口地址:https://api-2.timecdn.cn/api/dwz
  • 请求方式:GET
  • 编码格式:UTF-8

请求参数

参数名类型必填说明
keystring用户Key
urlstring待转换的长链接
callbackstringjsonp回调函数名

⚠️ 特别注意

传入的长链接应该先使用 UrlEncode 编码,比如 JavaScript 中使用 encodeURIComponent() 方法。

请求示例

sh
curl "https://api-2.timecdn.cn/api/dwz?url={your url}" \
     -H 'Authorization: Bearer {your key}'
php
<?php
$url = urlencode('your url');
$key = 'your key';

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api-2.timecdn.cn/api/dwz?url=' . $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer ' . $key,
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
js
var url = encodeURIComponent('your url');
var key = 'your key';

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener('readystatechange', function() {
  if(this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open('GET', 'https://api-2.timecdn.cn/api/dwz?url=' + url);
xhr.setRequestHeader('Authorization', 'Bearer ' + key);

xhr.send();
js
const axios = require('axios');

let config = {
  method: 'get',
  maxBodyLength: Infinity,
  url: 'https://api-2.timecdn.cn/api/dwz?url={url}',
  headers: { 
    'Authorization': 'Bearer {key}'
  }
};

axios.request(config)
.then((response) => {
  console.log(JSON.stringify(response.data));
})
.catch((error) => {
  console.log(error);
});
python
import requests

url = "https://api-2.timecdn.cn/api/dwz?url={url}"

payload = {}
headers = {
  'Authorization': 'Bearer {key}'
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)
go
package main

import (
  "fmt"
  "net/http"
  "io/ioutil"
)

func main() {

  url := "https://api-2.timecdn.cn/api/dwz?url={url}"
  method := "GET"

  client := &http.Client {
  }
  req, err := http.NewRequest(method, url, nil)

  if err != nil {
    fmt.Println(err)
    return
  }
  req.Header.Add("Authorization", "Bearer {key}")

  res, err := client.Do(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer res.Body.Close()

  body, err := ioutil.ReadAll(res.Body)
  if err != nil {
    fmt.Println(err)
    return
  }
  fmt.Println(string(body))
}
java
OkHttpClient client = new OkHttpClient().newBuilder()
  .build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = RequestBody.create(mediaType, "");
Request request = new Request.Builder()
  .url("https://api-2.timecdn.cn/api/dwz?url={url}")
  .method("GET", body)
  .addHeader("Authorization", "Bearer {key}")
  .build();
Response response = client.newCall(request).execute();

返回参数

以下返回参数为 data 字段内容,完整返回参数请查看 接口地址与错误码 章节。
字段名类型说明
originalUrlstring待转换的长链接
shortUrlstring短链接
aliasstring短链接别名

返回示例

json
{
  "code": 200,
  "message": "请求成功",
  "data": {
    "originalUrl": "https://api.timecdn.cn",
    "shortUrl": "https://f8h.cn/klk7k3",
    "alias": "klk7k3"
  },
  "docs": "https://api.timecdn.cn/docs/dwz",
  "requestId": "a14b6994-d22d-4cd7-bd30-b8cad9be0290"
}

更新记录

时间更新内容
2022-07-25新增接口