tp5 调用自己配置的config参数

本文介绍在ThinkPHP5框架中如何通过applicationconfig.php文件进行自定义配置参数设置,并演示了如何在控制器中使用这些配置参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在tp5框架中 application\config.php中自定义配置参数  如下图

在控制层调用时需:

use think\Config;

调用:

$accessKeyId = Config::get('accessKeyId');
$accessKeySecret = Config::get('accessKeySecret');
### ThinkPHP 6 中调用 OCR 功能的方法 要在 ThinkPHP 6 中调用 OCR 功能,可以按照以下方法操作。以下是基于百度 AI 提供的 OCR 接口以及相关 PHP 实现逻辑的内容。 #### 配置应用参数 首先,在 `config/app.php` 文件中配置百度 API 的 AppID 和 Secret Key: ```php return [ 'baidu_appId' => 'your_baidu_app_id', // 替换为实际的百度 App ID 'baidu_secret' => 'your_baidu_secret_key', // 替换为实际的百度 Secret Key ]; ``` 此部分配置是为了便于后续代码中的统一管理和调用[^3]。 --- #### 获取 Access Token 在使用百度 OCR 接口之前,需要先获取 Access Token。可以通过以下函数实现: ```php namespace app\common\service; use think\facade\Config; class BaiduOcrService { private $appId; private $secretKey; public function __construct() { $this->appId = Config::get('baidu_appId'); $this->secretKey = Config::get('baidu_secret'); } /** * 获取 AccessToken */ public function getAccessToken() { $url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={$this->appId}&client_secret={$this->secretKey}"; $response = file_get_contents($url); $result = json_decode($response, true); if (isset($result['access_token'])) { return $result['access_token']; } else { throw new \Exception("无法获取 Access Token:" . ($result['error_description'] ?? '未知错误')); } } } ``` 上述代码通过访问百度提供的 OAuth 地址来获取 Access Token,并将其返回给调用方[^1]。 --- #### 身份证识别功能实现 对于身份证识别的功能,可以在服务类中继续扩展如下方法: ```php /** * 身份证识别 * * @param string $image 图片文件路径或 Base64 编码字符串 * @return array 返回解析后的数据 */ public function idCardRecognition(string $image): array { $accessToken = $this->getAccessToken(); $url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=" . $accessToken; $data = ['id_card_side' => 'front']; // front 表示正面;back 表示反面 if (file_exists($image)) { // 如果传入的是本地图片路径,则读取并转换成 base64 $content = file_get_contents($image); $base64Image = base64_encode($content); $data['image'] = $base64Image; } elseif (preg_match('/^(?:[A-Za-z0-9+/]{4})*(?:|[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)$/', $image)) { // 判断是否为 Base64 字符串 $data['image'] = $image; } else { throw new \Exception("图片格式不正确"); } $options = [ 'http' => [ 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data), ], ]; $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return json_decode($result, true); } ``` 该方法支持两种输入方式:一是上传本地图片文件路径,二是直接提供 Base64 编码的图片字符串[^2]。 --- #### 使用示例 假设有一个控制器用来处理用户的身份证扫描请求,可以这样写: ```php namespace app\controller; use app\common\service\BaiduOcrService; use think\Request; class OcrController { public function recognizeIdCard(Request $request) { try { $filePath = $request->post('image'); // 假设前端传递了一个图片文件路径 $baiduOcr = new BaiduOcrService(); $result = $baiduOcr->idCardRecognition($filePath); return json(['code' => 0, 'message' => '成功', 'data' => $result]); } catch (\Exception $e) { return json(['code' => 1, 'message' => $e->getMessage()]); } } } ``` 以上是一个简单的接口设计案例,允许用户提交图片并通过 OCR 技术解析其内容[^4]。 --- #### 注意事项 1. **Access Token 过期时间**:每次请求都会重新获取新的 Access Token,建议优化缓存机制以减少重复请求。 2. **图片大小限制**:确保上传的图片不超过百度 API 所规定的最大尺寸(通常为 4MB 左右)。 3. **异常捕获**:在生产环境中应增加更多的日志记录和错误提示以便排查问题。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值