接口说明
使用C1N短链接服务(c1n.cn)将原始链接快速转为短链接。
接口地址
https://c1n.cn/link/short
请求方式
POST
请求头:Headers
请前往C1N短链接服务(c1n.cn)「控制台」-「个人中心」-「短链配置」获取token
请求参数:Form 表单
响应数据:JSON格式
{
code: 0,
data: "https://c1n.cn/xxxxx",
msg: "成功"
}
//说明:code为0表示成功,其他情况表示生成失败。
代码示例
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' => '',
'domainName' => ''
];
$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;
?>
python
import requests
def short_url(long_url):
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'token': 'you_token' # 替换为您的token
}
data = {
'url': long_url,
'key': '',
'remark': '',
'expiryDate': '',
'domainName': ''
}
response = requests.post('https://c1n.cn/link/short', headers=headers, data=data)
response_data = response.json()
if response_data.get('code') == 0:
return response_data.get('data')
print(response_data.get('msg'))
res = short_url('https://example.com') # 替换为您要生成短链接的原始网址
print(res)
java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class ShortUrlDemo {
/**
* 使用前需要先引入fastjson依赖
* <dependency>
* <groupId>com.alibaba</groupId>
* <artifactId>fastjson</artifactId>
* <version>1.2.47</version>
* </dependency>
*/
public static void main(String[] args) {
String res = shortUrl("https://example.com"); //替换为您要生成短链接的原始网址
System.out.println(res);
}
public static String shortUrl(String longUrl) {
try {
URL url = new URL("https://c1n.cn/link/short");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("token", "you_token"); // 替换为您的token
connection.setDoOutput(true);
String requestBody = "url=" + URLEncoder.encode(longUrl, "UTF-8") + "&key=&remark=&expiryDate=&domainName=";
connection.getOutputStream().write(requestBody.getBytes(StandardCharsets.UTF_8));
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
connection.disconnect();
JSONObject obj = JSON.parseObject(response.toString());
if (obj.getInteger("code") == 0) {
return obj.getString("data");
}
System.out.println(obj);
return "";
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
}
JavaScript
function shortUrl(longUrl) {
var xhr = new XMLHttpRequest();
var url = 'https://c1n.cn/link/short';
var headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'token': 'your_token' // 替换为您的token
};
var data = 'url=' + encodeURIComponent(longUrl) + '&key=&remark=&expiryDate=&domainName=';
xhr.open('POST', url, true);
for (var key in headers) {
xhr.setRequestHeader(key, headers[key]);
}
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
var responseJson = JSON.parse(xhr.responseText);
if (responseJson.code === 0) {
console.log(responseJson.data);
} else {
console.log(responseJson.msg);
}
} else {
console.log('Error:', xhr.status);
}
}
};
xhr.send(data);
}
shortUrl('https://example.com'); // 替换为您要生成短链接的原始网址