PHP code example of qingze-lab / esignbao-sdk-php-7.0

1. Go to this page and download the library: Download qingze-lab/esignbao-sdk-php-7.0 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

qingze-lab / esignbao-sdk-php-7.0 example snippets


use QingzeLab\ESignBao\Client;
use QingzeLab\ESignBao\Config\Configuration;

$config = new Configuration(
    'your_app_id',
    'your_app_secret',
    'https://smlopenapi.esign.cn' // 沙箱环境
);

$client = new Client($config);

// 1. 上传文件
$uploadResult = $client->file()->uploadFileByPath('./contract.pdf');
$fileId = $uploadResult['data']['fileId'];

// 2. 创建签署流程
$flowResult = $client->signFlow()->createByFile(
    [
        ['fileId' => $fileId, 'fileName' => '合同.pdf']
    ],
    null, // attachments
    [
        'signFlowTitle' => '测试合同签署',
        'autoStart' => true,
        'autoFinish' => true
    ],
    null, // signFlowInitiator
    [
        [
            'signerType' => 0, // 个人签署
            'psnSignerInfo' => ['psnAccount' => '188****8888'],
            'signFields' => [
                ['fileId' => $fileId, 'posPage' => '1', 'posX' => 100, 'posY' => 100]
            ]
        ]
    ]
);
$signFlowId = $flowResult['data']['signFlowId'];

// 3. 获取签署链接
$signUrl = $client->signFlow()->getSignUrl(
    $signFlowId,
    ['psnAccount' => '188****8888']
);

echo "签署链接: " . $signUrl['data']['shortUrl'];

use QingzeLab\ESignBao\Client;
use QingzeLab\ESignBao\Config\Configuration;
use QingzeLab\ESignBao\Log\LaravelLogger;

public function register()
{
    $this->app->singleton(Client::class, function ($app) {
        $config = new Configuration(
            env('ESIGN_APP_ID'),
            env('ESIGN_APP_SECRET'),
            env('ESIGN_API_URL', 'https://openapi.esign.cn')
        );
        
        // 使用 Laravel 的日志系统
        $config->setLogger(new LaravelLogger(app('log')));
        
        return new Client($config);
    });
}

public function sign(Client $client)
{
    // ...
}

use QingzeLab\ESignBao\Client;
use QingzeLab\ESignBao\Config\Configuration;

class ESignService
{
    protected static $client;

    public static function getClient()
    {
        if (!self::$client) {
            $config = new Configuration(
                config('esign.app_id'),
                config('esign.app_secret'),
                config('esign.api_url')
            );
            self::$client = new Client($config);
        }
        return self::$client;
    }
}

namespace app\common\library; // 命名空间根据实际情况调整

use QingzeLab\ESignBao\Log\LoggerInterface;
use think\Log;

/**
 * 适配 ThinkPHP 5.0 的日志类
 */
class Tp5Logger implements LoggerInterface
{
    public function info(string $message, array $context = [])
    {
        // 记录 info 级别日志
        // JSON_UNESCAPED_UNICODE 确保中文不被转义
        $contextStr = !empty($context) ? ' ' . json_encode($context, JSON_UNESCAPED_UNICODE) : '';
        Log::record('[ESignBao] ' . $message . $contextStr, 'info');
    }

    public function error(string $message, array $context = [])
    {
        // 记录 error 级别日志
        $contextStr = !empty($context) ? ' ' . json_encode($context, JSON_UNESCAPED_UNICODE) : '';
        Log::record('[ESignBao] ' . $message . $contextStr, 'error');
    }
}

// 在 ESignService::getClient() 中
$config = new Configuration(
    config('esign.app_id'),
    config('esign.app_secret'),
    config('esign.api_url')
);

// 注入日志适配器
$config->setLogger(new \app\common\library\Tp5Logger());

self::$client = new Client($config);
bash
composer