api.php 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. // 引入类
  3. require_once('../inc/require.php');
  4. global $config;
  5. if($config['api']) {
  6. $url_c = new url();
  7. $opt = [];
  8. $opt['success'] = 'false';
  9. if(isset($_GET['url'])) {
  10. // 添加 HTTP 协议前缀
  11. if(!strstr($_GET['url'], 'http://') && !strstr($_GET['url'], 'https:')) $_GET['url'] = 'http://' . $_GET['url'];
  12. // 检测网址格式是否正确
  13. $is_link = preg_match('(http(|s)://([\w-]+\.)+[\w-]+(/)?)', $_GET['url']);
  14. // 判断条件
  15. if($_GET['url'] != '' && !strstr($_GET['url'], $_SERVER['HTTP_HOST']) && $is_link) {
  16. $opt['success'] = 'true';
  17. $opt['content']['url'] = $url_c->set_url($_GET['url'], $config['length']);
  18. } else if(strstr($_GET['url'], $_SERVER['HTTP_HOST'])) {
  19. $opt['content'] = '链接已经是短地址了。';
  20. } else if(!$is_link) {
  21. $opt['content'] = '请输入正确格式的网址。';
  22. }
  23. } else {
  24. $opt['content'] = '调用参数不能为空。';
  25. }
  26. // 输出
  27. echo json_encode($opt);
  28. }
  29. ?>