首页
关于小站
朋友
壁纸
留言
时光之书
笔顺字帖
LayUI手册
Search
1
【PHP】PHPoffice/PHPSpreadsheet读取和写入Excel
1,785 阅读
2
【Layui】控制页面元素展示隐藏
1,602 阅读
3
【Git】No tracked branch configured for branch master or the branch doesn't exist.
1,560 阅读
4
【PHP】PHP实现JWT生成和验证
1,482 阅读
5
【composer】composer常用命令
1,356 阅读
默认分类
PHP
ThinkPHP
Laravel
面向对象
设计模式
算法
基础
网络安全
webman
Web
HTML
CSS
JavaScript
jQuery
Layui
VUE
uni-app
Database
MySQL
Redis
RabbitMQ
Nginx
Git
Linux
Soft Ware
Windows
网赚
Go
Docker
Elasticsearch
登录
Search
标签搜索
PHP
函数
方法
类
MySQL
ThinkPHP
JavaScript
OOP
Layui
Web
Server
Docker
PHPSpreadsheet
PHPoffice
Array
设计模式
Nginx
Git
排序算法
基础
小破孩
累计撰写
256
篇文章
累计收到
13
条评论
首页
栏目
默认分类
PHP
ThinkPHP
Laravel
面向对象
设计模式
算法
基础
网络安全
webman
Web
HTML
CSS
JavaScript
jQuery
Layui
VUE
uni-app
Database
MySQL
Redis
RabbitMQ
Nginx
Git
Linux
Soft Ware
Windows
网赚
Go
Docker
Elasticsearch
页面
关于小站
朋友
壁纸
留言
时光之书
笔顺字帖
LayUI手册
搜索到
94
篇与
的结果
2025-11-14
【Elasticsearch】Elasticsearch 与数据库实时同步方案
在宝塔环境中实现 Elasticsearch 与数据库的实时同步,主要有以下几种方案:一、同步方案概览方案实时性复杂度数据一致性适用场景应用层双写最高中等最终一致新建项目,可修改代码Logstash JDBC分钟级低延迟一致已有项目,增量同步Canal秒级高最终一致MySQL 数据库,要求高实时性数据库触发器实时高强一致数据库层面同步二、方案一:应用层双写(推荐)实现原理在业务代码中同时写入数据库和 Elasticsearch。PHP 实现示例<?php /** * 应用层双写方案 * 在写入数据库的同时写入 Elasticsearch */ require '/www/wwwroot/your-site/vendor/autoload.php'; use Elasticsearch\ClientBuilder; class DualWriteService { private $esClient; private $db; public function __construct() { // 初始化 ES 客户端 $this->esClient = ClientBuilder::create() ->setHosts(['localhost:9200']) ->build(); // 初始化数据库连接 $this->initDatabase(); } private function initDatabase() { $host = 'localhost'; $dbname = 'your_database'; $username = 'your_username'; $password = 'your_password'; try { $this->db = new PDO( "mysql:host={$host};dbname={$dbname};charset=utf8mb4", $username, $password, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ] ); } catch (PDOException $e) { throw new Exception("数据库连接失败: " . $e->getMessage()); } } /** * 添加文章(双写) */ public function addArticle($articleData) { $dbResult = $this->writeToDatabase($articleData); if ($dbResult['success']) { $esResult = $this->writeToElasticsearch($articleData, $dbResult['id']); if (!$esResult['success']) { // ES 写入失败,记录日志或加入重试队列 $this->logSyncFailure('add', $dbResult['id'], $articleData); } return $dbResult; } return $dbResult; } /** * 更新文章(双写) */ public function updateArticle($id, $updateData) { $dbResult = $this->updateDatabase($id, $updateData); if ($dbResult['success']) { $esResult = $this->updateElasticsearch($id, $updateData); if (!$esResult['success']) { $this->logSyncFailure('update', $id, $updateData); } } return $dbResult; } /** * 删除文章(双写) */ public function deleteArticle($id) { $dbResult = $this->deleteFromDatabase($id); if ($dbResult['success']) { $esResult = $this->deleteFromElasticsearch($id); if (!$esResult['success']) { $this->logSyncFailure('delete', $id); } } return $dbResult; } /** * 写入数据库 */ private function writeToDatabase($data) { try { $sql = "INSERT INTO articles (title, content, author, category, tags, status, created_at, updated_at) VALUES (:title, :content, :author, :category, :tags, :status, NOW(), NOW())"; $stmt = $this->db->prepare($sql); $stmt->execute([ ':title' => $data['title'], ':content' => $data['content'], ':author' => $data['author'], ':category' => $data['category'], ':tags' => is_array($data['tags']) ? implode(',', $data['tags']) : $data['tags'], ':status' => $data['status'] ?? 1 ]); $id = $this->db->lastInsertId(); return [ 'success' => true, 'id' => $id ]; } catch (Exception $e) { return [ 'success' => false, 'error' => $e->getMessage() ]; } } /** * 写入 Elasticsearch */ private function writeToElasticsearch($data, $id) { try { $esData = [ 'id' => (int)$id, 'title' => $data['title'], 'content' => $data['content'], 'author' => $data['author'], 'category' => $data['category'], 'tags' => is_array($data['tags']) ? $data['tags'] : explode(',', $data['tags']), 'status' => $data['status'] ?? 1, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s') ]; $params = [ 'index' => 'articles', 'id' => $id, 'body' => $esData ]; $response = $this->esClient->index($params); return ['success' => true]; } catch (Exception $e) { return [ 'success' => false, 'error' => $e->getMessage() ]; } } /** * 更新数据库 */ private function updateDatabase($id, $data) { try { $sql = "UPDATE articles SET title = :title, content = :content, author = :author, category = :category, tags = :tags, status = :status, updated_at = NOW() WHERE id = :id"; $stmt = $this->db->prepare($sql); $stmt->execute([ ':title' => $data['title'], ':content' => $data['content'], ':author' => $data['author'], ':category' => $data['category'], ':tags' => is_array($data['tags']) ? implode(',', $data['tags']) : $data['tags'], ':status' => $data['status'] ?? 1, ':id' => $id ]); return ['success' => true]; } catch (Exception $e) { return [ 'success' => false, 'error' => $e->getMessage() ]; } } /** * 更新 Elasticsearch */ private function updateElasticsearch($id, $data) { try { $updateData = [ 'title' => $data['title'], 'content' => $data['content'], 'author' => $data['author'], 'category' => $data['category'], 'tags' => is_array($data['tags']) ? $data['tags'] : explode(',', $data['tags']), 'status' => $data['status'] ?? 1, 'updated_at' => date('Y-m-d H:i:s') ]; $params = [ 'index' => 'articles', 'id' => $id, 'body' => [ 'doc' => $updateData ] ]; $response = $this->esClient->update($params); return ['success' => true]; } catch (Exception $e) { return [ 'success' => false, 'error' => $e->getMessage() ]; } } /** * 从数据库删除 */ private function deleteFromDatabase($id) { try { $sql = "DELETE FROM articles WHERE id = :id"; $stmt = $this->db->prepare($sql); $stmt->execute([':id' => $id]); return ['success' => true]; } catch (Exception $e) { return [ 'success' => false, 'error' => $e->getMessage() ]; } } /** * 从 Elasticsearch 删除 */ private function deleteFromElasticsearch($id) { try { $params = [ 'index' => 'articles', 'id' => $id ]; $response = $this->esClient->delete($params); return ['success' => true]; } catch (Exception $e) { return [ 'success' => false, 'error' => $e->getMessage() ]; } } /** * 记录同步失败 */ private function logSyncFailure($operation, $id, $data = null) { $logData = [ 'timestamp' => date('Y-m-d H:i:s'), 'operation' => $operation, 'id' => $id, 'data' => $data ]; file_put_contents( '/www/wwwlogs/es_sync_failures.log', json_encode($logData) . "\n", FILE_APPEND ); } /** * 重试失败的同步 */ public function retryFailedSyncs() { $logFile = '/www/wwwlogs/es_sync_failures.log'; if (!file_exists($logFile)) { return ['success' => true, 'message' => '无失败记录']; } $logs = file($logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $successCount = 0; $failCount = 0; foreach ($logs as $log) { $data = json_decode($log, true); try { switch ($data['operation']) { case 'add': $this->writeToElasticsearch($data['data'], $data['id']); break; case 'update': $this->updateElasticsearch($data['id'], $data['data']); break; case 'delete': $this->deleteFromElasticsearch($data['id']); break; } $successCount++; } catch (Exception $e) { $failCount++; } } // 清空日志文件 file_put_contents($logFile, ''); return [ 'success' => true, 'retried' => $successCount, 'failed' => $failCount ]; } } // 使用示例 $syncService = new DualWriteService(); // 添加文章 $article = [ 'title' => '测试文章标题', 'content' => '这是文章内容...', 'author' => '张三', 'category' => '技术', 'tags' => ['PHP', 'Elasticsearch', '搜索'], 'status' => 1 ]; $result = $syncService->addArticle($article); if ($result['success']) { echo "文章添加成功,ID: " . $result['id']; } else { echo "添加失败: " . $result['error']; } ?>三、方案二:Logstash JDBC 输入插件安装和配置 Logstash1. 在宝塔中安装 Logstash# 进入宝塔终端 cd /www/server # 下载 Logstash(版本需要与 ES 对应) wget https://artifacts.elastic.co/downloads/logstash/logstash-7.17.0-linux-x86_64.tar.gz # 解压 tar -zxvf logstash-7.17.0-linux-x86_64.tar.gz mv logstash-7.17.0 logstash # 下载 MySQL JDBC 驱动 cd logstash wget https://cdn.mysql.com/archives/mysql-connector-java-8.0/mysql-connector-java-8.0.28.tar.gz tar -zxvf mysql-connector-java-8.0.28.tar.gz2. 创建 Logstash 配置文件创建 /www/server/logstash/config/mysql-sync.conf:input { jdbc { # MySQL 连接配置 jdbc_connection_string => "jdbc:mysql://localhost:3306/your_database?useUnicode=true&characterEncoding=utf8&useSSL=false" jdbc_user => "your_username" jdbc_password => "your_password" # JDBC 驱动路径 jdbc_driver_library => "/www/server/logstash/mysql-connector-java-8.0.28/mysql-connector-java-8.0.28.jar" jdbc_driver_class => "com.mysql.cj.jdbc.Driver" # 启用分页 jdbc_paging_enabled => true jdbc_page_size => 50000 # 调度配置(每分钟执行一次) schedule => "* * * * *" # SQL 查询(增量同步) statement => "SELECT * FROM articles WHERE updated_at > :sql_last_value OR created_at > :sql_last_value" # 记录最后一次同步时间 use_column_value => true tracking_column_type => "timestamp" tracking_column => "updated_at" # 记录文件位置 last_run_metadata_path => "/www/server/logstash/last_run_metadata.txt" # 时区设置 jdbc_default_timezone => "Asia/Shanghai" } } filter { # 添加 @timestamp 字段 date { match => [ "updated_at", "yyyy-MM-dd HH:mm:ss" ] target => "@timestamp" } # 处理 tags 字段 if [tags] { mutate { gsub => [ "tags", ",", "|" ] split => { "tags" => "|" } } } # 移除不需要的字段 mutate { remove_field => ["@version", "@timestamp"] } } output { # 输出到 Elasticsearch elasticsearch { hosts => ["localhost:9200"] index => "articles" document_id => "%{id}" # 定义映射模板 template => "/www/server/logstash/config/articles-template.json" template_name => "articles" template_overwrite => true } # 可选:输出到文件用于调试 file { path => "/www/server/logstash/logs/mysql-sync.log" } }3. 创建 Elasticsearch 映射模板创建 /www/server/logstash/config/articles-template.json:{ "index_patterns": ["articles*"], "settings": { "number_of_shards": 1, "number_of_replicas": 0, "analysis": { "analyzer": { "ik_smart_analyzer": { "type": "custom", "tokenizer": "ik_smart" }, "ik_max_analyzer": { "type": "custom", "tokenizer": "ik_max_word" } } } }, "mappings": { "properties": { "id": {"type": "integer"}, "title": { "type": "text", "analyzer": "ik_smart_analyzer", "search_analyzer": "ik_smart_analyzer" }, "content": { "type": "text", "analyzer": "ik_max_analyzer", "search_analyzer": "ik_smart_analyzer" }, "author": {"type": "keyword"}, "category": {"type": "keyword"}, "tags": {"type": "keyword"}, "status": {"type": "integer"}, "created_at": {"type": "date", "format": "yyyy-MM-dd HH:mm:ss"}, "updated_at": {"type": "date", "format": "yyyy-MM-dd HH:mm:ss"} } } }4. 启动 Logstash# 创建日志目录 mkdir -p /www/server/logstash/logs # 启动 Logstash cd /www/server/logstash ./bin/logstash -f config/mysql-sync.conf # 或作为守护进程运行 nohup ./bin/logstash -f config/mysql-sync.conf > logs/logstash.log 2>&1 &5. 创建宝塔计划任务在宝塔面板中添加计划任务:# 命令 cd /www/server/logstash && ./bin/logstash -f config/mysql-sync.conf # 执行周期:每分钟四、方案三:Canal(MySQL Binlog 同步)安装和配置 Canal1. 安装 Canal# 下载 Canal cd /www/server wget https://github.com/alibaba/canal/releases/download/canal-1.1.6/canal.deployer-1.1.6.tar.gz tar -zxvf canal.deployer-1.1.6.tar.gz mv canal.deployer-1.1.6 canal2. 配置 MySQL 开启 Binlog-- 检查是否开启 binlog SHOW VARIABLES LIKE 'log_bin'; -- 如果没有开启,在 MySQL 配置文件中添加 -- /etc/my.cnf [mysqld] log-bin=mysql-bin binlog-format=ROW server_id=13. 创建 Canal 用户CREATE USER 'canal'@'%' IDENTIFIED BY 'canal_password'; GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%'; FLUSH PRIVILEGES;4. 配置 Canal编辑 /www/server/canal/conf/example/instance.properties:# 数据库配置 canal.instance.master.address=127.0.0.1:3306 canal.instance.dbUsername=canal canal.instance.dbPassword=canal_password canal.instance.connectionCharset=UTF-8 # 要监听的数据库和表 canal.instance.filter.regex=.*\\..* canal.instance.filter.black.regex= # 表映射 canal.instance.filter.regex=your_database.articles5. 创建 PHP Canal 客户端<?php /** * Canal PHP 客户端 * 监听 MySQL Binlog 并同步到 Elasticsearch */ class CanalClient { private $esClient; public function __construct() { $this->esClient = ClientBuilder::create() ->setHosts(['localhost:9200']) ->build(); } public function startSync() { // 连接 Canal 服务器 $client = new \Canal\Client(); $client->connect("127.0.0.1", 11111); $client->subscribe("example", "your_database.articles", ""); while (true) { $message = $client->get(100); if ($message->getEntries()) { foreach ($message->getEntries() as $entry) { if ($entry->getEntryType() == \Canal\Protocol\EntryType::ROWDATA) { $this->processRowChange($entry); } } } sleep(1); // 避免 CPU 过高 } } private function processRowChange($entry) { $rowChange = \Canal\Protocol\RowChange::parseFromString($entry->getStoreValue()); foreach ($rowChange->getRowDatasList() as $rowData) { switch ($rowChange->getEventType()) { case \Canal\Protocol\EventType::INSERT: $this->handleInsert($rowData); break; case \Canal\Protocol\EventType::UPDATE: $this->handleUpdate($rowData); break; case \Canal\Protocol\EventType::DELETE: $this->handleDelete($rowData); break; } } } private function handleInsert($rowData) { $afterColumns = $rowData->getAfterColumnsList(); $data = []; foreach ($afterColumns as $column) { $data[$column->getName()] = $column->getValue(); } $this->syncToElasticsearch('index', $data); } private function handleUpdate($rowData) { $afterColumns = $rowData->getAfterColumnsList(); $data = []; $id = null; foreach ($afterColumns as $column) { $data[$column->getName()] = $column->getValue(); if ($column->getName() == 'id') { $id = $column->getValue(); } } $this->syncToElasticsearch('update', $data, $id); } private function handleDelete($rowData) { $beforeColumns = $rowData->getBeforeColumnsList(); $id = null; foreach ($beforeColumns as $column) { if ($column->getName() == 'id') { $id = $column->getValue(); break; } } $this->syncToElasticsearch('delete', null, $id); } private function syncToElasticsearch($operation, $data = null, $id = null) { try { switch ($operation) { case 'index': $params = [ 'index' => 'articles', 'id' => $data['id'], 'body' => $this->transformData($data) ]; $this->esClient->index($params); break; case 'update': $params = [ 'index' => 'articles', 'id' => $id, 'body' => [ 'doc' => $this->transformData($data) ] ]; $this->esClient->update($params); break; case 'delete': $params = [ 'index' => 'articles', 'id' => $id ]; $this->esClient->delete($params); break; } echo "同步成功: {$operation} ID: {$id}\n"; } catch (Exception $e) { echo "同步失败: {$e->getMessage()}\n"; $this->logFailure($operation, $id, $data); } } private function transformData($data) { return [ 'id' => (int)$data['id'], 'title' => $data['title'], 'content' => $data['content'], 'author' => $data['author'], 'category' => $data['category'], 'tags' => !empty($data['tags']) ? explode(',', $data['tags']) : [], 'status' => (int)$data['status'], 'created_at' => $data['created_at'], 'updated_at' => $data['updated_at'] ]; } private function logFailure($operation, $id, $data) { // 记录同步失败日志 file_put_contents( '/www/wwwlogs/canal_sync_failures.log', json_encode([ 'timestamp' => date('Y-m-d H:i:s'), 'operation' => $operation, 'id' => $id, 'data' => $data ]) . "\n", FILE_APPEND ); } } // 启动 Canal 客户端 $canalClient = new CanalClient(); $canalClient->startSync(); ?>五、方案四:数据库触发器 + 消息队列1. 创建消息表CREATE TABLE es_sync_queue ( id BIGINT AUTO_INCREMENT PRIMARY KEY, table_name VARCHAR(100) NOT NULL, record_id BIGINT NOT NULL, operation ENUM('INSERT', 'UPDATE', 'DELETE') NOT NULL, sync_status TINYINT DEFAULT 0 COMMENT '0:未同步, 1:已同步, 2:同步失败', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, INDEX idx_status (sync_status), INDEX idx_created (created_at) );2. 创建数据库触发器-- INSERT 触发器 DELIMITER $$ CREATE TRIGGER after_article_insert AFTER INSERT ON articles FOR EACH ROW BEGIN INSERT INTO es_sync_queue (table_name, record_id, operation, sync_status) VALUES ('articles', NEW.id, 'INSERT', 0); END$$ DELIMITER ; -- UPDATE 触发器 DELIMITER $$ CREATE TRIGGER after_article_update AFTER UPDATE ON articles FOR EACH ROW BEGIN INSERT INTO es_sync_queue (table_name, record_id, operation, sync_status) VALUES ('articles', NEW.id, 'UPDATE', 0); END$$ DELIMITER ; -- DELETE 触发器 DELIMITER $$ CREATE TRIGGER after_article_delete AFTER DELETE ON articles FOR EACH ROW BEGIN INSERT INTO es_sync_queue (table_name, record_id, operation, sync_status) VALUES ('articles', OLD.id, 'DELETE', 0); END$$ DELIMITER ;3. PHP 消息队列处理器<?php /** * 消息队列同步处理器 */ class QueueSyncService { private $db; private $esClient; public function __construct() { $this->initDatabase(); $this->esClient = ClientBuilder::create() ->setHosts(['localhost:9200']) ->build(); } public function processQueue($batchSize = 100) { // 获取待同步的记录 $sql = "SELECT * FROM es_sync_queue WHERE sync_status = 0 ORDER BY id ASC LIMIT :limit FOR UPDATE"; $stmt = $this->db->prepare($sql); $stmt->bindValue(':limit', $batchSize, PDO::PARAM_INT); $stmt->execute(); $records = $stmt->fetchAll(); foreach ($records as $record) { $this->processRecord($record); } return count($records); } private function processRecord($record) { try { switch ($record['operation']) { case 'INSERT': case 'UPDATE': $this->syncUpsert($record); break; case 'DELETE': $this->syncDelete($record); break; } // 标记为已同步 $this->markAsSynced($record['id']); } catch (Exception $e) { // 标记为同步失败 $this->markAsFailed($record['id'], $e->getMessage()); } } private function syncUpsert($record) { // 从数据库获取最新数据 $data = $this->getRecordData($record['table_name'], $record['record_id']); if ($data) { $params = [ 'index' => $record['table_name'], 'id' => $record['record_id'], 'body' => $this->transformData($data) ]; $this->esClient->index($params); } } private function syncDelete($record) { $params = [ 'index' => $record['table_name'], 'id' => $record['record_id'] ]; $this->esClient->delete($params); } private function getRecordData($tableName, $recordId) { $sql = "SELECT * FROM {$tableName} WHERE id = :id"; $stmt = $this->db->prepare($sql); $stmt->execute([':id' => $recordId]); return $stmt->fetch(); } private function transformData($data) { // 根据表结构转换数据 if (isset($data['tags']) && !empty($data['tags'])) { $data['tags'] = explode(',', $data['tags']); } return $data; } private function markAsSynced($queueId) { $sql = "UPDATE es_sync_queue SET sync_status = 1 WHERE id = :id"; $stmt = $this->db->prepare($sql); $stmt->execute([':id' => $queueId]); } private function markAsFailed($queueId, $error) { $sql = "UPDATE es_sync_queue SET sync_status = 2, error_message = :error WHERE id = :id"; $stmt = $this->db->prepare($sql); $stmt->execute([ ':id' => $queueId, ':error' => $error ]); } public function retryFailed($batchSize = 50) { $sql = "SELECT * FROM es_sync_queue WHERE sync_status = 2 ORDER BY id ASC LIMIT :limit"; $stmt = $this->db->prepare($sql); $stmt->bindValue(':limit', $batchSize, PDO::PARAM_INT); $stmt->execute(); $records = $stmt->fetchAll(); $successCount = 0; foreach ($records as $record) { try { $this->processRecord($record); $successCount++; } catch (Exception $e) { // 记录重试失败 } } return $successCount; } } // 创建宝塔计划任务执行同步 $syncService = new QueueSyncService(); // 每次处理 100 条记录 $processed = $syncService->processQueue(100); echo "已处理 {$processed} 条同步记录"; // 重试失败的记录 $retried = $syncService->retryFailed(50); echo "重试成功 {$retried} 条记录"; ?>六、方案对比和选择建议方案选择指南场景推荐方案理由新建项目应用层双写代码可控,实时性最好已有项目,可修改代码应用层双写 + 全量初始化渐进式改造MySQL 数据库,要求高实时性Canal基于 Binlog,对业务无侵入简单同步,可接受分钟级延迟Logstash JDBC配置简单,稳定可靠数据库层面同步触发器 + 消息队列强一致性,对应用透明宝塔环境推荐配置对于大多数宝塔用户,我推荐:首选:应用层双写方案备选:Logstash JDBC 方案高级需求:Canal 方案性能优化建议// 批量操作优化 public function bulkSync($records) { $params = ['body' => []]; foreach ($records as $record) { $params['body'][] = [ 'index' => [ '_index' => 'articles', '_id' => $record['id'] ] ]; $params['body'][] = $this->transformData($record); } return $this->esClient->bulk($params); }选择最适合你项目需求和团队技术能力的方案。对于宝塔环境,应用层双写和 Logstash 方案相对更容易实施和维护。
2025年11月14日
3 阅读
0 评论
0 点赞
2025-11-13
【Elasticsearch】IK 分词器安装指南和PHP使用实例
IK 分词器安装指南是的,IK 分词器需要单独安装。 Elasticsearch 默认只提供标准分词器,对于中文分词效果不佳,IK 分词器是专门针对中文优化的分词插件。一、IK 分词器安装方法方法 1:手动安装(推荐)步骤 1:下载对应版本的 IK 分词器# 进入 Elasticsearch 插件目录 cd /www/server/elasticsearch/plugins # 创建 ik 目录 mkdir ik cd ik # 下载对应版本的 IK 分词器 # 注意:版本号必须与 Elasticsearch 版本一致! # 查看 ES 版本:curl -X GET "localhost:9200/" wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.17.0/elasticsearch-analysis-ik-7.17.0.zip # 解压 unzip elasticsearch-analysis-ik-7.17.0.zip # 删除 zip 文件 rm -f elasticsearch-analysis-ik-7.17.0.zip # 返回插件目录上级,确保目录结构正确 cd .. ls -la ik/步骤 2:重启 Elasticsearch# 通过宝塔面板重启 # 或者使用命令 systemctl restart elasticsearch步骤 3:验证安装# 验证 IK 分词器是否安装成功 curl -X GET "localhost:9200/_cat/plugins?v" # 测试分词效果 curl -X POST "localhost:9200/_analyze" -H 'Content-Type: application/json' -d' { "analyzer": "ik_smart", "text": "中华人民共和国" }'正常响应:{ "tokens" : [ { "token" : "中华人民共和国", "start_offset" : 0, "end_offset" : 7, "type" : "CN_WORD", "position" : 0 } ] }方法 2:使用 Elasticsearch 插件命令安装# 进入 Elasticsearch 安装目录 cd /www/server/elasticsearch # 使用插件管理器安装 ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.17.0/elasticsearch-analysis-ik-7.17.0.zip # 重启 Elasticsearch systemctl restart elasticsearch二、宝塔面板特有的安装方法通过宝塔终端操作打开宝塔面板 → 终端执行安装命令:# 切换到 elasticsearch 用户(宝塔环境可能需要) sudo -u elasticsearch /bin/bash # 进入 ES 插件目录 cd /www/server/elasticsearch/plugins # 下载并安装 IK 分词器 wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.17.0/elasticsearch-analysis-ik-7.17.0.zip unzip elasticsearch-analysis-ik-7.17.0.zip -d ik rm -f elasticsearch-analysis-ik-7.17.0.zip # 退出 elasticsearch 用户 exit # 重启 Elasticsearch 服务 systemctl restart elasticsearch宝塔文件管理器操作下载 IK 分词器:访问 IK 分词器 Releases下载对应版本的 ZIP 文件通过宝塔文件管理器上传:进入 /www/server/elasticsearch/plugins/创建 ik 文件夹上传 ZIP 文件到 ik 文件夹解压并删除 ZIP 文件重启服务:宝塔面板 → 软件商店 → Elasticsearch → 重启三、IK 分词器版本对应关系Elasticsearch 版本IK 分词器版本7.17.x7.17.07.16.x7.16.07.15.x7.15.07.14.x7.14.07.13.x7.13.07.12.x7.12.07.11.x7.11.07.10.x7.10.07.9.x7.9.0重要:必须确保版本一致,否则 Elasticsearch 无法启动!四、验证安装的完整脚本<?php /** * IK 分词器验证脚本 * 保存为 test_ik.php 并在浏览器访问 */ require '/www/wwwroot/your-site/vendor/autoload.php'; use Elasticsearch\ClientBuilder; class IKValidator { private $client; public function __construct() { $this->client = ClientBuilder::create() ->setHosts(['localhost:9200']) ->build(); } /** * 检查 IK 分词器是否安装 */ public function checkIKInstallation() { echo "<h2>IK 分词器安装验证</h2>"; // 1. 检查插件列表 echo "<h3>1. 已安装插件列表</h3>"; try { $plugins = $this->client->cat()->plugins(['v' => true]); echo "<pre>"; foreach ($plugins as $plugin) { echo $plugin . "\n"; if (strpos($plugin, 'analysis-ik') !== false) { echo "✅ IK 分词器已安装\n"; } } echo "</pre>"; } catch (Exception $e) { echo "❌ 获取插件列表失败: " . $e->getMessage() . "\n"; } // 2. 测试 IK 分词器功能 echo "<h3>2. IK 分词器功能测试</h3>"; $testTexts = [ "中华人民共和国", "北京大学生活动中心", " Elasticsearch中文分词器", "我喜欢吃苹果" ]; foreach ($testTexts as $text) { $this->testAnalyzer($text); } } /** * 测试分词器 */ private function testAnalyzer($text) { echo "<h4>测试文本: \"{$text}\"</h4>"; // 测试 ik_smart(智能分词) echo "<b>ik_smart 分词:</b><br>"; $this->analyzeText($text, 'ik_smart'); // 测试 ik_max_word(细粒度分词) echo "<b>ik_max_word 分词:</b><br>"; $this->analyzeText($text, 'ik_max_word'); // 测试 standard(标准分词器对比) echo "<b>standard 分词:</b><br>"; $this->analyzeText($text, 'standard'); echo "<hr>"; } private function analyzeText($text, $analyzer) { try { $params = [ 'body' => [ 'analyzer' => $analyzer, 'text' => $text ] ]; $response = $this->client->indices()->analyze($params); echo "分析器: <code>{$analyzer}</code><br>"; echo "分词结果: "; foreach ($response['tokens'] as $token) { echo "<span style='background:#e0e0e0; margin:2px; padding:2px 5px; border-radius:3px;'>" . $token['token'] . "</span> "; } echo "<br><br>"; } catch (Exception $e) { echo "❌ 分析器 <code>{$analyzer}</code> 不可用: " . $e->getMessage() . "<br><br>"; } } /** * 创建测试索引验证 IK 分词器 */ public function createTestIndex() { $indexName = 'test_ik_index'; // 删除已存在的测试索引 if ($this->client->indices()->exists(['index' => $indexName])) { $this->client->indices()->delete(['index' => $indexName]); } // 创建使用 IK 分词器的索引 $params = [ 'index' => $indexName, 'body' => [ 'settings' => [ 'analysis' => [ 'analyzer' => [ 'ik_smart_analyzer' => [ 'type' => 'custom', 'tokenizer' => 'ik_smart' ], 'ik_max_analyzer' => [ 'type' => 'custom', 'tokenizer' => 'ik_max_word' ] ] ] ], 'mappings' => [ 'properties' => [ 'title' => [ 'type' => 'text', 'analyzer' => 'ik_smart_analyzer', 'search_analyzer' => 'ik_smart_analyzer' ], 'content' => [ 'type' => 'text', 'analyzer' => 'ik_max_analyzer', 'search_analyzer' => 'ik_smart_analyzer' ] ] ] ] ]; try { $response = $this->client->indices()->create($params); echo "<h3>3. 测试索引创建</h3>"; echo "✅ 测试索引创建成功,IK 分词器配置正常<br>"; // 测试索引文档 $doc = [ 'title' => 'Elasticsearch中文分词器测试', 'content' => '这是一个关于IK分词器的功能测试文档,用于验证中文分词效果。' ]; $this->client->index([ 'index' => $indexName, 'body' => $doc ]); echo "✅ 测试文档索引成功<br>"; } catch (Exception $e) { echo "<h3>3. 测试索引创建</h3>"; echo "❌ 创建测试索引失败: " . $e->getMessage() . "<br>"; } } } // 执行验证 $validator = new IKValidator(); $validator->checkIKInstallation(); $validator->createTestIndex(); echo "<h2>验证完成</h2>"; echo "如果看到 IK 分词器正常工作,说明安装成功!"; ?>五、IK 分词器使用示例PHP 中使用 IK 分词器<?php require 'vendor/autoload.php'; use Elasticsearch\ClientBuilder; class IKSearchExample { private $client; public function __construct() { $this->client = ClientBuilder::create() ->setHosts(['localhost:9200']) ->build(); } /** * 创建使用 IK 分词器的索引 */ public createNewsIndex() { $params = [ 'index' => 'news', 'body' => [ 'settings' => [ 'number_of_shards' => 1, 'number_of_replicas' => 0, 'analysis' => [ 'analyzer' => [ 'ik_smart_analyzer' => [ 'type' => 'custom', 'tokenizer' => 'ik_smart' ], 'ik_max_analyzer' => [ 'type' => 'custom', 'tokenizer' => 'ik_max_word' ] ] ] ], 'mappings' => [ 'properties' => [ 'title' => [ 'type' => 'text', 'analyzer' => 'ik_smart_analyzer', // 索引时使用智能分词 'search_analyzer' => 'ik_smart_analyzer' // 搜索时使用智能分词 ], 'content' => [ 'type' => 'text', 'analyzer' => 'ik_max_analyzer', // 索引时使用最大分词 'search_analyzer' => 'ik_smart_analyzer' // 搜索时使用智能分词 ], 'tags' => [ 'type' => 'text', 'analyzer' => 'ik_max_analyzer' ], 'author' => [ 'type' => 'keyword' // 关键字类型,不分词 ] ] ] ] ]; return $this->client->indices()->create($params); } /** * 使用 IK 分词器进行搜索 */ public function searchWithIK($keyword) { $params = [ 'index' => 'news', 'body' => [ 'query' => [ 'multi_match' => [ 'query' => $keyword, 'fields' => ['title^3', 'content^2', 'tags^2'], 'type' => 'best_fields' ] ], 'highlight' => [ 'fields' => [ 'title' => [ 'pre_tags' => ['<em>'], 'post_tags' => ['</em>'] ], 'content' => [ 'pre_tags' => ['<em>'], 'post_tags' => ['</em>'], 'fragment_size' => 150, 'number_of_fragments' => 3 ] ] ] ] ]; return $this->client->search($params); } } // 使用示例 $search = new IKSearchExample(); // 创建索引 $search->createNewsIndex(); // 搜索示例 $result = $search->searchWithIK('北京大学生'); print_r($result['hits']['hits']); ?>六、常见问题解决问题 1:版本不匹配症状:Elasticsearch 启动失败解决:# 检查错误日志 tail -f /www/server/elasticsearch/logs/elasticsearch.log # 如果看到版本不匹配错误,下载正确版本 cd /www/server/elasticsearch/plugins/ik rm -rf * wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v[正确版本]/elasticsearch-analysis-ik-[正确版本].zip unzip elasticsearch-analysis-ik-[正确版本].zip问题 2:权限问题症状:无法创建插件目录或文件解决:# 确保 elasticsearch 用户有权限 chown -R elasticsearch:elasticsearch /www/server/elasticsearch/plugins/ik chmod -R 755 /www/server/elasticsearch/plugins/ik问题 3:IK 分词器不生效症状:安装成功但分词效果不对解决:# 重启 Elasticsearch systemctl restart elasticsearch # 验证插件是否加载 curl -X GET "localhost:9200/_cat/plugins?v" # 检查索引配置是否正确使用了 IK 分词器七、IK 分词器扩展词典(可选)如果需要自定义词典,可以编辑 IK 配置文件:# 进入 IK 配置目录 cd /www/server/elasticsearch/plugins/ik/config # 编辑主词典 vi IKAnalyzer.cfg.xml # 添加自定义词典 # 在 <entry key="ext_dict">custom.dic</entry> # 创建自定义词典文件 vi custom.dic # 添加自定义词汇,每行一个词 区块链 人工智能 大数据记得安装完成后一定要重启 Elasticsearch 服务!这样就完成了 IK 分词器的安装和配置,现在可以在 PHP 代码中使用更优秀的中文搜索功能了。
2025年11月13日
9 阅读
0 评论
0 点赞
2025-11-13
【Elasticsearch】 PHP 使用ES示例demo
1. 使用 Composer 安装# 安装 Composer(如果未安装) curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer # 创建项目目录 mkdir elasticsearch-php-demo cd elasticsearch-php-demo # 初始化 Composer composer init # 安装 Elasticsearch PHP 客户端 composer require elasticsearch/elasticsearch2. 手动安装(不使用 Composer)<?php // 手动引入 Elasticsearch 客户端 require_once 'path/to/elasticsearch-php/autoload.php';示例 1:基础连接和索引操作<?php require 'vendor/autoload.php'; use Elasticsearch\ClientBuilder; class ElasticsearchDemo { private $client; public function __construct() { // 创建 Elasticsearch 客户端 $this->client = ClientBuilder::create() ->setHosts(['localhost:9200']) // ES 服务器地址 ->setRetries(2) // 重试次数 ->build(); // 检查连接 if (!$this->checkConnection()) { throw new Exception("无法连接到 Elasticsearch"); } } /** * 检查 Elasticsearch 连接 */ private function checkConnection() { try { return $this->client->ping(); } catch (Exception $e) { echo "连接失败: " . $e->getMessage() . "\n"; return false; } } /** * 创建索引 */ public function createIndex($indexName) { $params = [ 'index' => $indexName, 'body' => [ 'settings' => [ 'number_of_shards' => 2, 'number_of_replicas' => 1 ], 'mappings' => [ 'properties' => [ 'title' => [ 'type' => 'text', 'analyzer' => 'standard' ], 'content' => [ 'type' => 'text', 'analyzer' => 'standard' ], 'author' => [ 'type' => 'keyword' ], 'publish_date' => [ 'type' => 'date' ], 'views' => [ 'type' => 'integer' ] ] ] ] ]; try { $response = $this->client->indices()->create($params); echo "索引 {$indexName} 创建成功\n"; return $response; } catch (Exception $e) { echo "创建索引失败: " . $e->getMessage() . "\n"; return false; } } /** * 删除索引 */ public function deleteIndex($indexName) { $params = ['index' => $indexName]; try { // 检查索引是否存在 if ($this->client->indices()->exists($params)) { $response = $this->client->indices()->delete($params); echo "索引 {$indexName} 删除成功\n"; return $response; } else { echo "索引 {$indexName} 不存在\n"; return false; } } catch (Exception $e) { echo "删除索引失败: " . $e->getMessage() . "\n"; return false; } } /** * 添加文档 */ public function addDocument($indexName, $document, $id = null) { $params = [ 'index' => $indexName, 'body' => $document ]; // 如果指定了 ID if ($id !== null) { $params['id'] = $id; } try { $response = $this->client->index($params); echo "文档添加成功,ID: " . $response['_id'] . "\n"; return $response; } catch (Exception $e) { echo "添加文档失败: " . $e->getMessage() . "\n"; return false; } } /** * 获取文档 */ public function getDocument($indexName, $id) { $params = [ 'index' => $indexName, 'id' => $id ]; try { $response = $this->client->get($params); return $response; } catch (Exception $e) { echo "获取文档失败: " . $e->getMessage() . "\n"; return false; } } /** * 更新文档 */ public function updateDocument($indexName, $id, $updateData) { $params = [ 'index' => $indexName, 'id' => $id, 'body' => [ 'doc' => $updateData ] ]; try { $response = $this->client->update($params); echo "文档更新成功\n"; return $response; } catch (Exception $e) { echo "更新文档失败: " . $e->getMessage() . "\n"; return false; } } /** * 删除文档 */ public function deleteDocument($indexName, $id) { $params = [ 'index' => $indexName, 'id' => $id ]; try { $response = $this->client->delete($params); echo "文档删除成功\n"; return $response; } catch (Exception $e) { echo "删除文档失败: " . $e->getMessage() . "\n"; return false; } } /** * 搜索文档 */ public function search($indexName, $query) { $params = [ 'index' => $indexName, 'body' => [ 'query' => $query ] ]; try { $response = $this->client->search($params); return $response; } catch (Exception $e) { echo "搜索失败: " . $e->getMessage() . "\n"; return false; } } /** * 批量插入文档 */ public function bulkInsert($indexName, $documents) { $params = ['body' => []]; foreach ($documents as $document) { $params['body'][] = [ 'index' => [ '_index' => $indexName ] ]; $params['body'][] = $document; } try { $response = $this->client->bulk($params); echo "批量插入完成,处理了 " . count($documents) . " 个文档\n"; return $response; } catch (Exception $e) { echo "批量插入失败: " . $e->getMessage() . "\n"; return false; } } } // 使用示例 try { $es = new ElasticsearchDemo(); // 创建索引 $es->createIndex('blog'); // 添加单个文档 $document = [ 'title' => 'Elasticsearch PHP 客户端使用指南', 'content' => '这是一篇关于如何使用 PHP 操作 Elasticsearch 的详细教程。', 'author' => '张三', 'publish_date' => '2023-10-01', 'views' => 100 ]; $es->addDocument('blog', $document, '1'); // 批量插入文档 $documents = [ [ 'title' => 'PHP 开发技巧', 'content' => '分享一些 PHP 开发中的实用技巧和最佳实践。', 'author' => '李四', 'publish_date' => '2023-10-02', 'views' => 150 ], [ 'title' => 'Linux 系统管理', 'content' => 'Linux 系统管理的基本命令和操作指南。', 'author' => '王五', 'publish_date' => '2023-10-03', 'views' => 200 ] ]; $es->bulkInsert('blog', $documents); // 搜索文档 $query = [ 'match' => [ 'title' => 'PHP' ] ]; $searchResult = $es->search('blog', $query); echo "搜索到 " . $searchResult['hits']['total']['value'] . " 个结果:\n"; foreach ($searchResult['hits']['hits'] as $hit) { echo "- " . $hit['_source']['title'] . " (作者: " . $hit['_source']['author'] . ")\n"; } } catch (Exception $e) { echo "错误: " . $e->getMessage() . "\n"; } ?>示例 2:高级搜索功能<?php require 'vendor/autoload.php'; use Elasticsearch\ClientBuilder; class AdvancedSearchDemo { private $client; public function __construct() { $this->client = ClientBuilder::create() ->setHosts(['localhost:9200']) ->build(); } /** * 多字段搜索 */ public function multiFieldSearch($indexName, $keyword) { $params = [ 'index' => $indexName, 'body' => [ 'query' => [ 'multi_match' => [ 'query' => $keyword, 'fields' => ['title^2', 'content', 'author'], // title 字段权重为 2 'type' => 'best_fields' ] ], 'highlight' => [ 'fields' => [ 'title' => new \stdClass(), 'content' => new \stdClass() ] ] ] ]; return $this->client->search($params); } /** * 布尔搜索 */ public function boolSearch($indexName, $must = [], $should = [], $must_not = []) { $boolQuery = []; if (!empty($must)) { $boolQuery['must'] = $must; } if (!empty($should)) { $boolQuery['should'] = $should; } if (!empty($must_not)) { $boolQuery['must_not'] = $must_not; } $params = [ 'index' => $indexName, 'body' => [ 'query' => [ 'bool' => $boolQuery ] ] ]; return $this->client->search($params); } /** * 范围查询 */ public function rangeSearch($indexName, $field, $gte = null, $lte = null) { $range = []; if ($gte !== null) $range['gte'] = $gte; if ($lte !== null) $range['lte'] = $lte; $params = [ 'index' => $indexName, 'body' => [ 'query' => [ 'range' => [ $field => $range ] ] ] ]; return $this->client->search($params); } /** * 聚合查询 */ public function aggregationSearch($indexName) { $params = [ 'index' => $indexName, 'body' => [ 'size' => 0, // 不需要返回具体文档 'aggs' => [ 'authors' => [ 'terms' => [ 'field' => 'author.keyword', 'size' => 10 ] ], 'total_views' => [ 'sum' => [ 'field' => 'views' ] ], 'avg_views' => [ 'avg' => [ 'field' => 'views' ] ] ] ] ]; return $this->client->search($params); } } // 使用示例 $searchDemo = new AdvancedSearchDemo(); // 多字段搜索 $result = $searchDemo->multiFieldSearch('blog', 'PHP'); print_r($result['hits']); // 布尔搜索 $boolResult = $searchDemo->boolSearch('blog', [ // must 条件 ['match' => ['author' => '张三']], ['range' => ['views' => ['gte' => 50]]] ], [ // should 条件 ['match' => ['title' => '教程']], ['match' => ['content' => '指南']] ] ); // 聚合查询 $aggResult = $searchDemo->aggregationSearch('blog'); echo "总浏览量: " . $aggResult['aggregations']['total_views']['value'] . "\n"; echo "平均浏览量: " . $aggResult['aggregations']['avg_views']['value'] . "\n"; ?>示例 3:完整的博客搜索应用<?php require 'vendor/autoload.php'; use Elasticsearch\ClientBuilder; class BlogSearch { private $client; private $indexName = 'blog_posts'; public function __construct() { $this->client = ClientBuilder::create() ->setHosts(['localhost:9200']) ->build(); $this->createBlogIndex(); } private function createBlogIndex() { // 检查索引是否存在,不存在则创建 if (!$this->client->indices()->exists(['index' => $this->indexName])) { $params = [ 'index' => $this->indexName, 'body' => [ 'settings' => [ 'number_of_shards' => 1, 'number_of_replicas' => 1, 'analysis' => [ 'analyzer' => [ 'ik_smart_analyzer' => [ 'type' => 'custom', 'tokenizer' => 'ik_smart' ] ] ] ], 'mappings' => [ 'properties' => [ 'title' => [ 'type' => 'text', 'analyzer' => 'ik_smart_analyzer' ], 'content' => [ 'type' => 'text', 'analyzer' => 'ik_smart_analyzer' ], 'author' => [ 'type' => 'keyword' ], 'tags' => [ 'type' => 'keyword' ], 'category' => [ 'type' => 'keyword' ], 'publish_date' => [ 'type' => 'date' ], 'views' => [ 'type' => 'integer' ], 'status' => [ 'type' => 'keyword' ] ] ] ] ]; $this->client->indices()->create($params); } } public function addBlogPost($post) { $params = [ 'index' => $this->indexName, 'body' => $post ]; return $this->client->index($params); } public function searchPosts($keyword, $filters = [], $page = 1, $pageSize = 10) { $from = ($page - 1) * $pageSize; $query = [ 'bool' => [ 'must' => [], 'filter' => [] ] ]; // 关键词搜索 if (!empty($keyword)) { $query['bool']['must'][] = [ 'multi_match' => [ 'query' => $keyword, 'fields' => ['title^3', 'content^2', 'tags^2'], 'type' => 'best_fields' ] ]; } // 过滤器 foreach ($filters as $field => $value) { if (!empty($value)) { $query['bool']['filter'][] = [ 'term' => [$field => $value] ]; } } $params = [ 'index' => $this->indexName, 'body' => [ 'from' => $from, 'size' => $pageSize, 'query' => $query, 'sort' => [ ['publish_date' => ['order' => 'desc']], ['views' => ['order' => 'desc']] ], 'highlight' => [ 'fields' => [ 'title' => [ 'number_of_fragments' => 0 ], 'content' => [ 'fragment_size' => 150, 'number_of_fragments' => 3 ] ] ] ] ]; $response = $this->client->search($params); return [ 'total' => $response['hits']['total']['value'], 'posts' => $response['hits']['hits'], 'took' => $response['took'] ]; } public function getRelatedPosts($postId, $limit = 5) { // 先获取当前文章 $currentPost = $this->client->get([ 'index' => $this->indexName, 'id' => $postId ]); $tags = $currentPost['_source']['tags'] ?? []; if (empty($tags)) { return []; } $params = [ 'index' => $this->indexName, 'body' => [ 'size' => $limit, 'query' => [ 'bool' => [ 'must' => [ 'terms' => [ 'tags' => $tags ] ], 'must_not' => [ 'term' => [ '_id' => $postId ] ] ] ], 'sort' => [ ['views' => ['order' => 'desc']], ['publish_date' => ['order' => 'desc']] ] ] ]; $response = $this->client->search($params); return $response['hits']['hits']; } } // 使用示例 $blogSearch = new BlogSearch(); // 添加示例文章 $samplePosts = [ [ 'title' => 'PHP 8 新特性详解', 'content' => 'PHP 8 引入了许多令人兴奋的新特性,如命名参数、联合类型、属性等...', 'author' => '技术达人', 'tags' => ['PHP', '编程', '后端'], 'category' => '编程语言', 'publish_date' => '2023-09-15', 'views' => 245, 'status' => 'published' ], [ 'title' => 'Elasticsearch 入门教程', 'content' => '学习如何使用 Elasticsearch 进行全文搜索和数据聚合...', 'author' => '搜索专家', 'tags' => ['Elasticsearch', '搜索', '数据库'], 'category' => '数据库', 'publish_date' => '2023-09-20', 'views' => 189, 'status' => 'published' ] ]; foreach ($samplePosts as $post) { $blogSearch->addBlogPost($post); } // 搜索文章 $result = $blogSearch->searchPosts('PHP 教程', ['category' => '编程语言'], 1, 10); echo "找到 {$result['total']} 篇文章,耗时 {$result['took']} 毫秒\n"; foreach ($result['posts'] as $post) { echo "标题: " . $post['_source']['title'] . "\n"; if (isset($post['highlight'])) { echo "高亮: " . implode('...', $post['highlight']['content'] ?? []) . "\n"; } echo "---\n"; } ?>
2025年11月13日
11 阅读
0 评论
0 点赞
2025-09-05
【PHP】境外微信支付生成平台证书 - wechatpay
使用方法 $wechatPayPlatformCertInstance = new \addons\unidrink\library\pay\overseas\wechat\Certificate(); try { $platformCerts = $wechatPayPlatformCertInstance->getPlatformCerts(); if (!empty($platformCerts)) { // 获取最新的证书 $latestCert = $platformCerts[0]; // 返回成功信息 return json([ 'code' => 1, 'msg' => '平台证书生成成功', 'data' => [ 'serial_no' => $latestCert['serial_no'], 'effective_time' => $latestCert['effective_time'], 'expire_time' => $latestCert['expire_time'], 'cert_saved_path' => $wechatPayPlatformCertInstance->getCertPath() . $latestCert['serial_no'] . '.pem', ] ]); } else { throw new Exception('未获取到平台证书'); } } catch (Exception $e) { // 错误处理 return json([ 'code' => 0, 'msg' => '平台证书生成失败: ' . $e->getMessage(), 'data' => [] ]); } 下面是类 <?php namespace addons\unidrink\library\pay\overseas\wechat; use think\Exception; use think\Log; use WeChatPay\Crypto\Rsa; use WeChatPay\Formatter; use WeChatPay\Util\PemUtil; class Certificate { // 配置参数 private $config; // 证书保存路径 private $certPath; // 平台证书列表 private $platformCerts = []; /** * 构造函数 * @param array $config 微信支付配置 */ public function __construct($config = []) { // 默认配置 $defaultConfig = [ 'mch_id' => '', // 商户号 'serial_no' => '', // 商户API证书序列号 'private_key' => ROOT_PATH . 'public/cert/apiclient_key.pem', // 商户私钥内容或路径 'api_v3_key' => '', // APIv3密钥 'cert_path' => ROOT_PATH . 'public/cert/wechatpay/', // 证书保存目录 'ca_cert_path' => ROOT_PATH . 'public/cert/cacert.pem' // CA证书路径 ]; $this->config = array_merge($defaultConfig, $config); $this->certPath = $this->config['cert_path']; // 验证APIv3密钥长度 $this->validateApiV3Key(); // 创建证书目录并确保权限 $this->prepareDirectories(); } /** * 准备目录并确保权限 */ private function prepareDirectories() { // 创建证书目录 if (!is_dir($this->certPath)) { if (!mkdir($this->certPath, 0755, true)) { throw new Exception('无法创建证书目录: ' . $this->certPath); } } // 检查证书目录权限 if (!is_writable($this->certPath)) { throw new Exception('证书目录不可写: ' . $this->certPath); } // 确保CA证书目录可写 $caCertDir = dirname($this->config['ca_cert_path']); if (!is_dir($caCertDir)) { if (!mkdir($caCertDir, 0755, true)) { throw new Exception('无法创建CA证书目录: ' . $caCertDir); } } if (!is_writable($caCertDir)) { throw new Exception('CA证书目录不可写: ' . $caCertDir); } } /** * 验证APIv3密钥长度是否正确 * APIv3密钥必须是32位字符串 * @throws Exception */ private function validateApiV3Key() { // 移除可能存在的空格 $apiV3Key = trim($this->config['api_v3_key']); // 重新设置清理后的密钥 $this->config['api_v3_key'] = $apiV3Key; // 检查长度 $keyLength = strlen($apiV3Key); if ($keyLength !== 32) { throw new Exception("APIv3密钥长度不正确,必须是32位,当前为{$keyLength}位"); } } /** * 获取平台证书 * @return array 平台证书列表 */ public function getPlatformCerts() { try { // 如果有缓存的证书且未过期,直接返回 $cachedCerts = $this->getCachedCerts(); if (!empty($cachedCerts)) { Log::info('使用缓存的平台证书,共' . count($cachedCerts) . '个'); return $cachedCerts; } Log::info('缓存的平台证书不存在或已过期,将从API获取'); // 从微信支付API获取证书 $this->fetchPlatformCerts(); // 缓存证书 $this->cacheCerts(); return $this->platformCerts; } catch (Exception $e) { Log::error('获取微信支付平台证书失败: ' . $e->getMessage() . ',堆栈信息: ' . $e->getTraceAsString()); throw new Exception('获取平台证书失败: ' . $e->getMessage()); } } /** * 从微信支付API获取平台证书 */ private function fetchPlatformCerts() { // 构建请求参数 $timestamp = time(); $nonce = $this->generateNonce(); $method = 'GET'; // $url = '/v3/certificates'; // 中国大陆境内 $url = '/v3/global/certificates'; // 注意:全球版API路径 $body = ''; Log::info("准备请求微信支付证书API: URL={$url}, 时间戳={$timestamp}, 随机串={$nonce}"); // 生成签名 $signature = $this->generateSignature($method, $url, $timestamp, $nonce, $body); // 构建授权头 $token = sprintf( 'WECHATPAY2-SHA256-RSA2048 mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"', $this->config['mch_id'], $nonce, $timestamp, $this->config['serial_no'], $signature ); // 发送请求 $headers = [ 'Accept: application/json', 'User-Agent: FastAdmin/WechatPay', 'Authorization: ' . $token, ]; // 根据商户号判断使用国内还是国际API // $apiDomain = 'https://api.mch.weixin.qq.com'; $apiDomain = 'https://apihk.mch.weixin.qq.com'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $apiDomain . $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 检查私钥文件是否存在 $privateKeyPath = $this->config['private_key']; if (!file_exists($privateKeyPath)) { throw new Exception('私钥文件不存在: ' . $privateKeyPath); } // 检查私钥文件权限和内容 $this->validatePrivateKey($privateKeyPath); // 设置SSL选项 curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM'); curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM'); curl_setopt($ch, CURLOPT_SSLKEY, $privateKeyPath); // 获取并设置CA证书 $caCertPath = $this->getCACertPath(); if ($caCertPath && file_exists($caCertPath)) { curl_setopt($ch, CURLOPT_CAINFO, $caCertPath); Log::info('使用CA证书: ' . $caCertPath); } else { Log::warning('无法获取CA证书,临时关闭SSL验证'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); } $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $errorNumber = curl_errno($ch); $error = curl_error($ch); curl_close($ch); // 记录完整响应,便于调试 Log::info('微信支付平台证书API响应: HTTP状态码=' . $httpCode . ', 错误代码=' . $errorNumber . ', 错误信息=' . $error . ', 响应内容=' . $response); if ($httpCode !== 200) { // 特殊处理常见错误码 $errorMsg = "请求微信支付API失败,HTTP状态码: {$httpCode}"; switch ($httpCode) { case 401: $errorMsg .= ",可能是签名错误或商户信息不正确"; break; case 403: $errorMsg .= ",权限不足,可能是API未开通或IP白名单问题"; break; case 404: $errorMsg .= ",请求路径错误"; break; case 500: $errorMsg .= ",微信支付服务器内部错误"; break; } $errorMsg .= ",响应: {$response}"; throw new Exception($errorMsg); } if (empty($response)) { throw new Exception('微信支付API返回空响应'); } $data = json_decode($response, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new Exception('解析微信支付API响应失败: ' . json_last_error_msg() . ',原始响应: ' . $response); } if (!isset($data['data']) || !is_array($data['data'])) { throw new Exception('微信支付API响应数据异常,未找到证书列表: ' . json_encode($data)); } if (empty($data['data'])) { throw new Exception('微信支付API返回空的证书列表'); } // 处理证书数据前记录原始数据 Log::info('准备处理的证书数据数量: ' . count($data['data']) . ',数据: ' . json_encode($data['data'])); // 处理证书数据 $this->processCertificates($data['data']); } /** * 验证私钥文件 */ private function validatePrivateKey($privateKeyPath) { // 检查文件权限 if (!is_readable($privateKeyPath)) { throw new Exception('私钥文件不可读: ' . $privateKeyPath); } // 检查文件大小 $fileSize = filesize($privateKeyPath); if ($fileSize < 100) { throw new Exception('私钥文件过小,可能不是有效的私钥: ' . $privateKeyPath); } // 检查私钥文件格式 $privateKeyContent = file_get_contents($privateKeyPath); if (strpos($privateKeyContent, '-----BEGIN PRIVATE KEY-----') === false || strpos($privateKeyContent, '-----END PRIVATE KEY-----') === false) { throw new Exception('私钥文件格式不正确,不是有效的PEM格式私钥'); } // 尝试加载私钥验证有效性 $key = openssl_pkey_get_private($privateKeyContent); if (!$key) { throw new Exception('私钥无效,无法加载: ' . openssl_error_string()); } // 检查密钥类型和长度 $keyDetails = openssl_pkey_get_details($key); if ($keyDetails['type'] !== OPENSSL_KEYTYPE_RSA || $keyDetails['bits'] < 2048) { throw new Exception('私钥必须是2048位或以上的RSA密钥'); } Log::info('私钥验证通过'); } /** * 获取CA证书路径 */ private function getCACertPath() { $caCertPath = $this->config['ca_cert_path']; // 如果CA证书不存在,则尝试下载 if (!file_exists($caCertPath)) { try { Log::info('CA证书不存在,尝试下载: ' . $caCertPath); $context = stream_context_create([ 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false ], 'http' => [ 'timeout' => 10, ] ]); $cacert = file_get_contents('https://curl.se/ca/cacert.pem', false, $context); if ($cacert && strlen($cacert) > 10000) { // 确保下载到有效内容 file_put_contents($caCertPath, $cacert); Log::info('CA证书下载成功: ' . $caCertPath); return $caCertPath; } else { Log::error('下载的CA证书内容无效'); } } catch (Exception $e) { Log::error('下载CA证书失败: ' . $e->getMessage()); } } else { Log::info('使用已存在的CA证书: ' . $caCertPath); } return $caCertPath; } /** * 处理证书数据 * @param array $certificates 证书列表 */ private function processCertificates($certificates) { $successCount = 0; $errorDetails = []; foreach ($certificates as $index => $certInfo) { try { // 记录当前处理的证书信息 $serialNo = $certInfo['serial_no'] ?? '未知'; Log::info('处理第' . ($index + 1) . '个证书,序列号: ' . $serialNo); // 验证证书信息是否完整 if (!isset($certInfo['encrypt_certificate'])) { throw new Exception('缺少encrypt_certificate字段'); } $encryptCert = $certInfo['encrypt_certificate']; $requiredFields = ['ciphertext', 'nonce', 'associated_data']; foreach ($requiredFields as $field) { if (!isset($encryptCert[$field]) || empty($encryptCert[$field])) { throw new Exception("证书信息缺少{$field}字段"); } } // 解密证书 $cert = $this->decryptCertificate( $encryptCert['ciphertext'], $encryptCert['nonce'], $encryptCert['associated_data'] ); // 解析证书 $parsedCert = openssl_x509_parse($cert); if (!$parsedCert) { $error = openssl_error_string(); throw new Exception('解析证书失败: ' . $error); } // 验证证书有效期 $now = time(); $effectiveTime = strtotime($certInfo['effective_time'] ?? ''); $expireTime = strtotime($certInfo['expire_time'] ?? ''); if ($effectiveTime === false || $expireTime === false) { throw new Exception('证书有效期格式不正确'); } if ($now < $effectiveTime) { throw new Exception('证书尚未生效'); } if ($now > $expireTime) { throw new Exception('证书已过期'); } // 保存证书信息 $this->platformCerts[] = [ 'serial_no' => $serialNo, 'effective_time' => $certInfo['effective_time'], 'expire_time' => $certInfo['expire_time'], 'cert' => $cert, 'parsed_cert' => $parsedCert, ]; $successCount++; Log::info('成功处理证书,序列号: ' . $serialNo); } catch (Exception $e) { $errorMsg = '处理证书失败: ' . $e->getMessage(); $errorDetails[] = $errorMsg . ',证书信息: ' . json_encode($certInfo); Log::error($errorMsg); } } Log::info("证书处理完成,成功: {$successCount}个,失败: " . (count($certificates) - $successCount) . "个"); if (empty($this->platformCerts)) { throw new Exception('所有证书处理失败,没有可用的平台证书。详细错误: ' . implode('; ', $errorDetails)); } // 按过期时间排序,最新的在前面 usort($this->platformCerts, function ($a, $b) { return strtotime($b['expire_time']) - strtotime($a['expire_time']); }); } /** * 解密证书 * @param string $ciphertext 密文 * @param string $nonce 随机串 * @param string $associatedData 附加数据 * @return string 解密后的证书内容 */ private function decryptCertificate($ciphertext, $nonce, $associatedData) { // 记录解密参数,便于调试 Log::info("开始解密,nonce长度: " . strlen($nonce) . ", associatedData长度: " . strlen($associatedData) . ", 密文长度: " . strlen($ciphertext)); // 验证输入参数 if (empty($ciphertext)) { throw new Exception('密文为空'); } if (empty($nonce)) { throw new Exception('随机串为空'); } if (empty($associatedData)) { throw new Exception('附加数据为空'); } // 尝试解码base64 $decodedCiphertext = base64_decode($ciphertext, true); if ($decodedCiphertext === false) { throw new Exception('密文base64解码失败,可能不是有效的base64字符串'); } if (strlen($decodedCiphertext) < 10) { throw new Exception('解码后的密文长度过短,可能是无效数据'); } // 使用正确的AEAD_AES_256_GCM解密方法 // 微信支付使用的是AEAD_AES_256_GCM,需要正确处理认证标签 $ciphertext = base64_decode($ciphertext); $authTag = substr($ciphertext, -16); $ciphertext = substr($ciphertext, 0, -16); // 清除之前的OpenSSL错误 while (openssl_error_string() !== false) { } // 使用AEAD_AES_256_GCM解密 $cert = openssl_decrypt( $ciphertext, 'aes-256-gcm', $this->config['api_v3_key'], OPENSSL_RAW_DATA, $nonce, $authTag, $associatedData ); // 收集所有OpenSSL错误 $errors = []; while ($error = openssl_error_string()) { $errors[] = $error; } if ($cert === false) { throw new Exception('解密平台证书失败: ' . implode('; ', $errors) . '。检查APIv3密钥是否正确,密钥长度是否为32位'); } // 验证解密结果是否为有效的证书 if (strpos($cert, '-----BEGIN CERTIFICATE-----') === false || strpos($cert, '-----END CERTIFICATE-----') === false) { throw new Exception('解密结果不是有效的证书格式,可能是密钥错误'); } Log::info('证书解密成功,解密结果长度: ' . strlen($cert)); return $cert; } /** * 生成签名 * @param string $method 请求方法 * @param string $url 请求URL * @param int $timestamp 时间戳 * @param string $nonce 随机串 * @param string $body 请求体 * @return string 签名 */ private function generateSignature($method, $url, $timestamp, $nonce, $body) { $message = "{$method}\n{$url}\n{$timestamp}\n{$nonce}\n{$body}\n"; Log::info("生成签名的原始消息: " . base64_encode($message)); // 用base64避免特殊字符问题 // 加载私钥 $privateKey = $this->getPrivateKey(); // 生成签名 $signature = ''; $success = openssl_sign($message, $signature, $privateKey, OPENSSL_ALGO_SHA256); if (!$success) { throw new Exception('生成签名失败: ' . openssl_error_string()); } $signatureBase64 = base64_encode($signature); Log::info("生成签名成功: {$signatureBase64}"); return $signatureBase64; } /** * 获取私钥 * @return resource 私钥资源 */ private function getPrivateKey() { $privateKey = $this->config['private_key']; // 如果私钥是文件路径,读取文件内容 if (is_file($privateKey)) { $privateKey = file_get_contents($privateKey); } // 加载私钥 $key = openssl_pkey_get_private($privateKey); if (!$key) { throw new Exception('加载商户私钥失败: ' . openssl_error_string()); } return $key; } /** * 获取缓存的证书 * @return array 证书列表 */ private function getCachedCerts() { $cacheFile = $this->certPath . 'platform_certs.cache'; if (!file_exists($cacheFile)) { Log::info('平台证书缓存文件不存在: ' . $cacheFile); return []; } if (!is_readable($cacheFile)) { Log::warning('平台证书缓存文件不可读: ' . $cacheFile); return []; } $cacheData = json_decode(file_get_contents($cacheFile), true); if (json_last_error() !== JSON_ERROR_NONE) { Log::error('解析平台证书缓存失败: ' . json_last_error_msg()); return []; } if (!isset($cacheData['expire_time']) || !isset($cacheData['certs']) || !is_array($cacheData['certs'])) { Log::error('平台证书缓存格式不正确'); return []; } // 检查缓存是否过期(提前1小时刷新) $expireTime = $cacheData['expire_time']; $now = time(); if ($now >= ($expireTime - 3600)) { Log::info("平台证书缓存已过期或即将过期,当前时间: {$now},过期时间: {$expireTime}"); return []; } return $cacheData['certs']; } /** * 缓存证书 */ private function cacheCerts() { if (empty($this->platformCerts)) { Log::warning('没有可缓存的平台证书'); return; } // 使用最早过期的时间作为缓存过期时间 $expireTime = time(); foreach ($this->platformCerts as $cert) { $certExpire = strtotime($cert['expire_time']); if ($certExpire > $expireTime) { $expireTime = $certExpire; } } $cacheData = [ 'expire_time' => $expireTime, 'certs' => $this->platformCerts, ]; $cacheFile = $this->certPath . 'platform_certs.cache'; $result = file_put_contents($cacheFile, json_encode($cacheData)); if ($result === false) { Log::error('保存平台证书缓存失败: ' . $cacheFile); } else { Log::info('平台证书缓存保存成功,有效期至: ' . date('Y-m-d H:i:s', $expireTime)); } // 保存证书文件 foreach ($this->platformCerts as $cert) { $certFile = $this->certPath . $cert['serial_no'] . '.pem'; if (file_put_contents($certFile, $cert['cert']) === false) { Log::error('保存平台证书文件失败: ' . $certFile); } else { Log::info('平台证书文件保存成功: ' . $certFile); } } } /** * 生成随机字符串 */ private function generateNonce($length = 16) { $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $nonce = ''; for ($i = 0; $i < $length; $i++) { $nonce .= $chars[random_int(0, strlen($chars) - 1)]; } return $nonce; } /** * 获取最新的平台证书 * @return string 证书内容 */ public function getLatestCert() { $certs = $this->getPlatformCerts(); if (empty($certs)) { throw new Exception('没有可用的平台证书'); } return $certs[0]['cert']; } /** * 根据序列号获取平台证书 * @param string $serialNo 证书序列号 * @return string 证书内容 */ public function getCertBySerialNo($serialNo) { $certs = $this->getPlatformCerts(); foreach ($certs as $cert) { if ($cert['serial_no'] === $serialNo) { return $cert['cert']; } } throw new Exception('找不到序列号为 ' . $serialNo . ' 的平台证书'); } /** * 获取配置参数 * @param string $key 配置键名 * @return mixed 配置值 */ public function getConfig($key = null) { if ($key === null) { return $this->config; } return isset($this->config[$key]) ? $this->config[$key] : null; } /** * 获取证书保存路径 * @return string 证书保存路径 */ public function getCertPath() { return $this->certPath; } }
2025年09月05日
38 阅读
0 评论
0 点赞
2025-08-18
【PHP】bc系列函数(高精度数学运算)
PHP bc系列函数(高精度数学运算)简介:bc系列函数用于处理任意精度的十进制计算,适用于金融、科学计算等对精度要求极高的场景。需启用bcmath扩展(可通过phpinfo()检查是否启用)。特点:所有参数和返回值均为字符串类型(避免精度丢失)支持自定义小数位数(scale参数)提供基本运算、比较、幂运算等完整功能一、基础运算函数1. 加法:bcadd()// 语法:bcadd(左操作数, 右操作数, 保留小数位数) echo bcadd('123.456', '78.9', 2); // 202.36(123.456 + 78.9 = 202.356,保留2位小数) echo bcadd('100', '200', 0); // 300(整数相加)2. 减法:bcsub()// 语法:bcsub(被减数, 减数, 保留小数位数) echo bcsub('100.5', '30.25', 1); // 70.2(100.5 - 30.25 = 70.25,保留1位小数) echo bcsub('50', '75', 0); // -25(负数结果)3. 乘法:bcmul()// 语法:bcmul(乘数1, 乘数2, 保留小数位数) echo bcmul('2.5', '4.2', 2); // 10.50(2.5 × 4.2 = 10.5,保留2位小数) echo bcmul('100', '0.01', 0); // 1(整数与小数相乘)4. 除法:bcdiv()// 语法:bcdiv(被除数, 除数, 保留小数位数) echo bcdiv('10', '3', 3); // 3.333(10 ÷ 3 ≈ 3.3333...,保留3位) var_dump(bcdiv('5', '0', 0)); // false(除数为0时返回false)5. 取模(余数):bcmod()// 语法:bcmod(被除数, 除数) echo bcmod('10', '3'); // 1(10 ÷ 3 余数为1) echo bcmod('7.5', '2.5'); // 0.0(7.5 是 2.5 的整数倍)二、比较函数bccomp()// 语法:bccomp(值1, 值2, 比较精度) // 返回值:1(值1 > 值2)、0(相等)、-1(值1 < 值2) echo bccomp('10', '10', 0); // 0(相等) echo bccomp('10.5', '10.49', 1); // 1(10.5 > 10.49,精确到1位小数) echo bccomp('5.999', '6', 2); // -1(5.999 < 6,精确到2位小数)三、幂运算与开方1. 幂运算:bcpow()// 语法:bcpow(底数, 指数, 保留小数位数) echo bcpow('2', '3', 0); // 8(2³ = 8) echo bcpow('1.2', '2', 2); // 1.44(1.2² = 1.44)2. 幂运算取模:bcpowmod()// 语法:bcpowmod(底数, 指数, 模数, 保留小数位数) // 作用:(base^exponent) % modulus,比先算幂再取模更高效 echo bcpowmod('3', '3', '5', 0); // 2(3³=27,27%5=2)3. 开平方:bcsqrt()// 语法:bcsqrt(数值, 保留小数位数) echo bcsqrt('25', 0); // 5(√25 = 5) echo bcsqrt('2', 4); // 1.4142(√2 ≈ 1.4142,保留4位) var_dump(bcsqrt('-4', 0)); // false(负数无法开平方)四、全局配置函数bcscale()// 功能:设置所有bc函数的默认小数位数(全局生效) bcscale(2); // 全局默认保留2位小数 echo bcadd('1', '2'); // 3.00(使用全局配置) echo bcsub('5', '3.5'); // 1.50(自动补全2位小数) echo bcadd('1', '2', 0); // 3(局部参数优先级高于全局)关键注意事项参数类型:必须传入字符串(如'123.45'),而非数字(避免自动转换导致精度丢失)。返回值:始终为字符串,需转换类型时用(float)或(int)。扩展依赖:需在php.ini中启用extension=bcmath(重启服务生效)。精度优先级:函数的scale参数 > bcscale()全局配置。通过这些函数,PHP可以实现高精度计算,解决普通数值类型精度不足的问题。
2025年08月18日
20 阅读
0 评论
0 点赞
2025-07-26
【PHP】ThinkPHP8数据库迁移示例
以下是一个包含 各种字段类型、约束条件、索引和特殊配置 的ThinkPHP8数据库迁移示例(以products产品表为例),覆盖常见场景:迁移文件示例(database/migrations/20250726153000_create_products_table.php)<?php use think\migration\Migrator; use think\migration\db\Column; class CreateProductsTable extends Migrator { /** * 执行迁移(创建表结构) */ public function change() { // 创建products表,配置表级参数 $table = $this->table('products', [ 'engine' => 'InnoDB', // 数据库引擎 'charset' => 'utf8mb4', // 字符集 'collation' => 'utf8mb4_unicode_ci', // 排序规则 'comment' => '产品表(包含多种字段类型示例)', // 表注释 'auto_increment' => 1000, // 自增ID起始值(MySQL支持) ]); // 字段定义(覆盖各种场景) $table // 1. 自增主键(整数型,无符号,自增) ->addColumn('id', 'integer', [ 'identity' => true, // 自增 'unsigned' => true, // 无符号(只存正数) 'comment' => '产品ID(主键)' ]) // 2. 字符串(有限长度,非空,唯一索引) ->addColumn('sku', 'string', [ 'limit' => 50, // 长度限制 'null' => false, // 非空(默认) 'comment' => '产品SKU(唯一标识)', 'unique' => true // 唯一约束 ]) // 3. 字符串(长文本,可空,带默认值) ->addColumn('name', 'string', [ 'limit' => 255, 'null' => false, 'default' => '', // 默认空字符串 'comment' => '产品名称' ]) // 4. 文本类型(无长度限制,可空) ->addColumn('description', 'text', [ 'null' => true, // 允许为空 'comment' => '产品详细描述' ]) // 5. 小数类型(高精度,带默认值) ->addColumn('price', 'decimal', [ 'precision' => 10, // 总位数 'scale' => 2, // 小数位数 'default' => 0.00, // 默认0.00 'comment' => '产品售价' ]) // 6. 整数类型(无符号,默认值,索引) ->addColumn('stock', 'integer', [ 'unsigned' => true, // 无符号(只存正数) 'default' => 0, // 默认库存0 'comment' => '库存数量', 'index' => true // 普通索引 ]) // 7. 枚举类型(固定可选值) ->addColumn('status', 'enum', [ 'values' => ['draft', 'active', 'disabled'], // 可选值 'default' => 'draft', // 默认草稿状态 'comment' => '产品状态(draft:草稿, active:上架, disabled:下架)' ]) // 8. 布尔类型(tinyint,默认true) ->addColumn('is_recommend', 'boolean', [ 'default' => true, // 默认true(1) 'comment' => '是否推荐(1:是, 0:否)' ]) // 9. 外键关联(整数,无符号,级联操作) ->addColumn('category_id', 'integer', [ 'unsigned' => true, 'null' => false, 'comment' => '所属分类ID', 'index' => true // 普通索引 ]) // 10. JSON类型(存储复杂结构,可空) ->addColumn('tags', 'json', [ 'null' => true, 'comment' => '产品标签(JSON格式,如["新品","热卖"])' ]) // 11. 时间戳类型(自动维护) ->addTimestamps() // 自动添加 create_time 和 update_time(datetime类型) // 12. 软删除字段(自动维护删除时间) ->addSoftDelete() // 添加 delete_time 字段(软删除标识) // 13. 整数类型(排序权重,默认0) ->addColumn('sort', 'integer', [ 'default' => 0, 'comment' => '排序权重(值越大越靠前)' ]) // 添加外键约束(关联分类表) ->addForeignKey('category_id', 'categories', 'id', [ 'delete' => 'CASCADE', // 分类删除时,关联产品也删除 'update' => 'CASCADE' // 分类ID更新时,产品关联ID同步更新 ]) // 添加联合索引(名称+状态) ->addIndex(['name', 'status'], [ 'name' => 'idx_name_status' // 自定义索引名 ]) // 执行创建表 ->create(); } /** * 回滚迁移(删除表) */ public function down() { // 先删除外键约束(避免删除表失败) $this->table('products')->dropForeignKey('category_id'); // 再删除表 $this->dropTable('products'); } }关键字段配置说明字段名类型/配置核心特点id自增整数、主键无符号,自增起始值1000sku字符串、唯一约束唯一标识,非空,长度50name字符串、默认值非空,默认空字符串,用于产品名称description文本类型、可空无长度限制,允许为空(适合长文本描述)price小数(10,2)高精度价格,默认0.00stock无符号整数、索引库存数量,非负,普通索引提升查询效率status枚举类型固定可选值,默认草稿状态is_recommend布尔类型本质tinyint,默认true(1)category_id外键、级联操作关联分类表,删除/更新时级联处理tagsJSON类型存储数组/对象结构,适合非结构化数据create_time时间戳(自动维护)由addTimestamps()生成,记录创建时间delete_time软删除字段由addSoftDelete()生成,非NULL表示已删除执行迁移# 创建表 php think migrate:run # 回滚(删除表) php think migrate:rollback注意事项外键依赖:示例中category_id关联categories表,需确保categories表已存在(可先创建分类表迁移)。索引优化:根据查询场景添加索引,避免过度索引影响写入性能。软删除:addSoftDelete()会自动添加delete_time字段(datetime类型,默认NULL),适合逻辑删除。枚举值:修改枚举值需谨慎,生产环境可能需要先添加新值再迁移数据。字段注释:每个字段添加清晰注释,便于后期维护(尤其团队协作场景)。通过这个示例,可以覆盖ThinkPHP8迁移工具中绝大多数字段配置场景,实际开发中可根据需求调整。
2025年07月26日
41 阅读
0 评论
0 点赞
2025-07-26
【PHP】ThinkPHP8 数据库迁移与数据填充完全指南
ThinkPHP8 数据库迁移与数据填充完全指南(官方文档补充版)一、安装与配置1. 安装迁移扩展composer require topthink/think-migration2. 配置文件说明配置文件位于 config/database.php默认迁移表名:think_migration支持多数据库连接3. 环境要求PHP >= 8.1ThinkPHP >= 8.0建议使用 InnoDB 引擎(支持事务)二、迁移文件操作1. 创建迁移文件# 创建基础迁移文件 php think migrate:create CreateUsersTable # 创建带表名的迁移文件(自动生成基础结构) php think migrate:create AddEmailToUsersTable --table=users # 指定数据库连接 php think migrate:create CreateLogsTable --connection=log2. 迁移文件结构生成的迁移文件位于 database/migrations,示例:<?php use think\migration\Migrator; use think\migration\db\Column; class CreateUsersTable extends Migrator { /** * 执行迁移(向上操作) */ public function up() { $table = $this->table('users'); $table->addColumn('name', 'string', ['limit' => 50]) ->addColumn('email', 'string', ['limit' => 100, 'unique' => true]) ->addColumn('password', 'string', ['limit' => 100]) ->addTimestamps() // 自动添加 create_time 和 update_time 字段 ->create(); } /** * 回滚迁移(向下操作) */ public function down() { $this->dropTable('users'); } }三、常用字段类型字段类型描述示例用法integer整数类型->addColumn('age', 'integer')biginteger大整数类型->addColumn('user_id', 'biginteger')string字符串类型->addColumn('name', 'string', ['limit' => 50])text长文本类型->addColumn('content', 'text')datetime日期时间类型->addColumn('created_at', 'datetime')timestamp时间戳类型->addColumn('updated_at', 'timestamp')boolean布尔类型->addColumn('status', 'boolean', ['default' => 0])decimal高精度小数->addColumn('price', 'decimal', ['precision' => 10, 'scale' => 2])enum枚举类型->addColumn('gender', 'enum', ['values' => ['male', 'female', 'other']])四、表结构操作1. 创建表public function up() { $table = $this->table('users', [ 'engine' => 'InnoDB', 'charset' => 'utf8mb4', 'comment' => '用户表', 'collation' => 'utf8mb4_unicode_ci' ]); $table->addColumn('id', 'integer', ['identity' => true]) // 自增ID(默认主键) ->addColumn('username', 'string', ['limit' => 30]) ->addColumn('email', 'string', ['limit' => 100, 'unique' => true]) ->addColumn('password', 'string', ['limit' => 100]) ->addColumn('status', 'boolean', ['default' => 1]) ->addIndex(['username']) // 普通索引 ->addUniqueIndex(['email']) // 唯一索引 ->addTimestamps() // 自动添加 create_time 和 update_time ->addSoftDelete() // 添加 delete_time 软删除字段 ->create(); }2. 修改表添加字段:public function up() { $this->table('users') ->addColumn('phone', 'string', ['limit' => 20, 'after' => 'email']) ->update(); }修改字段:public function up() { $this->table('users') ->changeColumn('phone', 'string', ['limit' => 11, 'default' => '']) ->update(); }删除字段:public function up() { $this->table('users') ->removeColumn('phone') ->update(); }3. 添加外键约束public function up() { $this->table('posts') ->addColumn('user_id', 'integer') ->addColumn('title', 'string') ->addColumn('content', 'text') ->addForeignKey('user_id', 'users', 'id', ['delete' => 'CASCADE', 'update' => 'CASCADE']) ->update(); }五、执行迁移命令1. 执行所有未迁移的文件php think migrate:run2. 回滚上一次迁移php think migrate:rollback3. 回滚到指定版本php think migrate:rollback --target=20250726101500 # 指定时间戳4. 重置所有迁移(先回滚再执行)php think migrate:reset5. 刷新数据库(重置并重新执行所有迁移)php think migrate:refresh6. 查看迁移状态php think migrate:status7. 指定数据库连接php think migrate:run --connection=db_log # 指定日志数据库六、数据填充操作1. 创建数据填充文件php think seed:create Users # 创建Users表的数据填充器2. 编写数据填充逻辑<?php use think\migration\Seeder; use think\facade\Db; class Users extends Seeder { /** * 填充数据 */ public function run() { $data = []; for ($i = 1; $i <= 10; $i++) { $data[] = [ 'username' => 'user' . $i, 'email' => 'user' . $i . '@example.com', 'password' => password_hash('123456', PASSWORD_DEFAULT), 'create_time' => date('Y-m-d H:i:s'), 'update_time' => date('Y-m-d H:i:s') ]; } // 使用批量插入提高性能 Db::name('users')->insertAll($data); } }3. 执行数据填充php think seed:run # 执行所有填充器 php think seed:run --seeder Users # 执行指定填充器七、高级技巧1. 使用 change() 方法(简化双向操作)public function change() { $table = $this->table('users'); // 创建表 $table->addColumn('username', 'string') ->addColumn('email', 'string') ->create(); // 修改表 $table->addColumn('phone', 'string') ->update(); }2. 使用事务public function up() { $this->getAdapter()->beginTransaction(); try { $this->table('table1')->addColumn(...)->create(); $this->table('table2')->addColumn(...)->create(); $this->getAdapter()->commit(); } catch (\Exception $e) { $this->getAdapter()->rollBack(); throw $e; } }3. 创建数据库快照php think migrate:snapshot # 创建当前数据库结构快照4. 指定迁移文件路径php think migrate:run --path=database/migrations/custom # 指定自定义路径八、最佳实践1. 命名规范迁移文件:YYYYMMDDHHMMSS_表名_操作.php(自动生成)表名:使用小写字母和下划线(如 user_info)字段名:使用小写字母和下划线(如 create_time)2. 避免复杂SQL单个迁移文件只做单一变更避免在迁移中执行数据迁移操作3. 测试迁移在开发环境充分测试使用测试数据库验证回滚功能4. 生产环境注意事项迁移前备份数据库使用 --pretend 参数预览变更避免在高峰期执行大型迁移5. 团队协作迁移文件提交到版本控制系统避免多人同时修改同一迁移文件拉取代码后先执行 php think migrate:run九、常见问题与解决方案1. 迁移文件冲突问题:多个迁移文件时间戳相近导致执行顺序异常解决方案:使用 --timestamp 参数手动指定时间戳php think migrate:create NewTable --timestamp=202507261530002. 外键约束错误问题:删除表时外键约束阻止操作解决方案:在 down() 方法中先删除外键public function down() { $this->table('posts') ->dropForeignKey('user_id') ->update(); $this->dropTable('posts'); }3. 数据填充重复问题问题:多次执行填充器导致数据重复解决方案:在填充前清空表public function run() { Db::name('users')->delete(true); // 清空表 // 填充新数据 }4. 迁移性能问题问题:大型表迁移缓慢解决方案:分批次执行数据迁移使用数据库原生工具导入大型数据避免在迁移中使用复杂查询
2025年07月26日
80 阅读
0 评论
0 点赞
2025-05-17
【ThinkPHP】创建有背景的微信小程序二维码
<?php namespace app\common\lib\wechat; use think\Exception; use think\facade\Cache; use think\facade\Request; use think\Image; use think\facade\Db; class QrCode extends Basic { // 小程序页面路径 private string $path = "pages/myCenter/invite"; // 小程序码保存路径 private string $savePath = "./storage/qrcode/"; // 二维码参数 private string $param = ""; public function __construct(string $path = "", string $field = "", string $param = "", int $width = 430) { if (!empty($path) && empty($field)) { $this->path = $path; } if (!empty($path) && !empty($field) && !empty($param)) { $this->path = "{$path}?{$field}={$param}"; } $this->param = $param; } /** * 生成小程序码并可选择合并背景图 * * @param string $backgroundPath 背景图路径 * @param int $qrWidth 二维码在背景图上的宽度位置 * @param int $qrHeight 二维码在背景图上的高度位置 * @return string 生成后的二维码URL */ public function setQrcode(string $backgroundPath = '', int $qrWidth = 160, int $qrHeight = 530): string { try { // 获取访问令牌 $accessToken = Cache::get('accesstoken'); if (empty($accessToken)) { $accessToken = (new AccessToken())->getAccesToken(); Cache::set('accesstoken', $accessToken, 7200); // 假设token有效期为2小时 } // 请求小程序码 $url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$accessToken}"; $data = [ 'path' => $this->path, 'scene' => 'type=qrcode', 'width' => 430, ]; $result = $this->curlPost($url, $data, 'POST'); if ($result === false || isset($result['errcode'])) { throw new Exception("Failed to get QR code: " . json_encode($result)); } // 保存二维码到文件 $fileName = md5($this->param); $directory = $this->savePath . date('Ymd') . '/'; if (!$this->createDirectory($directory)) { throw new Exception("目录创建失败: {$directory}"); } $filePath = "{$directory}{$fileName}.png"; if (!file_put_contents($filePath, $result)) { throw new Exception("文件写入失败: {$filePath}"); } // 生成完整URL $fullDomain = (new \app\common\lib\data\Str())->getFullDomain(); $qrcodeUrl = $fullDomain . ltrim($filePath, '.'); // 合并背景图(如果提供) if (!empty($backgroundPath)) { return $this->mergeWithBackground($backgroundPath, $filePath, $qrcodeUrl, $qrWidth, $qrHeight); } return $qrcodeUrl; } catch (Exception $e) { // 记录错误日志 error_log($e->getMessage()); return ''; } } /** * 将二维码与背景图合并 */ private function mergeWithBackground(string $backgroundPath, string $qrcodePath, string $defaultUrl, int $qrWidth, int $qrHeight): string { try { $fileName = md5(uniqid() . time()); $newImagePath = $this->savePath . date('Ymd') . "/{$fileName}.png"; $background = Image::open($backgroundPath); $background->water($qrcodePath, [$qrWidth, $qrHeight])->save($newImagePath); $imageInfo = [ 'url' => Request::domain() . substr($newImagePath, 1), 'size' => filesize($newImagePath), 'name' => $fileName, 'mime' => mime_content_type($newImagePath), 'ext' => 'png', ]; $fileData = [ 'f_uuid' => setUUID(), 'f_file' => $imageInfo['url'], 'f_location' => 0, 'f_type' => 'image', 'f_info' => serialize($imageInfo), 'f_user_uuid' => $this->request->index_user_uuid ?? '', 'f_create_time' => time(), ]; Db::name('File')->insert($fileData); return $imageInfo['url']; } catch (Exception $e) { // 记录错误日志 error_log($e->getMessage()); return $defaultUrl; } } /** * 递归创建目录 */ private function createDirectory(string $path, int $mode = 0777, bool $recursive = true): bool { if (is_dir($path)) { return true; } if (mkdir($path, $mode, $recursive)) { chmod($path, $mode); return true; } if (!is_dir(dirname($path))) { if ($this->createDirectory(dirname($path), $mode, $recursive)) { return $this->createDirectory($path, $mode, $recursive); } } return false; } /** * 发送HTTP POST请求 */ // public function curlPost($url, $data, $method = "POST") // { // $ch = curl_init(); //1.初始化 // curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址 // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式 // //4.参数如下 // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器 // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip, deflate'));//gzip解压内容 // curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); // // if ($method == "POST") {//5.post方式的时候添加数据 // $data = json_encode($data); // curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // } // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // $tmpInfo = curl_exec($ch);//6.执行 // // if (curl_errno($ch)) {//7.如果出错 // return curl_error($ch); // } // curl_close($ch);//8.关闭 // return $tmpInfo; // } }
2025年05月17日
46 阅读
0 评论
0 点赞
2025-05-13
【PHP】PHPExcel 使用手册
PHPExcel 是一个用于操作 Microsoft Excel 格式文件的 PHP 库,它支持读取和写入多种格式(如 .xls, .xlsx, .csv 等)。不过需要注意,PHPExcel 项目已停止维护,推荐使用其继任者 PhpSpreadsheet。以下是 PHPExcel 的基本使用指南:1. 安装 PHPExcel# 使用 Composer 安装(推荐) composer require phpoffice/phpexcel2. 读取 Excel 文件require 'vendor/autoload.php'; use PHPExcel_IOFactory; // 读取文件 $inputFileName = 'example.xlsx'; $objPHPExcel = PHPExcel_IOFactory::load($inputFileName); // 获取第一个工作表 $sheet = $objPHPExcel->getActiveSheet(); // 获取最高行和最高列 $highestRow = $sheet->getHighestRow(); $highestColumn = $sheet->getHighestColumn(); // 遍历数据 for ($row = 1; $row <= $highestRow; $row++) { for ($col = 'A'; $col <= $highestColumn; $col++) { $cellValue = $sheet->getCell($col.$row)->getValue(); echo "Row $row, Column $col: $cellValue\n"; } }3. 创建新的 Excel 文件require 'vendor/autoload.php'; use PHPExcel; use PHPExcel_IOFactory; // 创建新的 PHPExcel 对象 $objPHPExcel = new PHPExcel(); // 设置文档属性 $objPHPExcel->getProperties() ->setCreator("Your Name") ->setTitle("Sample Excel File"); // 获取活动工作表 $sheet = $objPHPExcel->getActiveSheet(); // 设置单元格值 $sheet->setCellValue('A1', 'Hello'); $sheet->setCellValue('B1', 'World!'); $sheet->setCellValue('A2', 'This is a sample spreadsheet.'); // 保存为 .xlsx 文件 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('new_file.xlsx');4. 设置样式// 设置字体样式 $sheet->getStyle('A1')->getFont() ->setName('Arial') ->setSize(14) ->setBold(true) ->setColor(new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_RED)); // 设置单元格背景色 $sheet->getStyle('A1')->getFill() ->setFillType(PHPExcel_Style_Fill::FILL_SOLID) ->getStartColor()->setARGB('FFEEEEEE'); // 设置对齐方式 $sheet->getStyle('A1')->getAlignment() ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER) ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); // 设置列宽 $sheet->getColumnDimension('A')->setWidth(20);5. 合并单元格// 合并 A1 到 B1 的单元格 $sheet->mergeCells('A1:B1'); // 取消合并 $sheet->unmergeCells('A1:B1');6. 处理日期格式// 设置日期值(PHPExcel 使用 Excel 时间戳格式) $dateTime = new DateTime(); $sheet->setCellValue('A1', $dateTime->format('Y-m-d H:i:s')); // 设置单元格格式为日期 $sheet->getStyle('A1')->getNumberFormat() ->setFormatCode('yyyy-mm-dd hh:mm:ss');7. 保存为不同格式// 保存为 .xlsx 格式 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('file.xlsx'); // 保存为 .xls 格式 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('file.xls'); // 保存为 CSV 格式 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV'); $objWriter->save('file.csv');8. 从浏览器下载文件// 设置响应头 header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="download.xlsx"'); header('Cache-Control: max-age=0'); // 输出到浏览器 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('php://output'); exit;注意事项性能问题:处理大量数据时,PHPExcel 可能占用较多内存。考虑使用 PHPExcel_CachedObjectStorageFactory 进行优化。替代方案:推荐使用 PhpSpreadsheet,它是 PHPExcel 的官方继任者,修复了许多问题并提供更好的性能。内存优化:// 设置为只读模式以减少内存消耗 $objReader = PHPExcel_IOFactory::createReader('Excel2007'); $objReader->setReadDataOnly(true); $objPHPExcel = $objReader->load('large_file.xlsx');如需更详细的文档,请参考 PHPExcel 官方文档(注意:项目已停止维护)。
2025年05月13日
40 阅读
0 评论
0 点赞
2025-05-07
【PHP】PHP实现无限级树形分类 树形分类 如果子集的pid不是空,父级id不存在,则子集不展示
/** * @Author:小破孩 * @Email:3584685883@qq.com * @Time:2024/12/17 22:27 * @param $arr * @param $id * @param $pid * @return array * @Description:树形分类 如果子集的pid不是空,父级id不存在,则子集不展示 */ public function getTreeByDateDelChile($arr,$id,$pid) { $items = []; foreach($arr as $v){ $items[$v[$id]] = $v; } $tree = []; foreach($items as $k => $item){ if($item[$pid] &&!isset($items[$item[$pid]])){ unset($items[$k]); continue; } if(isset($items[$item[$pid]])){ $items[$item[$pid]]['child'][] = &$items[$k]; }else{ $tree[] = &$items[$k]; } } return $tree; } /** * @Author:小破孩 * @Email:3584685883@qq.com * @Time:2024/9/14 16:59 * @param array $arr * @param string $children * @return array * @Description:将树形数组转换成一维数组 */ public function setManyTosingle(array $arr = [], string $children = 'child'): array { $result = []; foreach ($arr as $item) { // 先将当前节点添加到结果数组中 $newItem = $item; if (isset($newItem[$children])) { unset($newItem[$children]); } $result[] = $newItem; // 递归处理子节点 if (isset($item[$children]) && is_array($item[$children])) { $result = array_merge($result, $this->setManyTosingle($item[$children], $children)); } } return $result; }
2025年05月07日
49 阅读
0 评论
0 点赞
2025-05-06
【PHP】清空MySQL数据,索引重置
<?php // 定义排除表的常量 const EXCLUDE_TABLES = [ 'web_admin_company', 'web_admin_func', 'web_admin_role', 'web_admin_user', 'web_app_config', 'web_china_city', 'web_china_city_area', 'web_china_city_backup', 'web_company_config', 'web_shop_category', 'web_tppay', // 'web_shop_goods_tmpserver', ]; class SomeClass { public function truncateTables() { try { // 开启事务 Db::startTrans(); // 获取所有表名 $tables = Db::query('SHOW TABLES'); $tableNames = array_map('current', $tables); foreach ($tableNames as $tableName) { if (!in_array($tableName, EXCLUDE_TABLES)) { // 使用参数化查询清空表并重置索引 Db::execute("TRUNCATE TABLE `$tableName`"); echo "表 {$tableName} 已清空<br>"; } } // 提交事务 Db::commit(); } catch (\Exception $e) { // 回滚事务 Db::rollback(); // 记录错误日志 error_log("发生错误: " . $e->getMessage()); echo "发生错误: " . $e->getMessage(); } } }
2025年05月06日
40 阅读
0 评论
0 点赞
2025-04-23
【PHP】管家婆各个大类的基础调用数据方法
<?php namespace app\common\lib\gjp\warehouse; use think\Exception; class Warehouse { protected $dbName; public function __construct() { $this->dbName = "*****"; // } //查询单据 public function getWarehouse($paramkey = "", $paramJson = "", $userId = "***********")// { $instanceGjpBasic = new \app\common\lib\gjp\Basic(); $gjpSignInfo = $instanceGjpBasic->getSignKey(); $instanceGjpApiUrl = new \app\common\lib\gjp\Apiurl($userId); $gjpApiUrlInfo = $instanceGjpApiUrl -> getApiUrl(); $mobile = empty($gjpApiUrlInfo['GraspCloudMobile']) ? 0 : $gjpApiUrlInfo['GraspCloudMobile']; $serviceid = empty($gjpApiUrlInfo['GraspCloudServerId']) ? 0 : $gjpApiUrlInfo['GraspCloudServerId']; $requestUrl = $gjpApiUrlInfo['ApiServerAddress']; $md5BeforeStr = "apiparam".$gjpApiUrlInfo['ApiParam']."apitype".'query'."dbname".(string)$this->dbName."interiorapi"."1"."managename"."GraspCMServerApi.dll"."mobile".$mobile."paramjson".$paramJson."paramkey".$paramkey."serviceid".$serviceid.$gjpSignInfo['SignKey']; $data = [ 'managename' => (string)"GraspCMServerApi.dll", 'dbname' => (string)$this->dbName, 'paramkey' => (string)$paramkey, 'paramjson' => (string)($paramJson), 'apiparam' => (string)$gjpApiUrlInfo['ApiParam'], 'apitype' => (string)"query", 'sign' => (string)md5($md5BeforeStr), 'mobile' => (string)$mobile, 'serviceid' => (string)$serviceid, 'interiorapi' => (integer)1, ]; $encodedParams = http_build_query($data); $result = $instanceGjpBasic->curlRequest($requestUrl,"POST",$encodedParams,true,false); if($result['code'] != 0){ print_r($result);die; } return $result; } //生产单据 public function setReceipts($vchtype = 0,$billdata = '',$userId = "*************"){ $instanceGjpBasic = new \app\common\lib\gjp\Basic(); $gjpSignInfo = $instanceGjpBasic->getSignKey(); $instanceGjpApiUrl = new \app\common\lib\gjp\Apiurl($userId); $gjpApiUrlInfo = $instanceGjpApiUrl -> getApiUrl(); $requestUrl = $gjpApiUrlInfo['ApiServerAddress']; $mobile = empty($gjpApiUrlInfo['GraspCloudMobile']) ? 0 : $gjpApiUrlInfo['GraspCloudMobile']; $serviceid = empty($gjpApiUrlInfo['GraspCloudServerId']) ? 0 : $gjpApiUrlInfo['GraspCloudServerId']; $md5BeforeStr = "apiparam".$gjpApiUrlInfo['ApiParam']."apitype".'process'."billdata". $billdata ."dbname".(string)$this->dbName."interiorapi"."1"."managename"."GraspCMServerApi.dll"."mobile".$mobile."processtype"."0"."serviceid".$serviceid."vchcode"."0"."vchtype".$vchtype.$gjpSignInfo['SignKey']; $data = [ 'managename' => (string)"GraspCMServerApi.dll", 'dbname' => (string)$this->dbName, 'processtype' => (integer)0, 'vchtype' => (integer)$vchtype, 'vchcode' => (integer)0, 'billdata' => (string)$billdata, 'apiparam' => (string)$gjpApiUrlInfo['ApiParam'], 'apitype' => (string)"process", 'sign' => (string)md5($md5BeforeStr), 'mobile' => (string)$mobile, 'serviceid' => (string)$serviceid, 'interiorapi' => (integer)1, ]; // print_r($data);die; $encodedParams = http_build_query($data); $result = $instanceGjpBasic->curlRequest($requestUrl,"POST",$encodedParams,true,false); return $result; } //新增或修改 基础资料新增 public function businessBaseInfo($basetype = "", $baseinfodata = "", $rec = "", $type = 1, $userId = "*********"){ if($type == 1){ $typeName = "add"; }else{ $typeName = "modify"; } if(empty($rec)){ (string)$rec = "0"; } $instanceGjpBasic = new \app\common\lib\gjp\Basic(); $gjpSignInfo = $instanceGjpBasic->getSignKey(); $instanceGjpApiUrl = new \app\common\lib\gjp\Apiurl($userId); $gjpApiUrlInfo = $instanceGjpApiUrl -> getApiUrl(); // print_r($gjpApiUrlInfo);die; $mobile = empty($gjpApiUrlInfo['GraspCloudMobile']) ? 0 : $gjpApiUrlInfo['GraspCloudMobile']; $serviceid = empty($gjpApiUrlInfo['GraspCloudServerId']) ? 0 : $gjpApiUrlInfo['GraspCloudServerId']; $requestUrl = $gjpApiUrlInfo['ApiServerAddress']; $md5BeforeStr ="actiontype".$typeName."apiparam".$gjpApiUrlInfo['ApiParam']."apitype".'baseinfo'."baseinfodata".$baseinfodata."basetype".$basetype."dbname".(string)$this->dbName."interiorapi"."1"."managename"."GraspCMServerApi.dll"."mobile".$mobile."rec".$rec."serviceid".$serviceid.$gjpSignInfo['SignKey']; // "actiontype".$typeName."apiparam".$gjpApiUrlInfo['ApiParam']."apitype"."baseinfo"."basetype".$basetype."baseinfodata".$baseinfodata."dbname".$this->dbName."interiorapi".1."managename"."GraspCMServerApi.dll"."mobile".$mobile."serviceid".$serviceid."sign".md5($md5BeforeStr) $data = [ 'managename' => (string)"GraspCMServerApi.dll", 'dbname' => (string)$this->dbName, 'actiontype' => (string)$typeName, 'rec' => (string)$rec, 'basetype' => (string)$basetype, 'baseinfodata' => (string)$baseinfodata, 'apiparam' => (string)$gjpApiUrlInfo['ApiParam'], 'apitype' => (string)"baseinfo", 'sign' => (string)md5($md5BeforeStr), 'mobile' => (string)$mobile, 'serviceid' => (string)$serviceid, 'interiorapi' => (integer)1, ]; $encodedParams = http_build_query($data); $result = $instanceGjpBasic->curlRequest($requestUrl,"POST",$encodedParams,true,false); if($result['code'] != 0){ print_r($result);die; } return $result; } }
2025年04月23日
43 阅读
0 评论
0 点赞
2025-04-23
【PHP】获取程序Api地址 - 管家婆基础对接 PHP版本
<?php namespace app\common\lib\gjp; class Apiurl { protected $userId; protected $appKey; protected $SercretKey; protected $currentTimestamp; protected $dbName; public function __construct($userId = '********') { $this->appKey = "*****************"; $this->SercretKey = "*******************"; $this->currentTimestamp = time(); $this->userId = $userId; $this->dbName = '*****'; } public function getApiUrl() { $url = "http://api.cmgrasp.com/CMGraspApi/GateWay"; $instanceGjpBasic = new \app\common\lib\gjp\Basic(); $instanceStr = new \app\common\lib\data\Str(); $randamStr = $instanceStr->setNonce(32,true); $md5BeforeStr = "AppKey".$this->appKey."InvalidTime".date('YmdHis',$this->currentTimestamp)."RandamStr".$randamStr."UserId".$this->userId.$this->SercretKey; $data = [ 'MethodName' => (string)"graspcm.cmapi.getcustomerapiurl", 'AppKey' => (string)$this->appKey, 'UserId' => (string)$this->userId, 'InvalidTime' => (string)date('Y-m-d H:i:s'), 'RandamStr' => (string)$randamStr, 'SignStr' => (string)md5($md5BeforeStr), 'DbName' => (string)$this->dbName ]; $jsonData = json_encode($data); $result = $instanceGjpBasic->curlRequest($url,"POST",$jsonData,true,false); if($result['RetCode'] != 0){ $this->getApiUrl(); } $resultData = json_decode($result['RetMsg'],true); return $resultData; } }
2025年04月23日
42 阅读
0 评论
0 点赞
2025-04-23
【PHP】获取接口所需的SignKey - 管家婆基础对接 PHP版本
<?php namespace app\common\lib\gjp; use think\Exception; class Basic { protected $appKey; protected $SercretKey; protected $currentTimestamp; public function __construct() { $this->appKey = "*************"; $this->SercretKey = "*************"; $this->currentTimestamp = time(); } public function getSignKey() { if(!empty( cache('gjp_sign'))) { $dataResult = cache('gjp_sign'); return $dataResult; } $signUrl = "http://api.cmgrasp.com/CMGraspApi/GateWay"; $instanceStr = new \app\common\lib\data\Str(); $randamStr = $instanceStr->setNonce(32,true); $md5BeforeStr = "AppKey".$this->appKey."InvalidTime".date('YmdHis',$this->currentTimestamp)."RandamStr".$randamStr.$this->SercretKey; $data = [ "MethodName" => (string)"graspcm.cmapi.getsignstr", 'AppKey' => (string)$this->appKey, 'InvalidTime' => (string)date('Y-m-d H:i:s',$this->currentTimestamp), 'RandamStr' => (string)$randamStr, 'SignStr' => (string)md5($md5BeforeStr), ]; $jsonData = json_encode($data); $result = $this->curlRequest($signUrl,"POST", $jsonData,true,false); if($result['RetCode'] != 0) { $this->getSignKey(); } $dataResult = json_decode($result['RetMsg'],true); if(empty( cache('gjp_sign'))) { $dataResult = json_decode($result['RetMsg'],true); cache('gjp_sign',$dataResult,60*60*20); } return $dataResult; } /** * @Author: 小破孩嫩 * @Email: 3584685883@qq.com * @Time: 2021/4/1 10:39 * @param string $url url地址 * @param string $method 请求方法,默认为 'GET',可选值为 'GET' 或 'POST' * @param mixed $data 要发送的数据,如果是 POST 请求则为数据内容,否则为 null * @param array $headers 自定义请求头信息 * @param int $timeout 超时时间,默认为 30 秒 * @param bool $verifySSL 是否验证 SSL 证书,默认为 true * @param bool $flbg 返回值是否转成数组,默认不转 * @param bool $headercontent 是否获取请求的header值内容,默认不获取 * @return array|bool|mixed|string * @Description:curl请求 */ public function curlRequest($url, $method = 'GET', $data = null, $flbg = false, $verifySSL = true, $headers = [], $headerContent = false, $timeout = 30) { // 初始化 cURL 会话 $ch = curl_init(); // 设置要请求的 URL curl_setopt($ch, CURLOPT_URL, $url); // 设置获取的信息以字符串形式返回,而不是直接输出 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 设置超时时间 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // 设置请求方法 if ($method === 'POST') { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } // 设置请求头 if (!empty($headers)) { curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } // 设置是否验证 SSL 证书 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verifySSL); // 执行 cURL 会话并获取响应 $response = curl_exec($ch); // 获取 HTTP 响应码 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 如果 cURL 执行出错 if (curl_errno($ch)) { // 输出错误信息 echo 'Curl error: ' . curl_error($ch); // 关闭 cURL 会话并返回 false curl_close($ch); return false; } // 如果 HTTP 响应码大于等于 400(表示错误) elseif ($httpCode >= 400) { // 输出错误信息 echo "HTTP error: $httpCode"; // 关闭 cURL 会话并返回 false curl_close($ch); return false; } // 处理是否获取请求头内容 if ($headerContent && $httpCode == 200) { $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $headers = substr($response, 0, $headerSize); $body = substr($response, $headerSize); curl_close($ch); return [$headers, $body]; } // 关闭 cURL 会话 curl_close($ch); // 处理是否将响应转换为数组 if ($flbg) { $response = json_decode($response, true); } // 返回响应内容 return $response; } }
2025年04月23日
39 阅读
0 评论
0 点赞
2025-03-13
【PHP】通联支付 通企付 生产签名 PHP版本
/** * @Author:小破孩 * @Email:3584685883@qq.com * @Time:2025/2/17 15:25 * @param $array * @return string * @Description:数组以key=value&key=value 返回字符串 */ public function arrayKeyValueToString($array) { $result = ''; foreach ($array as $key => $value) { $result.= $key. '='. $value. '&'; } // 去除末尾多余的 & 符号 return rtrim($result, '&'); } /** * @Author:小破孩 * @Email:3584685883@qq.com * @Time:2025/2/14 15:32 * @return string * @Description:生产签名 */ public function setSign() { $data = [ 'mchNo' => $this->payConfig['tppay_mchid'], // 商户号 'appId' => $this->payConfig['tppay_appid'], // appid 'reqTime' => $this->currentTimestamp, // 13位时间戳 'version' => "1.0", // 固定值 'signType' => 'RSA', // 验签方式 'mchOrderNo' => $this->orderNo, // 订单号 'amount' => (string)$this->amount, // 金额 单位 分 'body' => $this->body, // 商品描述 'notifyUrl' => $this->getNotifyUrl(), // 回调通知地址 'expiredTime' => '1800', // 订单超时支付时间 单位 秒 'channelExtra' => $this->channelExtra, 'payTypeInfo' => (string)$this->payTypeInfo(), // 收银台展示的付款方式 // 'directPayType'=> (string)$this->getPayType(), // 直接支付的支付方式 ]; ksort($data); // Log::write("发起签名的参数:".var_export($data,true),"tppay"); $instanceArr = new \app\common\lib\data\Arr(); $encodedParams = $instanceArr->arrayKeyValueToString($data); Log::write("处理后的签名字符串:".PHP_EOL.var_export($encodedParams,true),"tppay"); $privateKey = "-----BEGIN PRIVATE KEY-----\n" . $this->payConfig['tppay_rsa_private_key'] . "\n-----END PRIVATE KEY-----"; $publicKey = "-----BEGIN PUBLIC KEY-----\n" . $this->payConfig['tppay_rsa_public_key'] . "\n-----END PUBLIC KEY-----"; // Log::write("发起签名的私钥:".var_export($privateKey,true),"tppay"); $instanceRsa = new \app\common\lib\pay\tppay\Rsa(null, null, $privateKey, $publicKey); $encryptedWithPrivate = $instanceRsa->sign($encodedParams); //签名使用SHA1withRSA // Log::write("签名的结果:".var_export($encryptedWithPrivate,true),"tppay"); return $encryptedWithPrivate; }
2025年03月13日
81 阅读
0 评论
0 点赞
2025-03-13
【PHP】ThinkPHP6.1 参数验证中间件
public function handle($request, \Closure $next) { try { // 获取并清理参数 $params = array_filter(array_map(function ($value) { return is_string($value) ? trim($value) : $value; }, $request->param()), function ($value) { return is_numeric($value) || !empty($value); }); unset($params['controller'], $params['function']); if (empty($params)) return $next($request); // 设置请求属性,方便后续使用 $request->checkParam = $params; // 获取应用名、控制器和操作名 $appName = app('http')->getName(); $controller = Request::instance()->controller(true); $action = Request::instance()->action(true); // 动态构建验证器路径 $controllerParts = explode('.', $controller); $validatePathParts = array_merge([$appName, 'validate'], $controllerParts); $lastKey = array_key_last($validatePathParts); $validatePathParts[$lastKey] = ucfirst((string) $validatePathParts[$lastKey]); // $validatePath = implode('\\', array_map('ucfirst', $validatePathParts)); $validatePath = 'app\\'.implode('\\', $validatePathParts); // 检查验证器是否存在及场景是否定义 if (!class_exists($validatePath) || !$this->sceneExists($validatePath, $action)) { return $next($request); } // 验证数据 $validateInstance = new $validatePath; if (!$validateInstance->scene($action)->check($params)) { throw new Exception($validateInstance->getError()); } } catch (Exception $e) { return show(100, $e->getMessage()); } return $next($request); } /** * 检查指定验证场景是否存在 * * @param string $validateClass 验证类名 * @param string $scene 场景名 * @return bool */ protected function sceneExists(string $validateClass, string $scene): bool { return (new $validateClass)->hasScene($scene); }
2025年03月13日
156 阅读
0 评论
0 点赞
2025-03-13
【PHP】发送腾讯云短信
优化空间很大,先用着,能用<?php namespace app\common\lib\sms\tencent; //缓存 use think\facade\Cache; use TencentCloud\Common\Credential; use TencentCloud\Common\Profile\ClientProfile; use TencentCloud\Common\Profile\HttpProfile; use TencentCloud\Common\Exception\TencentCloudSDKException; use TencentCloud\Sms\V20210111\SmsClient; use TencentCloud\Sms\V20210111\Models\SendSmsRequest; class Sms{ public $SecretID = "......................"; public $SecretKey = "......................."; public $SmsSdkAppId = "..........."; public $TemplateId = "........."; public $SignName = "............"; public $code; public $phone; public function __construct($phone = '', $code = '', $tempID = '') { $this->phone = $phone; $this->code = $code; if(!empty($tempID)){ $this->TemplateId = $tempID; } } public function send(){ try { //控制台 >API密钥管理页面获取 SecretID 和 SecretKey $cred = new Credential($this->SecretID, $this->SecretKey); //实例化一个http选项 [可选] $httpProfile = new HttpProfile(); $httpProfile->setEndpoint("sms.tencentcloudapi.com"); //实例化一个client选项 [可选] $clientProfile = new ClientProfile(); $clientProfile->setHttpProfile($httpProfile); /** * 实例化以sms为例的client对象, [第三个参数 可选] * * 第二个参数是地域信息,可以直接填 ap-guangzhou */ $client = new SmsClient($cred, "ap-beijing", $clientProfile); // 实例化一个sms发送短信请求对象,每个接口都会对应一个request对象。 $req = new SendSmsRequest(); //生成随机验证码 // $code = rand(11111, 99999); // $params = array( // //接收方手机号,带上+86 示例:+8613711112222 // "PhoneNumberSet" => array((string)$this->phone), // //短信应用ID:在 [短信控制台] 添加应用后生成的实际SdkAppId // "SmsSdkAppId" => (string)$this->SmsSdkAppId, // //短信签名内容:[不理解可以看文章里的截图] // "SignName" => (string)$this->SignName, // //模板ID:必须填写已审核通过的模板 // "TemplateId" => (string)$this->TemplateId, // //我的模板中有两个参数 第一个是验证码参数 第二个是有效时间 若无模板参数,则设置为空 // "TemplateParamSet" => array((string)$this->code, '10'), // //SecretID // // "SenderId" => (string)$this->SecretID // ); $params = array( "PhoneNumberSet" => array( (string)$this->phone ), "SmsSdkAppId" => (string)$this->SmsSdkAppId, "SignName" => (string)$this->SignName, "TemplateId" => (string)$this->TemplateId, "TemplateParamSet" => array( (string)$this->code), // "SenderId" => (string)$this->SecretID ); $req->fromJsonString(json_encode($params)); //发出请求,返回一个实例 $resp = $client->SendSms($req); // print_r($resp);die; //如果成功,把验证码存入缓存 //成功实例中的Code值为 Ok if ($resp->SendStatusSet[0]->Code === "Ok") { return true; // Cache::set('name', $code, 600); // return json(['msg' => "发送成功", 'code' => 200]); } } catch (TencentCloudSDKException $e) { echo $e; } } }
2025年03月13日
67 阅读
0 评论
0 点赞
2025-03-13
【PHP】打印猿&蜂打打 开放平台 完整对接
基础类<?php namespace app\common\lib\printman; use think\facade\Log; class Basic { #APPID public $AppId; #密钥 public $AppSecret; #API地址 public $ApiUrl; #打印机ID public $PrinterId; public function __construct($AppId, $AppSecret, $PrinterId) { $this->AppId = $AppId; $this->AppSecret = $AppSecret; $this->ApiUrl = "https://iot-app-prod.fengdada.cn/mk/api"; $this->PrinterId = $PrinterId; } public function encode($BizData, $nonce) { // global $AppSecret; $jsonBytes = mb_convert_encoding($BizData , 'utf-8'); $bizData = strval($jsonBytes); $sign_ori = $bizData . $nonce . $this->AppSecret; $md5_hash = md5($sign_ori, true); $sign = base64_encode($md5_hash); return $sign; } public function generate_verification_code() { $verification_code = ""; for ($i = 0; $i < 6; $i++) { $verification_code .= strval(rand(0, 9)); } return $verification_code; } public function requests_post($url, $data, $headers) { $ch = curl_init(); curl_setopt($ch, CURLOPT_CAINFO, "cacert-2023-01-10.pem"); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); return $response; } public function DoMyPost($URL, $json_BizData) { $ts = round(microtime(true) * 1000); $nonce = $this->generate_verification_code(); $sign = $this->encode($json_BizData, $nonce); $data = array( "bizData" => $json_BizData, "nonce" => $nonce, "appId" => $this->AppId, "timestamp" => strval($ts) ); $headers = array( 'Content-Type: application/json', 'sign:'.$sign ); // $response = $this->requests_post($URL, json_encode($data), $headers); $response = $this->curlRequest($URL, "POST",json_encode($data),true,false, $headers); Log::write("打印机日志:".print_r($response,true),'printman'); if(empty($response)) { echo "Error: no response received"; }else{ return $response; } } // 云打印验证码 public function PrintCaptcha() { $URL = $this->ApiUrl."/print/captcha"; $BizData = array('printerId' => $this->PrinterId); $json_BizData = json_encode($BizData); return $this->DoMyPost($URL, $json_BizData); } // 云打印机绑定 public function PrintBind($VerificationCode) { $URL = $this->ApiUrl."/printer/bind"; $BizData = array('printerId' => $this->PrinterId, 'captcha' => $VerificationCode); $json_BizData = json_encode($BizData); return $this->DoMyPost($URL, $json_BizData); } // 云打印 public function CloudPrint( $ShareCode, $PrintDataList) { $URL = $this->ApiUrl."/print"; $BizData = array('printerId' => $this->PrinterId, 'shareCode' => $ShareCode, 'printData' => $PrintDataList); $json_BizData = json_encode($BizData); return $this->DoMyPost($URL, $json_BizData); } // 云打印状态查询 public function QueryPrintStatus($ShareCode) { $URL = $this->ApiUrl."/printer/status/query"; $BizData = array('printerId' => $this->PrinterId, 'shareCode' => $ShareCode); $json_BizData = json_encode($BizData); return $this->DoMyPost($URL, $json_BizData); } //云打印解绑//0标识解绑失败,1标识解绑成功 public function unbind($ShareCode){ $URL = $this->ApiUrl."/printer/unbind"; $BizData = array('printerId' => $this->PrinterId, 'shareCode' => $ShareCode); $json_BizData = json_encode($BizData); return $this->DoMyPost($URL, $json_BizData); } /** * @Author: 小破孩嫩 * @Email: 3584685883@qq.com * @Time: 2021/4/1 10:39 * @param string $url url地址 * @param string $method 请求方法,默认为 'GET',可选值为 'GET' 或 'POST' * @param mixed $data 要发送的数据,如果是 POST 请求则为数据内容,否则为 null * @param array $headers 自定义请求头信息 * @param int $timeout 超时时间,默认为 30 秒 * @param bool $verifySSL 是否验证 SSL 证书,默认为 true * @param bool $flbg 返回值是否转成数组,默认不转 * @param bool $headercontent 是否获取请求的header值内容,默认不获取 * @return array|bool|mixed|string * @Description:curl请求 */ protected function curlRequest($url, $method = 'GET', $data = null, $flbg = false, $verifySSL = true, $headers = [], $headerContent = false, $timeout = 30) { // 初始化 cURL 会话 $ch = curl_init(); // 设置要请求的 URL curl_setopt($ch, CURLOPT_URL, $url); // 设置获取的信息以字符串形式返回,而不是直接输出 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 设置超时时间 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // 设置请求方法 if ($method === 'POST') { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } // 设置请求头 if (!empty($headers)) { curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } // 设置是否验证 SSL 证书 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verifySSL); // 执行 cURL 会话并获取响应 $response = curl_exec($ch); // 获取 HTTP 响应码 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 如果 cURL 执行出错 if (curl_errno($ch)) { // 输出错误信息 echo 'Curl error: ' . curl_error($ch); // 关闭 cURL 会话并返回 false curl_close($ch); return false; } // 如果 HTTP 响应码大于等于 400(表示错误) elseif ($httpCode >= 400) { // 输出错误信息 echo "HTTP error: $httpCode"; // 关闭 cURL 会话并返回 false curl_close($ch); return false; } // 处理是否获取请求头内容 if ($headerContent && $httpCode == 200) { $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $headers = substr($response, 0, $headerSize); $body = substr($response, $headerSize); curl_close($ch); return [$headers, $body]; } // 关闭 cURL 会话 curl_close($ch); // 处理是否将响应转换为数组 if ($flbg) { $response = json_decode($response, true); } // 返回响应内容 return $response; } /** * @Author:小破孩 * @Email:3584685883@qq.com * @Time:2025/1/10 14:02 * @param $code * @return string * @Description:打印机错误码 */ public function errorCode($code){ $errorCodes = [ 200 => "success 成功", 500 => "sys fail 系统异常", 2001 => "sign check fail 签名失败", 2002 => "not find partner 查无合作伙伴", 2003 => "illegal access 非法访问", 3001 => "param check error 参数错误", 3002 => "please input params 请输入参数", 40001 => "please input APPID 请输入appid", 40002 => "biz exception 业务异常", 40003 => "printer xxx is offline 打印机离线", 40004 => "printer xxx is not auth 打印机未授权", 40005 => "shareCode is error 分享码错误", 40006 => "printer xxx after 5 minutes reprinting 请5分钟后重试", 40007 => "printer xxx captcha error 验证码错误", 40008 => "printer xxx captcha expired 验证码过期", 40009 => "printer xxx bind fail 绑定失败", 40023 => "lip not close 盖子未闭合", 40023 => "sticker 粘纸", 40023 => "sticker and lip not close 粘纸并且盖子未闭合", 40023 => "no page 缺纸", 40023 => "no page and lip not close 缺纸并且盖子未闭合", 40023 => "temperature too high 温度过高", 40023 => "temperature too high and lip not close 温度过高且盖子未闭合", 40023 => "temperature too high and sticker 温度过高且粘纸", 40023 => "temperature too high and lip not close and sticker 温度过高且粘纸,盖子未闭合", 40023 => "command error 指令错误" ]; return $errorCodes[$code]; } }模板<?php namespace app\common\lib\printman\template; use app\common\lib\printman\Template; class Temp1 implements Template { protected $id; public function __construct($id = "") { $this->id = $id; } /** * @Author:小破孩 * @Email:3584685883@qq.com * @Time:2025/1/10 10:51 * @return string * @Description:打印模板1 */ public function temp($order = ""){ if(empty($order)){ return "没有订单信息"; } $height = ceil(self::getTempHight($order)/10+50); $i = 40; $template = ""; $template .= "SIZE 72 mm, ".$height." mm\r\n"; $template .= "CODEPAGE 437\r\n"; $template .= "DENSITY 8\r\n"; $template .= "CLS \r\n"; $template .= "CODEPAGE 936\r\n"; $template .= "DIRECTION 0\r\n"; $template .= "TEXT 220,0,\"4\",0,1,1,\""."订单详情"."\"\r\n"; //小票标题 $template .= "TEXT 220,".$i.",\"4\",0,1,1,\"".""."\"\r\n"; //换行 $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"订单编号:".$order['o_uuid']."\"\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"打印时间:".date("Y-m-d H:i:s")."\"\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"申 请 人:".$order['o_address_name']."\"\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"联系方式:".$order['o_address_tel_default']."\"\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"收货地址:".mb_substr($order['o_address_info'],0,16)."\"\r\n"; if(mb_strlen($order['o_address_info']) > 16){ $template .= "TEXT 40,"; $template .= $i+=30; $template .=",\"0\",0,1,1,\" ".mb_substr($order['o_address_info'],16)."\"\r\n"; } $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"申请时间:".$order['o_create_time']."\"\r\n"; // $template .= "TEXT 40,"; // $template .= $i+=30; // $template .= ",\"0\",0,1,1,\"审核时间:".date("Y-m-d H:i:s",$order['o_pay_receipt_allow_time'])."\"\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"配 送 员:".$order['salesmaninfo']['sm_name']."\"\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"配送电话:".$order['salesmaninfo']['sm_phone']."\"\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"出货仓库:".$order['warehouse_name']."\"\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"商品总数:".$order['goods_total_num']."\"\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"商品总价:".$order['o_real_price']."\"\r\n"; $template .= "BAR 20,"; $template .= $i+=28; $template .= ",720,2\r\n"; $template .= "TEXT 40,"; $template .= $i+=16; $template .= ",\"0\",0,1,1,\"商品 数量 单价 金额\"\r\n"; $template .= "BAR 20,"; $template .= $i+=28; $template .= ",720,2\r\n"; foreach ($order['order_list'] as $kk => $vv){ if(!empty($vv['oi_issendgoods'])){ $firstNamaText = "赠品:"; }else{ $firstNamaText = "商品:"; } $knum = $kk+=1; $template .= "TEXT 30,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"$knum.". mb_substr($firstNamaText.$vv['oi_sku_info']['goods_info']['withgoodsinfoinfo']['sg_name'].'('.$vv['oi_sku_info']['sgcs_name'].')',0,26)."\"\r\n"; if(mb_strlen($firstNamaText.$vv['oi_sku_info']['goods_info']['withgoodsinfoinfo']['sg_name'].'('.$vv['oi_sku_info']['sgcs_name'].')') > 26){ $template .= "TEXT 30,"; $template .= $i+=30; $template .=",\"0\",0,1,1,\" ".mb_substr($firstNamaText.$vv['oi_sku_info']['goods_info']['withgoodsinfoinfo']['sg_name'].'('.$vv['oi_sku_info']['sgcs_name'].')',26)."\"\r\n"; } $template .= "TEXT 65,"; $template .= $i+=30; if(!empty($vv['oi_issendgoods'])){ $template .=",\"0\",0,1,1,\"".$vv['oi_sku_info']['num'].'件'.' '."0.00".' '."0.00"."\"\r\n"; }else{ $template .=",\"0\",0,1,1,\"".$vv['oi_sku_info']['num'].'件'.' '.sprintf("%.2f",$vv['oi_sku_info']['sgcs_price']/$vv['oi_sku_info']['num']).' '.$vv['oi_real_price']."\"\r\n"; } } $template .= "BAR 20,"; $template .= $i+=28; $template .= ",720,2\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"数量总计:".$order['goods_total_num'].'件'."\"\r\n"; $template .= "PRINT 1,1"; $instacneStr = new \app\common\lib\data\Str(); $data = [ 'waybillPrinterData' => $instacneStr->gzipAndBase64Encode($template), 'printType' => 'tspl', 'id' => $this->id ]; return [$data]; } protected function getTempHight($order){ $i = 50; $template = ""; $template .= "SIZE 72 mm, 90 mm\r\n"; $template .= "CODEPAGE 437\r\n"; $template .= "DENSITY 8\r\n"; $template .= "CLS \r\n"; $template .= "CODEPAGE 936\r\n"; $template .= "DIRECTION 0\r\n"; $template .= "TEXT 220,0,\"4\",0,1,1,\""."订单详情"."\"\r\n"; //小票标题 $template .= "TEXT 220,".$i.",\"4\",0,1,1,\"".""."\"\r\n"; //换行 $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"订单编号:".$order['o_uuid']."\"\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"打印时间:".date("Y-m-d H:i:s")."\"\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"申 请 人:".$order['o_address_name']."\"\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"联系方式:".$order['o_address_tel_default']."\"\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"收货地址:".mb_substr($order['o_address_info'],0,16)."\"\r\n"; if(mb_strlen($order['o_address_info']) > 16){ $template .= "TEXT 40,"; $template .= $i+=30; $template .=",\"0\",0,1,1,\" ".mb_substr($order['o_address_info'],16)."\"\r\n"; } $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"申请时间:".$order['o_create_time']."\"\r\n"; // if(empty($order['o_help'])){ // $template .= "TEXT 40,"; // $template .= $i+=30; // $template .= ",\"0\",0,1,1,\"审核时间:".date("Y-m-d H:i:s",$order['o_pay_receipt_allow_time'])."\"\r\n"; // } $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"配送员姓名:".$order['salesmaninfo']['sm_name']."\"\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"配送员电话:".$order['salesmaninfo']['sm_phone']."\"\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"出货仓库:".$order['warehouse_name']."\"\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"商品总数:".$order['goods_total_num']."\"\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"商品总价:".$order['o_real_price']."\"\r\n"; $template .= "BAR 20,"; $template .= $i+=28; $template .= ",720,2\r\n"; $template .= "TEXT 40,"; $template .= $i+=16; $template .= ",\"0\",0,1,1,\"商品 数量 单价 金额\"\r\n"; $template .= "BAR 20,"; $template .= $i+=28; $template .= ",720,2\r\n"; // foreach ($order as $key => $val){ foreach ($order['order_list'] as $kk => $vv){ if(!empty($vv['oi_issendgoods'])){ $firstNamaText = "赠品:"; }else{ $firstNamaText = "商品:"; } $knum = $kk+=1; $template .= "TEXT 30,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"$knum.". mb_substr($firstNamaText.$vv['oi_sku_info']['goods_info']['withgoodsinfoinfo']['sg_name'].'('.$vv['oi_sku_info']['sgcs_name'].')',0,26)."\"\r\n"; if(mb_strlen($firstNamaText.$vv['oi_sku_info']['goods_info']['withgoodsinfoinfo']['sg_name'].'('.$vv['oi_sku_info']['sgcs_name'].')') > 26){ $template .= "TEXT 30,"; $template .= $i+=30; $template .=",\"0\",0,1,1,\" ".mb_substr($firstNamaText.$vv['oi_sku_info']['goods_info']['withgoodsinfoinfo']['sg_name'].'('.$vv['oi_sku_info']['sgcs_name'].')',26)."\"\r\n"; } $template .= "TEXT 65,"; $template .= $i+=30; $template .=",\"0\",0,1,1,\"".$vv['oi_sku_info']['num'].'件'.' '.$vv['oi_sku_info']['sgcs_price'].' '.$vv['oi_sku_info']['sgcs_price']*$vv['oi_sku_info']['num']."\"\r\n"; } $template .= "BAR 20,"; $template .= $i+=28; $template .= ",720,2\r\n"; $template .= "TEXT 40,"; $template .= $i+=30; $template .= ",\"0\",0,1,1,\"数量总计:".$order['goods_total_num']."\"\r\n"; // } $template .= "PRINT 1,1"; return $i; } }
2025年03月13日
202 阅读
0 评论
1 点赞
2025-03-13
【PHP】给富文本内容的图片,视频,文件 拼接当前网址域名
/** * @Author:小破孩 * @Email:3584685883@qq.com * @Time:2024/11/18 15:20 * @param $text * @param $domain * @return string|string[]|null * @Description:给服务文本拼接当前网址域名 */ public function addDomainToPaths($text, $domain){ // 匹配图片路径 $text = preg_replace('/<img.*?src="([^"]+)"/i', '<img src="' . $domain . '$1"', $text); // 匹配视频路径 $text = preg_replace('/<video.*?src="([^"]+)"/i', '<video src="' . $domain . '$1"', $text); // 匹配文件路径(可根据具体文件类型的链接特征进行修改) $text = preg_replace('/<a.*?href="([^"]+)"/i', '<a href="' . $domain . '$1"', $text); return $text; }
2025年03月13日
129 阅读
0 评论
0 点赞
2025-03-13
【PHP】过滤富文本内容
封装了一个类class TextFilter { // 定义要过滤的 SQL 关键字模式 const SQL_PATTERNS = [ '/\b(SELECT|INSERT|UPDATE|DELETE|FROM|WHERE|AND|OR|JOIN|DROP|CREATE|ALTER|TRUNCATE|GRANT|REVOKE|SET)\b/i', '/\b(AS|LIKE|NOT|IN|BETWEEN|IS|NULL|COUNT|SUM|AVG|MIN|MAX)\b/i', '/\b(UNION|ALL|ANY|EXISTS)\b/i', '/\b(ORDER\s+BY|LIMIT)\b/i' ]; // 定义要过滤的常见函数模式 const FUNCTION_PATTERNS = [ '/\b(function\s+\w+\s*\([^)]*\))\b/i', '/\b(eval|exec|system|passthru|shell_exec|assert)\b/i' ]; // 定义要过滤的特殊字符和表达式模式 const SPECIAL_PATTERNS = [ '/\$\{.*?\}/', // 过滤类似 ${expression} 的表达式 '/@.*?;/', // 过滤以 @ 开头并以 ; 结尾的表达式 '/\b(phpinfo|var_dump)\b/i', // 过滤特定的 PHP 函数 '/<\s*(script|iframe|object|embed|applet)[^>]*>/i' // 过滤危险的脚本标签 ]; // 定义要过滤的危险属性模式 const DANGEROUS_ATTRIBUTES_PATTERNS = [ '/on\w+\s*=/i', // 过滤以 "on" 开头的事件属性 '/javascript:[^"]*"/i' // 过滤 JavaScript 协议的链接 ]; /** * @Author:小破孩 * @Email:3584685883@qq.com * @Time:2024/10/24 13:50 * @param $text * @return string|string[]|null * @Description:过滤富文本 */ public static function filterRichText($text) { // 合并所有要过滤的模式 $allPatterns = array_merge( self::SQL_PATTERNS, self::FUNCTION_PATTERNS, self::SPECIAL_PATTERNS, self::DANGEROUS_ATTRIBUTES_PATTERNS ); // 先过滤所有匹配的模式 $filteredText = preg_replace($allPatterns, '', $text); // 保留 <img> 标签,但需要确保 src 属性是安全的 $filteredText = preg_replace_callback('/<img[^>]+>/i', [__CLASS__, 'filterImgTag'], $filteredText); // 允许表情符号和其他图标 $filteredText = preg_replace('/[\x{1F600}-\x{1F64F}]|\x{1F300}-\x{1F5FF}|\x{1F680}-\x{1F6FF}|\x{2600}-\x{26FF}|\x{2700}-\x{27BF}/u', '$0', $filteredText); // 处理可能出现的连续空格 $filteredText = preg_replace('/\s+/', ' ', $filteredText); // 去除前后的空格 $filteredText = trim($filteredText); // 转换 HTML 实体 $filteredText = htmlentities($filteredText, ENT_QUOTES, 'UTF-8'); return $filteredText; } private static function filterImgTag($matches) { $imgTag = $matches[0]; if (preg_match('/src=["\'](?<src>[^"\']+)["\']/i', $imgTag, $srcMatch)) { $src = $srcMatch['src']; // 这里可以进一步验证 src 是否是允许的 URL 或本地路径 if (filter_var($src, FILTER_VALIDATE_URL) || strpos($src, '/') === 0) { return $imgTag; } } return ''; } } // 示例调用 $text = '<script>alert("XSS")</script><img src="https://example.com/image.jpg">'; $filteredText = TextFilter::filterRichText($text); echo $filteredText; 函数 方法 /** * @Author:小破孩 * @Email:3584685883@qq.com * @Time:2024/10/24 13:50 * @param $text * @return string|string[]|null * @Description:过滤富文本 */ public static function filterRichText($text){ // 定义要过滤的 SQL 关键字模式 $sqlPatterns = [ '/\b(SELECT|INSERT|UPDATE|DELETE|FROM|WHERE|AND|OR|JOIN|DROP|CREATE|ALTER|TRUNCATE|GRANT|REVOKE|SET)\b/i', '/\b(AS|LIKE|NOT|IN|BETWEEN|IS|NULL|COUNT|SUM|AVG|MIN|MAX)\b/i', '/\b(UNION|ALL|ANY|EXISTS)\b/i', '/\b(ORDER\s+BY|LIMIT)\b/i' ]; // 定义要过滤的常见函数模式 $functionPatterns = [ '/\b(function\s+\w+\s*\([^)]*\))\b/i', '/\b(eval|exec|system|passthru|shell_exec|assert)\b/i' ]; // 定义要过滤的特殊字符和表达式模式 $specialPatterns = [ '/\$\{.*?\}/', // 过滤类似 ${expression} 的表达式 '/@.*?;/', // 过滤以 @ 开头并以 ; 结尾的表达式 '/\b(phpinfo|var_dump)\b/i', // 过滤特定的 PHP 函数 '/<\s*(script|iframe|object|embed|applet)[^>]*>/i' // 过滤危险的脚本标签 ]; // 定义要过滤的危险属性模式 $dangerousAttributesPatterns = [ '/on\w+\s*=/i', // 过滤以 "on" 开头的事件属性 '/javascript:[^"]*"/i' // 过滤 JavaScript 协议的链接 ]; // 先过滤 SQL 关键字 $filteredText = preg_replace($sqlPatterns, '', $text); // 再过滤函数 $filteredText = preg_replace($functionPatterns, '', $filteredText); // 然后过滤特殊字符和表达式 $filteredText = preg_replace($specialPatterns, '', $filteredText); // 接着过滤危险的属性 $filteredText = preg_replace($dangerousAttributesPatterns, '', $filteredText); // 允许表情符号和其他图标 $filteredText = preg_replace('/[\x{1F600}-\x{1F64F}]|\x{1F300}-\x{1F5FF}|\x{1F680}-\x{1F6FF}|\x{2600}-\x{26FF}|\x{2700}-\x{27BF}/u', '$0', $filteredText); // 处理可能出现的连续空格 $filteredText = preg_replace('/\s+/', ' ', $filteredText); // 去除前后的空格 $filteredText = trim($filteredText); // 转换 HTML 实体 $filteredText = htmlentities($filteredText, ENT_QUOTES, 'UTF-8'); return $filteredText; } /** * @Author:小破孩 * @Email:3584685883@qq.com * @Time:2024/10/24 13:50 * @param $text * @return string|string[]|null * @Description:过滤富文本 */ function filterRichText($text) { // 合并所有要过滤的模式 $patterns = [ '/\b(SELECT|INSERT|UPDATE|DELETE|FROM|WHERE|AND|OR|JOIN|DROP|CREATE|ALTER|TRUNCATE|GRANT|REVOKE|SET)\b/i', '/\b(AS|LIKE|NOT|IN|BETWEEN|IS|NULL|COUNT|SUM|AVG|MIN|MAX)\b/i', '/\b(UNION|ALL|ANY|EXISTS)\b/i', '/\b(ORDER\s+BY|LIMIT)\b/i', '/\b(function\s+\w+\s*\([^)]*\))\b/i', '/\b(eval|exec|system|passthru|shell_exec|assert)\b/i', '/\$\{.*?\}/', '/@.*?;/', '/\b(phpinfo|var_dump)\b/i', '/<\s*(script|iframe|object|embed|applet)[^>]*>/i', '/on\w+\s*=/i', '/javascript:[^"]*"/i' ]; // 先过滤所有匹配的模式 $filteredText = preg_replace($patterns, '', $text); // 允许表情符号和其他图标 $filteredText = preg_replace('/[\x{1F600}-\x{1F64F}]|\x{1F300}-\x{1F5FF}|\x{1F680}-\x{1F6FF}|\x{2600}-\x{26FF}|\x{2700}-\x{27BF}/u', '$0', $filteredText); // 处理可能出现的连续空格 $filteredText = preg_replace('/\s+/', ' ', $filteredText); // 去除前后的空格 $filteredText = trim($filteredText); // 转换 HTML 实体 $filteredText = htmlentities($filteredText, ENT_QUOTES, 'UTF-8'); return $filteredText; } // 示例调用 $text = '<script>alert("XSS")</script><img src="https://example.com/image.jpg">'; $filteredText = filterRichText($text); echo $filteredText;
2025年03月13日
102 阅读
0 评论
0 点赞
1
2
...
5