Files
MoYuBan/api/text.php
2025-11-18 14:18:28 +08:00

84 lines
3.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// 文案代理随机或指定来源yiyan/dujitang/renjian返回 { text }
header('Content-Type: application/json; charset=utf-8');
$type = strtolower($_GET['type'] ?? '');
$map = [
'dujitang'=> 'https://api.iyuns.com/api/dujitang',
'renjian' => 'https://api.iyuns.com/api/renjian',
'ktff'=> 'https://v.api.aa1.cn/api/api-wenan-ktff/index.php?type=5', // 新增文艺文案
'yiyan'=> 'https://v.api.aa1.cn/api/yiyan/index.php?type=json', // 新增纯文本一言
'wenan_anwei'=> 'https://v.api.aa1.cn/api/api-wenan-anwei/index.php?type=json', // 新增文艺文案
'wenan_gaoxiao'=> 'https://v.api.aa1.cn/api/api-wenan-gaoxiao/index.php?aa1=json', // 新增文艺文案
'wenan_shenhuifu'=> 'https://v.api.aa1.cn/api/api-wenan-shenhuifu/index.php?aa1=json', // 新增文艺文案
];
if (!$type || !isset($map[$type])) {
$keys = array_keys($map);
$type = $keys[array_rand($keys)];
}
$endpoint = $map[$type];
function fetch_json($url) {
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 8,
CURLOPT_TIMEOUT => 12,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_USERAGENT => 'DiscussionProxy/1.0',
]);
$body = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) { return null; }
$json = json_decode($body, true);
return is_array($json) ? $json : null;
}
$resp = fetch_json($endpoint);
$text = '';
if ($resp && ($resp['code'] ?? 0) == 200) {
$fields = ['yiyan','data','text','anwei','gaoxiao','shenhuifu'];
foreach ($fields as $k) {
$t = trim((string)($resp[$k] ?? ''));
if ($t !== '') { $text = $t; break; }
if (is_array($resp['data'] ?? null)) {
$t = trim((string)($resp['data'][$k] ?? ''));
if ($t !== '') { $text = $t; break; }
}
}
$text = preg_replace('/[`]/', '', $text);
}
if (!$text) {
// 远程失败时,回退到本地随机文案
$root = __DIR__ . '/../texts';
$pool = [];
if (is_dir($root)) {
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root));
foreach ($rii as $file) {
if ($file->isFile() && strtolower($file->getFilename()) === 'all.json') {
$json = @file_get_contents($file->getPathname());
if ($json) {
$arr = json_decode($json, true);
if (is_array($arr)) {
foreach ($arr as $it) {
$t = trim((string)($it['text'] ?? ''));
if ($t !== '') { $pool[] = $t; }
}
}
}
}
}
}
if (!empty($pool)) {
$text = $pool[array_rand($pool)];
$type = 'local';
} else {
// 如果本地也没有,则使用一个默认文案
$text = '今天也要元气满满!';
}
}
echo json_encode(['text' => $text, 'source' => $type], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);