使用API接口创建短链接【PHP版】

php 复制代码
<?php

function short_url($long_url)
{
    $headers = [
        'Content-Type: application/x-www-form-urlencoded',
        'token: your_token'  // 替换为您的token
    ];
    $data = [
        'url' => $long_url,
        'key' => '',
		'remark' => '',
		'expiryDate' => ''
    ];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://c1n.cn/link/short');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    $response_data = json_decode($response, true);

    if ($response_data['code'] == 0) {
        return $response_data['data'];
    }
    echo $response_data['msg'];
}

// 请确保您的PHP环境中已经安装了cURL库
$res = short_url('https://example.com');  // 替换为您要生成短链接的原始网址
echo $res;

?>

上述代码中使用到的token需要前往C1N短网址(c1n.cn)-控制台-我的信息进行查询。

C1N短网址(c1n.cn)还提供了丰富的短链接管理的API接口,只需进行简单的接口调用,就能将稳定好用的短链接系统接入到自己的产品中,极大地降低了开发成本。

相关推荐
捷米研发三部11 分钟前
ProfiNet转Modbus TCP协议转换网关实现西门子PLC与工控机通讯在智能交通系统的应用案例
网络·网络协议
dreamtm12335 分钟前
通俗理解 TCP 拥塞控制:像 “快递员看路况调速”,避免网络 “堵车”
网络·tcp/ip·php
李纲明4 小时前
WordPress外贸成品网站的免费获取渠道
vue·php
ASKED_20196 小时前
常用 Linux 命令大全(文件、网络、时间、进程、数据库、工具全覆盖)
linux·网络·数据库
我还可以再学点7 小时前
八股文面试攻略四:网络篇
网络·面试·职场和发展
q***76667 小时前
显卡(Graphics Processing Unit,GPU)架构详细解读
大数据·网络·架构
网安小白的进阶之路7 小时前
B模块 安全通信网络 第一门课 园区网实现与安全-1
网络·安全
dragoooon349 小时前
[Linux网络——Lesson2.socket套接字 && 简易UDP网络程序]
linux·网络·udp
独行soc9 小时前
2025年渗透测试面试题总结-254(题目+回答)
网络·python·安全·web安全·adb·渗透测试·安全狮