主力推荐
版本 v3.2 · PHP 7.1 – 8.3
火车头通用 AI 大模型翻译 PHP 插件
一键打通 DeepSeek、通义千问 Qwen-MT 与任意 OpenAI 兼容接口。内置 HTML 标签安全占位、万字长文按段落自动切片与 429/5xx 指数退避重试机制。
完整生产级 PHP 插件源码
将下方代码保存为 UTF-8(无 BOM)编码的 .php 文件,放入火车头安装目录的 Plugins/ 目录,填入你自己的 API Key 即可使用。
<?php
/**
* 石头在线翻译插件 v3.2 - 火车头通用 AI 大模型翻译 PHP 插件
* 适用接口:DeepSeek / 通义千问 Qwen-MT / 任何 OpenAI 兼容接口
* 特性:HTML 标签占位保护 + 段落智能切片 + 429/5xx 指数退避重试
*/
define('STONE_API_URL', 'https://api.deepseek.com/v1/chat/completions');
define('STONE_API_KEY', 'sk-请替换为你的真实密钥');
define('STONE_MODEL', 'deepseek-chat'); // 也可改为 qwen-mt-flash
define('STONE_TARGET', '简体中文');
define('STONE_CHUNK_SIZE', 2800);
function stone_request_llm($text, $isTitle = false) {
$sys = $isTitle
? '你是专业双语标题编辑。将输入标题翻译为简洁自然的' . STONE_TARGET . ',只输出最终标题,不要加引号或多余说明。'
: '你是专业翻译引擎。将用户输入精准翻译为流畅地道的' . STONE_TARGET . ',必须原样保留所有 [[NS_TAG_x]] 占位符和代码片段,不要添加任何解释或前言。';
$payload = json_encode(array(
'model' => STONE_MODEL,
'temperature' => 0.2,
'messages' => array(
array('role' => 'system', 'content' => $sys),
array('role' => 'user', 'content' => $text),
),
), JSON_UNESCAPED_UNICODE);
for ($i = 1; $i <= 3; $i++) {
$ch = curl_init(STONE_API_URL);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_CONNECTTIMEOUT => 15,
CURLOPT_TIMEOUT => 60,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Authorization: Bearer ' . STONE_API_KEY,
),
));
$body = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code === 200 && $body !== false) {
$res = json_decode($body, true);
if (!empty($res['choices'][0]['message']['content'])) {
return trim($res['choices'][0]['message']['content']);
}
}
if ($i < 3) {
sleep($i * 2);
}
}
return $text;
}
function stone_translate_html_safe($html) {
$tagMap = array();
$masked = preg_replace_callback('/<[^>]+>/u', function ($m) use (&$tagMap) {
$token = '[[NS_TAG_' . count($tagMap) . ']]';
$tagMap[$token] = $m[0];
return $token;
}, $html);
$lines = preg_split('/\R+/u', $masked);
$buf = '';
$parts = array();
foreach ($lines as $line) {
if (mb_strlen($buf . "\n" . $line, 'UTF-8') > STONE_CHUNK_SIZE && $buf !== '') {
$parts[] = stone_request_llm($buf, false);
$buf = $line;
} else {
$buf = ($buf === '') ? $line : ($buf . "\n" . $line);
}
}
if ($buf !== '') {
$parts[] = stone_request_llm($buf, false);
}
return strtr(implode("\n", $parts), $tagMap);
}
// 火车头标签数组处理入口
if (isset($LabelArray['标题']) && trim($LabelArray['标题']) !== '') {
$LabelArray['标题'] = stone_request_llm($LabelArray['标题'], true);
}
if (isset($LabelArray['内容']) && trim($LabelArray['内容']) !== '') {
$LabelArray['内容'] = stone_translate_html_safe($LabelArray['内容']);
}
echo serialize($LabelArray);
快速部署与火车头配置步骤
- 复制上方完整源码,使用 VS Code 或 Notepad++ 保存为
Stone_AI_Translate.php,务必选择 UTF-8(无 BOM) 编码。 - 修改顶部
STONE_API_URL、STONE_API_KEY与STONE_MODEL三个常量为你自己的接口凭证。 - 将文件放入火车头安装目录下的
Plugins/目录(不同版本可能命名为Plug/)。 - 打开火车头任务设置,进入「数据处理 / PHP 插件」选项卡,选中
Stone_AI_Translate.php并开启测试验证。 - 若待处理正文普遍超过 5000 字,建议保持
STONE_CHUNK_SIZE在 2400–2800 区间,避免触发网关超时。
核心设计亮点
HTML标签无损保真
长文语义自动分段
DeepSeek/Qwen-MT通吃
失败自动退避重试