PHP Composer 包安装: composer require lunzi/dlj

使用文档、GitHub 地址

短链接生成接口

    请求地址

  • https://dlj.biz/api/create
  • 请求方式

  • POST
  • Content-Type:

  • application/json; charset=UTF-8
  • 请求参数(Body)

    字段 类型 是否必须 说明 示例
    url string 必须 长网址 "https://dlj.biz"

    请求示例

    • Java
    • curl
    • Javascript
    • PHP
    • Python
    
        import java.io.IOException;
        import java.io.OutputStreamWriter;
        import java.net.HttpURLConnection;
        import java.net.URL;
        import java.io.BufferedReader;
        import java.io.InputStreamReader;
    
        import com.google.gson.Gson;
        import com.google.gson.annotations.SerializedName;
    
        public class Dlj {
            final static String CREATE_API = "https://dlj.biz/api/create";
    
            class UrlResponse {
                @SerializedName("Code")
                private int code;
    
                @SerializedName("ErrMsg")
                private String errMsg;
    
                @SerializedName("LongUrl")
                private String longUrl;
    
                @SerializedName("ShortUrl")
                private String shortUrl;
    
                public int getCode() {
                    return code;
                }
    
                public void setCode(int code) {
                    this.code = code;
                }
    
                public String getErrMsg() {
                    return errMsg;
                }
    
                public void setErrMsg(String errMsg) {
                    this.errMsg = errMsg;
                }
    
                public String getLongUrl() {
                    return longUrl;
                }
    
                public void setLongUrl(String longUrl) {
                    this.longUrl = longUrl;
                }
    
                public String getShortUrl() {
                    return shortUrl;
                }
    
                public void setShortUrl(String shortUrl) {
                    this.shortUrl = shortUrl;
                }
            }
    
            /**
             * 创建短网址
             *
             * @param longUrl
             *            长网址:即原网址
             * @return  成功:短网址
             *          失败:返回空字符串
             */
            public static String createShortUrl(String longUrl) {
                String params = "{\"url\":\""+ longUrl + "\"}";
    
                BufferedReader reader = null;
                try {
                    // 创建连接
                    URL url = new URL(CREATE_API);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setDoOutput(true);
                    connection.setDoInput(true);
                    connection.setUseCaches(false);
                    connection.setInstanceFollowRedirects(true);
                    connection.setRequestMethod("POST"); // 设置请求方式
                    connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
    
                    // 发起请求
                    connection.connect();
                    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
                    out.append(params);
                    out.flush();
                    out.close();
    
                    // 读取响应
                    reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
                    String line;
                    String res = "";
                    while ((line = reader.readLine()) != null) {
                        res += line;
                    }
                    reader.close();
    
                    // 抽取生成短网址
                    UrlResponse urlResponse = new Gson().fromJson(res, UrlResponse.class);
                    if (urlResponse.getCode() == 0) {
                        return urlResponse.getShortUrl();
                    } else {
                        System.out.println(urlResponse.getErrMsg());
                    }
    
                    return ""; // TODO:自定义错误信息
                } catch (IOException e) {
                    // TODO
                    e.printStackTrace();
                }
                return ""; // TODO:自定义错误信息
            }
    
            public static void main(String[] args) {
                String res = createShortUrl("你的长网址");
                System.out.println(res);
    
            }
    
        }
                        
        curl -H "Content-Type:application/json" -X POST "https://dlj.biz/api/create" -d '{"url":"你的长网址"}'
                        
        var ajax = new XMLHttpRequest();
        var longUrl = '你的长网址';
    
        ajax.open('post','https://dlj.biz/api/create', true);
    
        ajax.setRequestHeader("Content-Type", "application/json");
    
        // 发送请求
        ajax.send(JSON.stringify({
            url: longUrl
        }));
    
        ajax.onreadystatechange = function () {
            if (ajax.readyState === 4 && ajax.status === 200) {
                // 获取缩短后的网址
                console.log(ajax.responseText);
            }
        }
                        
        $host = 'https://dlj.biz';
        $path = '/api/create';
        $url = $host . $path;
        $method = 'POST';
        $content_type = 'application/json';
    
        // TODO:设置待注册长网址
        $bodys = array('url'=>'你的长网址');
    
        // 配置headers
        $headers = array('Content-Type:'.$content_type);
    
        // 创建连接
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($bodys));
    
        // 发送请求
        $response = curl_exec($curl);
        curl_close($curl);
    
        // 读取响应
        var_dump($response);
                        
        #!/usr/bin/python
        # -*- coding: utf-8 -*-
        import requests
        import json
    
        host = 'https://dlj.biz'
        path = '/api/create'
        url = host + path
        method = 'POST'
        content_type = 'application/json'
    
        # TODO: 设置Token
        token = '你的token'
    
        # TODO:设置待创建的长网址
        bodys = {'url':'你的长网址'}
    
        # 配置headers
        headers = {'Content-Type':content_type, 'Token':token}
    
        # 发起请求
        response = requests.post(url=url, data=json.dumps(bodys), headers=headers)
    
        # 读取响应
        print(response.text)
                        

    响应结果示例

    {
        "Code": 0,
        "ShortUrl": "https://dlj.biz/abcde",
        "LongUrl": "https://dlj.biz/doc",
        "ErrMsg": ""
    }
        

    响应参数说明

    字段 类型 说明
    Code int 0:正常返回短网址
    int -1:短链接生成失败
    int -2:长链接不合法
    int -3:长链接存在安全隐患
    int -4:长链接插入数据库失败
    int -5:长链接在黑名单中,不允许注册
    ShortUrl string 短网址
    LongUrl string 长网址(原网址)
    ErrMsg string 错误信息

