久久久久av_欧美日韩一区二区在线_国产精品三区四区_日韩中字在线

Discuz! 官方交流社區

標題: PHP運行之后無法正常顯示頁面 [打印本頁]

作者: 73327424    時間: 3 天前
標題: PHP運行之后無法正常顯示頁面
  1. <?php
  2. if(!defined('IN_DISCUZ')) {
  3.     exit('Access Denied');
  4. }
  5. global $_G;

  6. class plugin_w0504_ym {

  7.     protected $config;
  8.     protected $api_key;
  9.     protected $api_url;
  10.     protected $api_model;

  11.     public function __construct() {
  12.         // 從插件配置獲取環境變量
  13.         $this->config = C::t('common_setting')->fetch('w0504_ym_config');
  14.         $this->api_key = $this->config['OPENAI_API_KEY'] ?? '';
  15.         $this->api_url = $this->config['OPENAI_API_BASE_URL'] ?? 'https://openrouter.ai/api/v1';
  16.         $this->api_model = $this->config['OPENAI_API_MODEL'] ?? 'deepseek/deepseek-r1:free';
  17.     }
  18.    
  19.     // 公共方法供全局作用域調用
  20.     public function check_api_key() {
  21.         return !empty($this->api_key);
  22.     }

  23.     // 發送消息處理
  24.     public function send_message() {
  25.         $message = dhtmlspecialchars(trim($_POST['message']));
  26.         if (empty($message)) {
  27.             $this->ajax_error('消息不能為空');
  28.         }

  29.         $post_data = json_encode([
  30.             'model' => $this->api_model,
  31.             'messages' => [
  32.                 ['role' => 'user', 'content' => $message]
  33.             ]
  34.         ]);

  35.         $ch = curl_init();
  36.         curl_setopt_array($ch, [
  37.             CURLOPT_URL => $this->api_url . '/chat/completions',
  38.             CURLOPT_RETURNTRANSFER => true,
  39.             CURLOPT_POST => true,
  40.             CURLOPT_HTTPHEADER => [
  41.                 'Authorization: Bearer ' . $this->api_key,
  42.                 'Content-Type: application/json'
  43.             ],
  44.             CURLOPT_POSTFIELDS => $post_data,
  45.             CURLOPT_TIMEOUT => 30
  46.         ]);

  47.         $response = curl_exec($ch);
  48.         $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  49.         
  50.         if (curl_errno($ch)) {
  51.             $this->ajax_error('API請求失敗: ' . curl_error($ch));
  52.         }
  53.         curl_close($ch);

  54.         if ($http_code != 200) {
  55.             $this->ajax_error('API返回錯誤: HTTP ' . $http_code);
  56.         }

  57.         $data = json_decode($response, true);
  58.         $reply = $data['choices'][0]['message']['content'] ?? '未收到有效回復';

  59.         // 返回JSON響應
  60.         header('Content-Type: application/json');
  61.         echo json_encode(['reply' => $reply]);
  62.     }
  63.    
  64.     // 錯誤處理函數
  65.     protected function ajax_error($message) {
  66.         header('Content-Type: application/json');
  67.         echo json_encode(['error' => $message]);
  68.         exit();
  69.     }
  70. }

  71. // === 全局作用域代碼開始 ===
  72. $plugin = new plugin_w0504_ym();

  73. // 檢查API密鑰配置
  74. if (empty($plugin->api_key)) {
  75.     die('管理員未配置API密鑰,請聯系管理員');
  76. }

  77. // 處理消息發送
  78. if ($_GET['action'] == 'send' && $_POST['message']) {
  79.     $plugin->send_message();
  80.     exit();
  81. }

  82. // 顯示聊天界面
  83. include template('w0504_ym:chat');
  84. return;
  85. // === 全局作用域代碼結束 ===
復制代碼



作者: 湖中沉    時間: 3 天前
你寫的代碼有問題唄




歡迎光臨 Discuz! 官方交流社區 (http://www.9999xn.com/) Powered by Discuz! W1.0