PHP 短链接生成器 - 使用 API 生成短链接并显示样式
<?php
// 获取原始长链接
$longUrl = $_POST['long_url'];
// 发送请求生成短链接
$url = 'https://t.apii.cn/?url=' . urlencode($longUrl);
$result = file_get_contents($url);
// 解析返回的 JSON 数据
$data = json_decode($result, true);
// 判断生成状态
if ($data['code'] == 200) {
// 生成成功,显示短链接
$shortUrl = $data['url'];
echo '<p>您的短链接为:</p>';
echo '<a href='' . $shortUrl . ''>' . $shortUrl . '</a>';
} else {
// 生成失败,显示失败信息
$errorMsg = $data['msg'];
echo '<p>短链接生成失败,失败原因:' . $errorMsg . '</p>';
}
?>
<p>// 以下为样式代码,可根据需求自行修改</p>
<style>
body {
font-family: 'Microsoft Yahei', sans-serif;
background-color: #f5f5f5;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 5px;
}
h1 {
font-size: 2em;
font-weight: bold;
text-align: center;
margin-bottom: 20px;
}
form {
display: flex;
flex-direction: column;
align-items: center;
}
input[type="text"] {
width: 100%;
max-width: 600px;
padding: 10px;
margin-bottom: 20px;
border-radius: 5px;
border: 1px solid #ccc;
font-size: 1.2em;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border-radius: 5px;
border: none;
font-size: 1.2em;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #0069d9;
}
p {
font-size: 1.2em;
text-align: center;
margin-bottom: 20px;
}
a {
font-size: 1.5em;
font-weight: bold;
color: #007bff;
text-decoration: none;
}
</style>
<p>// 以下为 HTML 代码,可根据需求自行修改</p>
<div class="container">
<h1>短链接生成器</h1>
<form action="" method="post">
<input type="text" name="long_url" placeholder="请输入原始长链接">
<input type="submit" value="生成短链接">
</form>
</div>
原文地址: http://www.cveoy.top/t/topic/l46j 著作权归作者所有。请勿转载和采集!