短网址还原接口

    请求地址

  • https://dlj.biz/api/query
  • 请求方式

  • POST
  • Content-Type:

  • application/json; charset=UTF-8
  • 请求参数(Body)

    字段 类型 是否必须 说明 示例
    shortUrl string 必须 短网址 "https://dlj.biz/abcde"

    请求示例

    • Java
    • curl
    • Javascript
    • PHP
    • Python
    
        import java.io.BufferedReader;
        import java.io.IOException;
        import java.io.InputStreamReader;
        import java.io.OutputStreamWriter;
        import java.net.HttpURLConnection;
        import java.net.URL;
    
        import com.google.gson.Gson;
        import com.google.gson.annotations.SerializedName;
    
        public class DljBiz {
            final static String CREATE_API = "https://dlj.biz/api/query";
    
            class UrlResponse {
                @SerializedName("Code")
                private int code;
    
                @SerializedName("ErrMsg")
                private String errMsg;
    
                @SerializedName("LongUrl")
                private String longUrl;
    
                @SerializedName("ShortUrl")
                private String shortUrl;
    
                public int getCode() {
                    return code;
                }
    
                public void setCode(int code) {
                    this.code = code;
                }
    
                public String getErrMsg() {
                    return errMsg;
                }
    
                public void setErrMsg(String errMsg) {
                    this.errMsg = errMsg;
                }
    
                public String getLongUrl() {
                    return longUrl;
                }
    
                public void setLongUrl(String longUrl) {
                    this.longUrl = longUrl;
                }
    
                public String getShortUrl() {
                    return shortUrl;
                }
    
                public void setShortUrl(String shortUrl) {
                    this.shortUrl = shortUrl;
                }
            }
            /**
             * 还原长网址短网址
             *
             * @param shortUrl 短网址
             * @return  成功:长网址
             *
             */
            public static String queryLongUrl(String shortUrl) {
                String params = "{\"shortUrl\":\""+ shortUrl + "\"}";
    
                BufferedReader reader = null;
                try {
                    // 创建连接
                    URL url = new URL(CREATE_API);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setDoOutput(true);
                    connection.setDoInput(true);
                    connection.setUseCaches(false);
                    connection.setInstanceFollowRedirects(true);
                    connection.setRequestMethod("POST"); // 设置请求方式
                    connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
                    connection.setRequestProperty("Token", TOKEN); // 设置发送数据的格式");
    
                    // 发起请求
                    connection.connect();
                    OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
                    out.append(params);
                    out.flush();
                    out.close();
    
                    // 读取响应
                    reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
                    String line;
                    String res = "";
                    while ((line = reader.readLine()) != null) {
                        res += line;
                    }
                    reader.close();
    
                    // 抽取生成长网址
                    UrlResponse urlResponse = new Gson().fromJson(res, UrlResponse.class);
                    if (urlResponse.getCode() == 0) {
                        return urlResponse.getLongUrl();
                    } else {
                        System.out.println(urlResponse.getErrMsg());
                    }
    
                    return ""; // TODO:自定义错误信息
                } catch (IOException e) {
                    // TODO
                    e.printStackTrace();
                }
                return ""; // TODO:自定义错误信息
            }
    
            public static void main(String[] args) {
                String res = queryLongUrl("你的短网址");
                System.out.println(res);
    
            }
    
        }
                        
        curl -H "Content-Type:application/json" -X POST "https://dlj.biz/api/query" -d '{"shortUrl":"你的短网址"}'
                        
        var ajax = new XMLHttpRequest();
        var token = '你的token';
        var shortUrl = '你的短网址';
    
        ajax.open('post','https://dlj.biz/api/query', true);
    
        ajax.setRequestHeader("Content-Type", "application/json");
        ajax.setRequestHeader("Token", token);
        // 发送请求
        ajax.send(JSON.stringify({
            shortUrl: shortUrl
        }));
    
        ajax.onreadystatechange = function () {
            if (ajax.readyState === 4 && ajax.status === 200) {
                // 短网址对应的长网址
                console.log(ajax.responseText);
            }
        }
                        
        $host = 'https://dlj.biz';
        $path = '/api/query';
        $url = $host . $path;
        $method = 'POST';
        $content_type = 'application/json';
    
        // TODO: 设置Token
        $token = '你的Token';
    
        // TODO:设置还原的短网址
        $bodys = array('shortUrl'=>'你的短网址');
    
        // 设置headers
        $headers = array('Content-Type:'.$content_type, 'Token:'.$token);
    
        // 创建连接
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($bodys));
    
        // 发送请求
        $response = curl_exec($curl);
        curl_close($curl);
    
        // 读取响应
        var_dump($response);
                        
        #!/usr/bin/python
        # -*- coding: utf-8 -*-
        import requests
        import json
    
        host = 'https://dlj.biz'
        path = '/api/query'
        url = host + path
        method = 'POST'
        content_type = 'application/json'
    
        # TODO: 设置Token
        token = '你的token'
    
        # TODO:设置待还原的短网址
        bodys = {'shortUrl':'你的短网址'}
    
        # 配置headers
        headers = {'Content-Type':content_type, 'Token':token}
    
        # 发起请求
        response = requests.post(url=url, data=json.dumps(bodys), headers=headers)
    
        # 读取响应
        print(response.text)
                        

    响应结果示例

    {
        "Code": 0,
        "ShortUrl": "https://dlj.biz/abcde",
        "LongUrl": "https://dlj.biz/doc",
        "ErrMsg": ""
    }
        

    响应参数说明

    字段 类型 说明
    Code int 0:正常返回短链接
    int -1:短链接不合法
    int -2:短链接不存在
    int -3:短链接对应的长链接不合法
    ShortUrl string 短网址
    LongUrl string 长网址(原网址)
    ErrMsg string 错误信息