Skip to content
徐连波 edited this page Jan 26, 2021 · 7 revisions

完整的配置如下:

<?php

/*
 * This file is part of the godruoyi/ocr.
 *
 * (c) Godruoyi <gmail@godruoyi.com>
 *
 * This source file is subject to the MIT license that is bundled.
 */


use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;

return [

    /*
    |--------------------------------------------------------------------------
    | Default client
    |--------------------------------------------------------------------------
    |
    | 指定一个默认的 client 名称,其值需要在下列的 drivers 数组中配置。
    |
    */
    'default' => 'aliyun',

    /*
    |--------------------------------------------------------------------------
    | Client 配置
    |--------------------------------------------------------------------------
    |
    | Client 配置信息,包括基本密钥等;注意目前 aliyun 暂只支持 appcode 方式。
    |
    */
    'drivers' => [
        'aliyun' => [
            'appcode'    => '',
            'secret_id'  => '',
            'secret_key' => '',
        ],

        'baidu' => [
            'access_key' => '',
            'secret_key' => '',
        ],

        'tencent' => [
            'secret_id'  => '',
            'secret_key' => '',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | 日志配置
    |--------------------------------------------------------------------------
    |
    | 基于 Monolog,可用的日志驱动有: "single", "daily", "slack", "syslog",
    | "errorlog", "monolog", "custom", "stack"
    |
    */
    'log' => [
        'enable' => true,
        'default' => 'stack',
        'channels' => [
            'daily' => [
                'name' => 'OCR',
                'driver' => 'daily',
                'path' => '/tmp/ocr.log',
                'level' => 'debug',
                'days' => 14,
            ]
        ],
    ],
];

Default & Drivers

若你在配置文件设置了一个默认的 driver(如:aliyun),那在调用时可省略掉用方关键字;

$application = new Application($config);

// 这将直接调用 aliyun client 里的 idcard 方法
$application->idcard('....');

drivers 配置的是相关服务商的密钥;其中 aliyun 暂只支持 appcode 形式。

Log

默认情况下,我们没用开启请求日志,你可以通过设置 enable 为 true 启动请求日志。

return [
    // ....

    'log' => [
        'enable' => true,
    ]
];

大部分服务商在识别图片时都会对图片进行 base64 操作,若将 base64 后的内容全部记录在日志中,将会导致文件过于臃肿;因此我们默认用如下的格式来记录请求日志:

>>>>>>>>\n{req_headers}\n\n<<<<<<<<\n{response}\n--------\n{error}

即 request 中只包含 headers,不包含请求 body;

[2020-11-12 08:22:04] OCR.DEBUG: >>>>>>>>
POST / HTTP/1.1
Content-Length: 1783744
Expect: 100-Continue
User-Agent: GuzzleHttp/6.3.3 curl/7.68.0 PHP/7.3.14
Host: ocr.tencentcloudapi.com
Content-Type: application/json
X-TC-Action: IDCardOCR
X-TC-RequestClient: Godruoyi_OCR_PHP_SDK_2.0
X-TC-Timestamp: 1605169322
X-TC-Version: 2018-11-19
X-TC-Region: ap-shanghai
Authorization: TC3-HMAC-SHA256 Credential=AKIDiRAtcIeX5WuA24jvhy670nLo9LPN6BRf/2020-11-12/ocr/tc3_request, SignedHeaders=content-type;host, Signature=x

null

你也可以通过如下的方式重置日志格式。

return [
    // ....

    'log' => [
        'enable' => true,
        'default' => 'errorlog',
        'channels' => [
            'errorlog' => [
                'driver' => 'errorlog',
                'level' => 'debug',
                'name' => 'OCR',
                'formatter' => \GuzzleHttp\MessageFormatter::DEBUG,
            ],
        ]
    ]
];

下一步

Clone this wiki locally