You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
1.8 KiB
75 lines
1.8 KiB
3 years ago
|
<?php
|
||
|
/*
|
||
|
* YOURLS API
|
||
|
*
|
||
|
* author:xiaoz<xiaoz.me>
|
||
|
* update:2022/03
|
||
|
*
|
||
|
*/
|
||
|
header('Content-Type:application/json; charset=utf-8');
|
||
|
//载入扩展配置文件
|
||
|
require('ext.php');
|
||
|
|
||
|
//默认域名,请改成你自己的域名,末尾不要带有/
|
||
|
define("DOMAIN","https://xw.al");
|
||
|
//每日单IP限制,就是每个IP每天可以生成多少个短链接,默认50个
|
||
|
define("LIMIT",50);
|
||
|
//过期时间设置
|
||
|
$today = strtotime(date("Y-m-d 23:59:59"),time());
|
||
|
$exp_time = $today - time();
|
||
|
//改成你自己的secret signature token,登录YOURLS后台 - 工具 - 安全的API调用 - 获取secret signature token
|
||
|
define("TOKEN",'');
|
||
|
|
||
|
//获取url
|
||
|
$url = @$_POST['url'];
|
||
|
//echo $url;
|
||
|
//exit;
|
||
|
//try{
|
||
|
// $url = urldecode($url);
|
||
|
//}
|
||
|
//catch(Exception $e){
|
||
|
// err_msg($e->getMessage());
|
||
|
//}
|
||
|
//如果URL不合法,直接停止
|
||
|
if( ! filter_var($url, FILTER_VALIDATE_URL) ) {
|
||
|
err_msg('URL不合法!');
|
||
|
}
|
||
|
|
||
|
|
||
|
//获取用户IP
|
||
|
$ip = getIP();
|
||
|
//设置key
|
||
|
$key = 'ip_'.str_replace('.','_',$ip);
|
||
|
|
||
|
//连接本地的 Redis 服务
|
||
|
$redis = new Redis();
|
||
|
$redis->connect('127.0.0.1', 6379);
|
||
|
|
||
|
//判断key是否存在
|
||
|
if( ! $redis->get($key) ) {
|
||
|
$redis->set($key, '0');
|
||
|
$redis->EXPIRE($key, $exp_time);
|
||
|
}
|
||
|
//如果key存在
|
||
|
if ( ($redis->get($key)) || ( $redis->get($key) === '0' ) ) {
|
||
|
//获取value
|
||
|
$value = intval($redis->get($key));
|
||
|
//如果value小于limit,则调用API
|
||
|
if( $value < LIMIT ) {
|
||
|
$api_url = DOMAIN.'/yourls-api.php?signature='.TOKEN.'&action=shorturl&format=json&url='.$url;
|
||
|
$re = curl($api_url);
|
||
|
$value = (string)($value + 1);
|
||
|
|
||
|
$redis->set($key, $value);
|
||
|
$redis->EXPIRE($key, $exp_time);
|
||
|
//
|
||
|
exit($re);
|
||
|
}
|
||
|
else{
|
||
|
err_msg('今日请求上限!');
|
||
|
}
|
||
|
}
|
||
|
//var_dump($redis->get($key));
|
||
|
//echo $key;
|
||
|
//echo $redis->get($key);
|