97久久国产亚洲精品超碰热,成人又色又爽的免费网站,色偷偷女人的天堂a,男女高潮喷水在线观看,国内精品一线二线三线区别在哪里

返回列表 發(fā)帖
查看: 279|回復(fù): 1

[求助] PHP運行之后無法正常顯示頁面

33

主題

102

回帖

151

積分

漸入佳境

貢獻
1 點
金幣
5 個
樓主
發(fā)表于 2025-7-3 19:53:33 | 只看樓主 |倒序瀏覽 |閱讀模式
  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.         // 從插件配置獲取環(huán)境變量
  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.     // 公共方法供全局作用域調(diào)用
  20.     public function check_api_key() {
  21.         return !empty($this->api_key);
  22.     }

  23.     // 發(fā)送消息處理
  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'] ?? '未收到有效回復(fù)';

  59.         // 返回JSON響應(yīng)
  60.         header('Content-Type: application/json');
  61.         echo json_encode(['reply' => $reply]);
  62.     }
  63.    
  64.     // 錯誤處理函數(shù)
  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密鑰,請聯(lián)系管理員');
  76. }

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

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


我知道答案 回答被采納將會獲得1 貢獻 已有1人回答

31

主題

5996

回帖

1萬

積分

應(yīng)用開發(fā)者

貢獻
624 點
金幣
1444 個
QQ
沙發(fā)
發(fā)表于 2025-7-3 21:19:20 | 只看Ta
你寫的代碼有問題唄
您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

  • 關(guān)注公眾號
  • 有償服務(wù)微信
  • 有償服務(wù)QQ

手機版|小黑屋|Discuz! 官方交流社區(qū) ( 皖I(lǐng)CP備16010102號 |皖公網(wǎng)安備34010302002376號 )|網(wǎng)站地圖|star

GMT+8, 2025-10-17 00:59 , Processed in 0.047154 second(s), 11 queries , Redis On.

Powered by Discuz! X5.0 Licensed

© 2001-2025 Discuz! Team.

關(guān)燈 在本版發(fā)帖
有償服務(wù)QQ
有償服務(wù)微信
返回頂部
快速回復(fù) 返回頂部 返回列表