首页
关于小站
朋友
壁纸
留言
时光之书
笔顺字帖
LayUI手册
Search
1
【PHP】PHPoffice/PHPSpreadsheet读取和写入Excel
1,703 阅读
2
【Layui】控制页面元素展示隐藏
1,555 阅读
3
【Git】No tracked branch configured for branch master or the branch doesn't exist.
1,493 阅读
4
【PHP】PHP实现JWT生成和验证
1,401 阅读
5
精准检测,助力社交管理 —— 微信好友检测服务来袭!
1,301 阅读
默认分类
PHP
ThinkPHP
Laravel
面向对象
设计模式
算法
基础
网络安全
Web
HTML
CSS
JavaScript
jQuery
Layui
VUE
uni-app
Database
MySQL
Redis
RabbitMQ
Nginx
Git
Linux
Soft Ware
Windows
网赚
Go
Docker
登录
Search
标签搜索
PHP
函数
方法
类
MySQL
ThinkPHP
JavaScript
OOP
Layui
Web
Server
Docker
Linux
PHPSpreadsheet
PHPoffice
Array
设计模式
Nginx
Git
排序算法
小破孩
累计撰写
254
篇文章
累计收到
13
条评论
首页
栏目
默认分类
PHP
ThinkPHP
Laravel
面向对象
设计模式
算法
基础
网络安全
Web
HTML
CSS
JavaScript
jQuery
Layui
VUE
uni-app
Database
MySQL
Redis
RabbitMQ
Nginx
Git
Linux
Soft Ware
Windows
网赚
Go
Docker
页面
关于小站
朋友
壁纸
留言
时光之书
笔顺字帖
LayUI手册
搜索到
69
篇与
的结果
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日
9 阅读
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日
29 阅读
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日
28 阅读
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日
24 阅读
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日
28 阅读
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日
24 阅读
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日
21 阅读
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日
65 阅读
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日
58 阅读
0 评论
0 点赞
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日
114 阅读
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日
94 阅读
0 评论
0 点赞
2025-03-13
【PHP】获取二维数组里面最小的值
/** * @Author:小破孩 * @Email:3584685883@qq.com * @Time:2024/12/5 17:16 * @param $array * @return array * @Description:获取一个二维数组,数据最小的,并返回对应的key和value */ public function getMinValueKey($array) { $minValue = PHP_INT_MAX; $desiredKey = null; foreach ($array as $key => $subArray) { foreach ($subArray as $subKey => $value) { if ($value < $minValue) { $minValue = $value; $desiredKey = $subKey; } } } return [$desiredKey, $minValue]; } //使用场景 $inatanceMap = new \app\common\lib\map\baidu\Lnglat($this->param['ac_address']); $lnglat = $inatanceMap->addressToLngLat(); $this->param['u_lng'] = $lnglat['lng'];//经度 $this->param['u_lat'] = $lnglat['lat'];//纬度 $companyList = M("AdminCompany")::getCompanyListUseSelect(); $instanceDis = new \app\common\lib\map\Distance(); foreach ($companyList as $key => $val){ $arrAddress[$key][$val['ac_uuid']] = $instanceDis->getdistance($val['ac_lng'],$val['ac_lat'],$lnglat['lng'],$lnglat['lat']); } $instanceArr = new \app\common\lib\data\Arr(); list($minKey, $minValue) = $instanceArr->getMinValueKey($arrAddress); $this->param['u_company_uuid'] = $minKey; $this->param['u_address'] = $this->param['ac_address'];
2025年03月13日
94 阅读
0 评论
0 点赞
2025-03-13
【PHP】按照个商品金额,等比例分配优惠劵
/** * @Author:小破孩 * @Email:3584685883@qq.com * @Time:2024/11/23 11:41 * @param $products ['id' => 'price','id' => price] * @param $totalCouponAmount 优惠劵优惠金额 * @return array * @Description:按照商品比例拆分优惠劵,分配给对应的商品 */ public function getSplitCoupon($products, $totalCouponAmount) { $totalAmount = array_sum($products); $discounts = []; $allocatedDiscount = 0; foreach ($products as $id => $amount) { $ratio = $amount / $totalAmount; $discount = $ratio * $totalCouponAmount; $roundedDiscount = round($discount, 2); $discounts[$id] = $roundedDiscount; $allocatedDiscount += $roundedDiscount; } // 调整以使总和为指定的优惠券总额 $diff = $totalCouponAmount - $allocatedDiscount; if ($diff!= 0) { $sortedDiscounts = $discounts; arsort($sortedDiscounts); $i = 0; foreach ($sortedDiscounts as $id => $discount) { if ($i < abs($diff)) { $discounts[$id] += ($diff > 0)? 0.01 : -0.01; } $i++; } } return $discounts; }
2025年03月13日
96 阅读
0 评论
0 点赞
2023-07-20
【PHP】打开文件路径的所有文件
<?php function open_dir($path) { if (!is_dir($path) || empty($path)) die("该" . $path . "不是目录"); $path = $path . '/'; $fileList = []; $dirFile = scandir($path); if (!empty($dirFile) && is_array($dirFile)) { foreach ($dirFile as $file) { if ($file != '.' && $file != '..') { $fullPath = $path . $file; if (is_dir($fullPath)) { // 子文件夹,进行递归 $fileList[$file] = open_dir($fullPath); } else { //根目录下的文件 $fileList[] = $file; } } } } return $fileList; } $list = open_dir("/www/wwwroot/8688web.com/one"); // echo '<pre>'; // print_r($list); // echo '</pre>'; ?>
2023年07月20日
287 阅读
0 评论
0 点赞
2023-06-09
【PHP】PHP实现二维数组转一维数组
可以使用 array_reduce() 函数将二维数组转换为一维数组。array_reduce() 函数将通过一个回调函数迭代数组中的所有值,并将它们合并为一个单一的值。在这种情况下,我们将使用 array_merge() 函数将所有子数组合并为一个单一的数组。示例代码如下:function flatten_array($arr) { return array_reduce($arr, function($carry, $item) { return array_merge($carry, is_array($item) ? flatten_array($item) : array($item)); }, array()); } // 测试 $arr = array( array(1, 2, 3), array(4, 5, 6), array( array(7, 8), 9, array(10, 11, 12), ), ); $result = flatten_array($arr); print_r($result); // 输出: // Array // ( // [0] => 1 // [1] => 2 // [2] => 3 // [3] => 4 // [4] => 5 // [5] => 6 // [6] => 7 // [7] => 8 // [8] => 9 // [9] => 10 // [10] => 11 // [11] => 12 // )在上面的示例代码中,我们定义了一个 flatten_array() 函数,该函数使用 array_reduce() 函数和递归调用来将二维数组转换为一维数组。
2023年06月09日
433 阅读
0 评论
0 点赞
2023-05-08
【PHP】PHP实现金额检测
public function checkMoney($value) { if (!is_numeric($value)) { return false; } if ($value <= 0) { return false; } if (preg_match('/^[0-9]+(\.\d{1,2})?$/',$value)) { return true; } else { return false; } } 优化 public function checkMoney($value) { if(is_array($value)){ foreach ($value as $v){ $this->checkMoney($v); } }else{ if (!is_numeric($value)) { return $this->show('100','金额不正确'); } if ($value <= 0) { return $this->show('100','金额不正确'); } if (preg_match('/^[0-9]+(\.\d{1,2})?$/',$value)) { return true; } else { return $this->show('100','金额不正确'); } } }
2023年05月08日
290 阅读
0 评论
0 点赞
2023-04-27
【PHP】针对时间段的处理
/** * 获取最近七天所有日期 */ public function getWeeks($time = '', $format='Y-m-d'){ $time = $time != '' ? $time : time(); //组合数据 $date = []; for ($i=1; $i<=7; $i++){ $date[$i] = date($format ,strtotime( '+' . $i-7 .' days', $time)); } return $date; } /** * 获取起始日期中的所有日期 * @param $start 开始时间 2022-09-22 * @param $end 结束时间 2022-09-29 * @return array */ public function getBetweenTime($start, $end){ $response = []; $dt_start = strtotime($start); $dt_end = strtotime($end); while ($dt_start <= $dt_end) { array_push($response, date('Y-m-d', $dt_start)); $dt_start = strtotime('+1 day', $dt_start); } return $response; } /** * 获取起始时间戳 * @param string $type 类型 1本年 2本季度 3上月 4本月 5本周 6上周 7下周 * @return string */ public function getStartEndtime($type=''){ switch ($type) { case 1: //本年开始 $startTime = strtotime(date("Y", time()) . "-1" . "-1"); //本年结束 $overTime = strtotime(date("Y", time()) . "-12" . "-31"); break; case 2: //获取当前季度 $season = ceil((date('m')) / 3); $startTime = mktime(00, 00, 00, $season * 2 + 1, 1, date('Y')); $overTime = mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y')); break; case 3: //上月 $startTime = mktime(0,0,0,date("m")-1,1,date("Y")); $overTime = mktime(0,0,0,date("m"),1,date("Y"))-1; break; case 4: //本月 $startTime = mktime(0, 0, 0, date('m'), 1, date('Y')); $overTime = mktime(23, 59, 59, date('m'), date('t'), date('Y')); break; case 5: //本周 $startTime=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y')); $overTime=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y')); break; case 6: //上周 $startTime=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y')); $overTime=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y')); break; case 7: //下周 $startTime=mktime(0,0,0,date('m'),date('d')-date('w')+1+7,date('Y')); $overTime=mktime(23,59,59,date('m'),date('d')-date('w')+7+7,date('Y')); break; default: //今天 $startTime = mktime(0,0,0,date("m"),date("d"),date("Y")); $overTime = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1; break; } return ['statTime'=>$startTime,'endTime'=>$overTime]; } /** * @Author:小破孩 * @Email:3584685883@qq.com * @Time:2024/11/11 11:34 * @return array * @Description:获取当前时间到一周前的时间 */ public function getTimeStamps() { $todayEnd = strtotime(date('Y-m-d 23:59:59')); $sevenDaysAgoStart = strtotime('-6 days 00:00:00'); return [ 'start' => $sevenDaysAgoStart, 'end' => $todayEnd ]; } /** * @Author:小破孩 * @Email:3584685883@qq.com * @Time:2024/12/7 11:05 * @param $year * @param $month * @return array * @Description:获取指定月份的开始时间和结束时间 */ public function getMonthStartAndEndTime($year, $month) { $startTime = strtotime($year. '-'. $month. '-01 00:00:00'); $endTime = strtotime($year. '-'. $month. '-'. date('t', strtotime($year. '-'. $month. '-01')). ' 23:59:59'); return ['statTime'=>$startTime,'endTime'=>$endTime]; }
2023年04月27日
363 阅读
0 评论
0 点赞
2022-11-03
【ThinkPHP】tp6获取应用名
protected $filter = ['htmlspecialchars']; public function app_name() { return App('http')->getName(); }
2022年11月03日
330 阅读
0 评论
0 点赞
2022-08-16
【PHP】PHPEmail的使用
第一步:使用composer安装phpmailercomposer require phpmailer/phpmailer第二步:common.php写个发送邮件的函数(腾讯邮箱的为例)/** * 系统邮件发送函数 * @param string $tomail 接收邮件者邮箱 * @param string $name 接收邮件者名称 * @param string $subject 邮件主题 * @param string $body 邮件内容 * @param string $attachment 附件列表 * @return boolean * @author static7 <static7@qq.com> */ function send_mail($tomail, $name, $subject = '', $body = '', $attachment = null) { $mail = new \PHPMailer(); //实例化PHPMailer对象 $mail->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码 $mail->IsSMTP(); // 设定使用SMTP服务 $mail->SMTPDebug = 0; // SMTP调试功能 0=关闭 1 = 错误和消息 2 = 消息 $mail->SMTPAuth = true; // 启用 SMTP 验证功能 $mail->SMTPSecure = 'ssl'; // 使用安全协议 $mail->Host = "smtp.exmail.qq.com"; // SMTP 服务器 $mail->Port = 465; // SMTP服务器的端口号 $mail->Username = "static7@qq.com"; // SMTP服务器用户名 $mail->Password = ""; // SMTP服务器密码 $mail->SetFrom('static7@qq.com', 'static7'); $replyEmail = ''; //留空则为发件人EMAIL $replyName = ''; //回复名称(留空则为发件人名称) $mail->AddReplyTo($replyEmail, $replyName); $mail->Subject = $subject; $mail->MsgHTML($body); $mail->AddAddress($tomail, $name); if (is_array($attachment)) { // 添加附件 foreach ($attachment as $file) { is_file($file) && $mail->AddAttachment($file); } } return $mail->Send() ? true : $mail->ErrorInfo; }第三步:控制器方法里写发送的内容/** * tp5邮件 * @param * @author staitc7 <static7@qq.com> * @return mixed */ public function email() { $toemail='static7@qq.com'; $name='static7'; $subject='QQ邮件发送测试'; $content='恭喜你,邮件测试成功。'; dump(send_mail($toemail,$name,$subject,$content)); }第4步:测试发送请自行测试转发:https://www.thinkphp.cn/topic/44477.html
2022年08月16日
347 阅读
0 评论
0 点赞
2022-06-28
【PHP】PHP获取开始时间结束时间方法总汇
<?php /** * @author yfl QQ554665488 * demo Time funtion */ //返回今天的开始时间和结束时间 function day_now() { $arr = [ mktime(0, 0, 0, date('m'), date('d'), date('Y')), mktime(23, 59, 59, date('m'), date('d'), date('Y')), ]; return $arr; } //返回昨天开始结束时间 改造上边的方法 function day_yesterday() { $yesterday = date('d') - 1; $arr = [ mktime(0, 0, 0, date('m'), $yesterday, date('Y')), mktime(23, 59, 59, date('m'), $yesterday, date('Y')), ]; return $arr; } //获取当前时间的本周开始结束时间 function week_now() { $arr = [ strtotime(date('Y-m-d', strtotime("-1 week Monday", time()))), strtotime(date('Y-m-d', strtotime("+0 week Sunday", time()))) - 1 ]; return $arr; } // var_dump(week_now()); // echo date('Y-m-d',strtotime('next Monday',time())); //返回上周开始和结束的时间戳 function last_week() { // 1520179200 1520783999 $arr = [ // date('Y-m-d',strtotime('last week Monday',time())), // date('Y-m-d',strtotime('last week Sunday',time())) strtotime('last week Monday', time()), strtotime('last week Sunday +1 days -1 seconds', time()) ]; return $arr; } // var_dump(last_week()); // 返回本月开始和结束的时间戳 function now_month() { $arr = [ mktime(0, 0, 0, date('m'), 1, date('Y')), mktime(23, 59, 59, date('m'), date('t'), date('Y')) ]; return $arr; } // var_dump(now_month()); // 返回某一年某一月的开始和结束的时间戳 function month_year($year, $month) { return [ $begin = mktime(0, 0, 0, $month, 1, $year), $end = mktime(23, 59, 59, $month, date('t', $begin), $year) ]; } // var_dump(month_year(2017,3)); // 返回当前季度的开始时间和结束时间 function now_quarter($month = 0) { $month = $month != 0 ? $month : date('n'); $season = ceil($month / 3); return [ mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')), mktime(0, 0, 0, $season * 3, date('t'), date('Y')) - 1 ]; } // var_dump(now_quarter()); // 返回上个月开始和结束的时间戳 function lastMonth() { $begin = mktime(0, 0, 0, date('m') - 1, 1, date('Y')); $end = mktime(23, 59, 59, date('m') - 1, date('t', $begin), date('Y')); return [$begin, $end]; } // var_dump(lastMonth());
2022年06月28日
318 阅读
0 评论
0 点赞
1
2
...
4