Files
SimpleNav/admin/fetch_meta.php
T

37 lines
1.2 KiB
PHP
Raw Normal View History

<?php
/**
* 简航 SimpleNav —— 添加/编辑网站时自动抓取元信息(供后台 AJAX 调用)
* 流程:访问网址拿标题 → 解析页面/ manifest 里的高分辨率图标候选 → 择优下载到本地
* 返回:{ ok, name, short, icon, icon_url, candidates, url }
*/
declare(strict_types=1);
require_once __DIR__ . '/_auth.php';
require_login();
send_no_cache();
header('Content-Type: application/json; charset=utf-8');
$url = isset($_GET['url']) ? norm_url((string)$_GET['url']) : '';
if ($url === '') {
echo json_encode(['ok' => false, 'msg' => '缺少网址'], JSON_UNESCAPED_UNICODE);
exit;
}
$meta = fetch_site_meta($url);
$domain = domain_of($meta['final_url'] ?: $url);
$iconFile = download_best_icon($meta['icons'], $domain);
$name = $meta['name'] !== '' ? $meta['name'] : ($meta['title'] !== '' ? $meta['title'] : $domain);
echo json_encode([
'ok' => true,
'url' => $url,
'final_url' => $meta['final_url'],
'name' => $name,
'short' => $name,
'title' => $meta['title'],
'icon' => $iconFile,
'icon_url' => $iconFile !== '' ? icon_src($iconFile) : '',
'candidates' => count($meta['icons']),
], JSON_UNESCAPED_UNICODE);