返回列表 发帖
查看: 74|回复: 1

[求助] PHP运行之后无法正常显示页面

31

主题

99

回帖

146

积分

渐入佳境

贡献
1 点
金币
5 个
发表于 前天 19:53 | 查看全部 |阅读模式
  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. // === 全局作用域代码结束 ===
复制代码


我知道答案 回答被采纳将会获得1 贡献 已有1人回答
回复

使用道具 举报

29

主题

5602

回帖

1万

积分

应用开发者

贡献
598 点
金币
1442 个
QQ
发表于 前天 21:19 | 查看全部
你写的代码有问题呗
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 关注公众号
  • 有偿服务微信
  • 有偿服务QQ

手机版|小黑屋|Discuz! 官方交流社区 ( 皖ICP备16010102号 |皖公网安备34010302002376号 )|网站地图|star

GMT+8, 2025-7-5 18:05 , Processed in 0.038360 second(s), 8 queries , Redis On.

Powered by Discuz! W1.0 Licensed

Cpoyright © 2001-2025 Discuz! Team.

关灯 在本版发帖
有偿服务QQ
有偿服务微信
返回顶部
快速回复 返回顶部 返回列表