进阶 yourls.org 2026-11-19 17:39:56 · 2 阅读
第14章 API 详解
API
YOURLS 自带一个 API 封装文件,位于安装根目录下的 yourls-api.php。
该 API 支持以下功能:
生成新的短链接或获取已有短链接,支持顺序编号或自定义关键词。
获取链接统计数据,如点击最多的链接、点击最少的链接、最新链接。
返回指定的输出格式:JSON、XML 或纯文本。
使用登录密码或无密码的安全机制进行身份验证。
甚至允许定义自定义的 API 请求和操作。
使用方法
参数需通过 GET 或 POST 方式发送至 https://your-own-domain-here.com/yourls-api.php。
提示:若使用 GET 方式,请记得对参数进行 URL 编码。
内置的 API 参数包括:
认证信息:
有效的用户名/密码组合,或
签名令牌(参见无密码 API 请求)
以下内置操作值之一:
shorturl 操作:获取链接的短 URL。
待缩短的 url
可选的自定义短链接关键词
可选的标题,用于覆盖 YOURLS 自动抓取
输出格式:jsonp、json、xml 或 simple 之一
expand 操作:获取短 URL 对应的长 URL。
待展开的 shorturl(可以是 abc 或 https://sho.rt/abc)
输出格式:jsonp、json、xml 或 simple 之一
url-stats 操作:获取单个短 URL 的统计数据。
目标 shorturl(可以是 abc 或 https://sho.rt/abc)
输出格式:jsonp、json 或 xml 之一
stats 操作:获取链接统计数据。
过滤器:top、bottom、rand 或 last 之一
限制数量(返回的最大链接数)
输出格式:jsonp、json 或 xml 之一
db-stats 操作:获取全局链接和点击总数。
输出格式:jsonp、json 或 xml 之一
version 操作:获取 YOURLS 版本。
输出格式:jsonp、json、xml 或 simple 之一
示例
使用 JavaScript(基于 jQuery)通过 GET 请求缩短 URL
JavaScript 请求
var api_url = 'https://sho.rt/yourls-api.php'
var response = $.get(
api_url,
{
username: 'your_username',
password: 'your_password',
action: 'shorturl',
format: 'json',
url: 'https://ozh.org/',
},
// 处理服务器响应的回调函数
function (data) {
// 此处处理数据,例如显示新的短链接
alert(data.shorturl)
},
)
JSON 返回结果 {
"url": {
"keyword": "ozh",
"url": "https:\/\/ozh.org",
"title": "Ozh RICHARD \u00ab ozh.org",
"date": "2014-10-24 16:01:39",
"ip": "127.0.0.1"
},
"status": "success",
"message": "https:\/\/ozh.org added to database",
"title": "Ozh RICHARD \u00ab ozh.org",
"shorturl": "https:\/\/sho.rt\/1f",
"statusCode": "200"
}
用 PHP 发送 POST 请求来展开短链接 PHP 请求 $username = 'your_username';
$password = 'your_password';
$api_url = 'https://sho.rt/yourls-api.php';
// Init the CURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request
curl_setopt($ch, CURLOPT_POSTFIELDS, array( // Data to POST
'shorturl' => 'ozh',
'format' => 'json',
'action' => 'expand',
'username' => $username,
'password' => $password
));
// Fetch and return content
$data = curl_exec($ch);
curl_close($ch);
// Do something with the result. Here, we echo the long URL
$data = json_decode( $data );
echo $data->longurl;
XML 返回
ozh
https://sho.rt/ozh
https://ozh.org/
success
200
示例文件 安装目录的根路径下自带一个 PHP 示例文件,演示了如何使用 API。 你可以复制并修改这个文件,以满足自己的需求。 扩展 API 通过插件可以轻松实现自定义的 API 动作。示例可参考插件列表:YOURLS/awesome。
var response = $.get(
api_url,
{
username: 'your_username',
password: 'your_password',
action: 'shorturl',
format: 'json',
url: 'https://ozh.org/',
},
// 处理服务器响应的回调函数
function (data) {
// 此处处理数据,例如显示新的短链接
alert(data.shorturl)
},
)
JSON 返回结果 {
"url": {
"keyword": "ozh",
"url": "https:\/\/ozh.org",
"title": "Ozh RICHARD \u00ab ozh.org",
"date": "2014-10-24 16:01:39",
"ip": "127.0.0.1"
},
"status": "success",
"message": "https:\/\/ozh.org added to database",
"title": "Ozh RICHARD \u00ab ozh.org",
"shorturl": "https:\/\/sho.rt\/1f",
"statusCode": "200"
}
用 PHP 发送 POST 请求来展开短链接 PHP 请求 $username = 'your_username';
$password = 'your_password';
$api_url = 'https://sho.rt/yourls-api.php';
// Init the CURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request
curl_setopt($ch, CURLOPT_POSTFIELDS, array( // Data to POST
'shorturl' => 'ozh',
'format' => 'json',
'action' => 'expand',
'username' => $username,
'password' => $password
));
// Fetch and return content
$data = curl_exec($ch);
curl_close($ch);
// Do something with the result. Here, we echo the long URL
$data = json_decode( $data );
echo $data->longurl;
XML 返回
示例文件 安装目录的根路径下自带一个 PHP 示例文件,演示了如何使用 API。 你可以复制并修改这个文件,以满足自己的需求。 扩展 API 通过插件可以轻松实现自定义的 API 动作。示例可参考插件列表:YOURLS/awesome。