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

相关推荐
tg-zm8899961 天前
2025返利商城源码/挂机自动收益可二开多语言/自定义返利比例/三级分销理财商城
java·mysql·php·laravel·1024程序员节
阿巴~阿巴~1 天前
JsonCpp:C++ JSON处理利器
linux·网络·c++·json·tcp·序列化和反序列化
j***29481 天前
IPV6公网暴露下的OPENWRT防火墙安全设置(只允许访问局域网中指定服务器指定端口其余拒绝)
服务器·安全·php
ao_lang1 天前
数据链路层
linux·服务器·网络
额呃呃1 天前
零拷贝I/O的核心概念
服务器·php·apache
执笔论英雄1 天前
【RL】python协程
java·网络·人工智能·python·设计模式
小兔薯了1 天前
7. LNMP-wordpress
android·运维·服务器·数据库·nginx·php
不过普通话一乙不改名1 天前
Linux 网络发包的极致之路:从普通模式到 AF_XDP ZeroCopy
linux·运维·网络
非凡的世界1 天前
Webman 可能是 PHP 最强框架没有之一
开发语言·php·workman
4***17541 天前
linux 网卡配置
linux·网络·php