久久久久av_欧美日韩一区二区在线_国产精品三区四区_日韩中字在线
Discuz! 官方交流社區
標題:
PHP運行之后無法正常顯示頁面
[打印本頁]
作者:
73327424
時間:
3 天前
標題:
PHP運行之后無法正常顯示頁面
<?php
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
global $_G;
class plugin_w0504_ym {
protected $config;
protected $api_key;
protected $api_url;
protected $api_model;
public function __construct() {
// 從插件配置獲取環境變量
$this->config = C::t('common_setting')->fetch('w0504_ym_config');
$this->api_key = $this->config['OPENAI_API_KEY'] ?? '';
$this->api_url = $this->config['OPENAI_API_BASE_URL'] ?? 'https://openrouter.ai/api/v1';
$this->api_model = $this->config['OPENAI_API_MODEL'] ?? 'deepseek/deepseek-r1:free';
}
// 公共方法供全局作用域調用
public function check_api_key() {
return !empty($this->api_key);
}
// 發送消息處理
public function send_message() {
$message = dhtmlspecialchars(trim($_POST['message']));
if (empty($message)) {
$this->ajax_error('消息不能為空');
}
$post_data = json_encode([
'model' => $this->api_model,
'messages' => [
['role' => 'user', 'content' => $message]
]
]);
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $this->api_url . '/chat/completions',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $this->api_key,
'Content-Type: application/json'
],
CURLOPT_POSTFIELDS => $post_data,
CURLOPT_TIMEOUT => 30
]);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
$this->ajax_error('API請求失敗: ' . curl_error($ch));
}
curl_close($ch);
if ($http_code != 200) {
$this->ajax_error('API返回錯誤: HTTP ' . $http_code);
}
$data = json_decode($response, true);
$reply = $data['choices'][0]['message']['content'] ?? '未收到有效回復';
// 返回JSON響應
header('Content-Type: application/json');
echo json_encode(['reply' => $reply]);
}
// 錯誤處理函數
protected function ajax_error($message) {
header('Content-Type: application/json');
echo json_encode(['error' => $message]);
exit();
}
}
// === 全局作用域代碼開始 ===
$plugin = new plugin_w0504_ym();
// 檢查API密鑰配置
if (empty($plugin->api_key)) {
die('管理員未配置API密鑰,請聯系管理員');
}
// 處理消息發送
if ($_GET['action'] == 'send' && $_POST['message']) {
$plugin->send_message();
exit();
}
// 顯示聊天界面
include template('w0504_ym:chat');
return;
// === 全局作用域代碼結束 ===
復制代碼
作者:
湖中沉
時間:
3 天前
你寫的代碼有問題唄
歡迎光臨 Discuz! 官方交流社區 (http://www.9999xn.com/)
Powered by Discuz! W1.0