使用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接口,只需进行简单的接口调用,就能将稳定好用的短链接系统接入到自己的产品中,极大地降低了开发成本。

相关推荐
合天网安实验室1 天前
记录一个免杀的php webshell demo
渗透测试·php·webshell·免杀
AnalogElectronic1 天前
linux 测试网络和端口是否连通的命令详解
linux·网络·php
Rust研习社1 天前
使用 Axum 构建高性能异步 Web 服务
开发语言·前端·网络·后端·http·rust
灰子学技术1 天前
Envoy HTTP 流量层面的 Metric 指标分析
网络·网络协议·http
上海云盾-小余1 天前
海外恶意 UDP 攻击溯源:分层封禁策略与业务兼容平衡方案
网络·网络协议·udp
智慧光迅AINOPOL1 天前
校园全光网建设指南:从架构到调优,打造稳定高体验校园网络
网络·全光网解决方案·全光网·酒店全光解决方案·泛住宿全光网解决方案
被摘下的星星1 天前
Internet 的域名系统:从“名字”到“地址”的翻译官
网络
跨境数据猎手1 天前
跨境独立站系统技术拆解(附带源码)
服务器·前端·php
淘矿人1 天前
从0到1:用Claude启动你的第一个项目
开发语言·人工智能·git·python·github·php·pygame
Diros1g1 天前
如何通过普通网线给另一个设备供网
网络·网络协议