PHP code example of xyj2156 / k3cloud-client

1. Go to this page and download the library: Download xyj2156/k3cloud-client 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/ */

    

xyj2156 / k3cloud-client example snippets


use K3Cloud\K3CloudClient;

$api = K3CloudClient::appSignature(
    serverUrl: 'https://api.kingdee.com/galaxyapi/',
    acctId:    '62f3c9b0xxxxxxxx',
    userName:  'api_user',
    appId:     '204399_xxxxxxxxxxxxxxxx',
    appSecret: 'your-app-secret',
);

$rows = $api->query('BD_Currency', ['FCURRENCYID', 'FNUMBER', 'FNAME'])
    ->throwIfError();

foreach ($rows->rows() as [$id, $number, $name]) {
    echo "$number — $name\n";
}

use K3Cloud\K3CloudClient;

$api = K3CloudClient::password(
    serverUrl: 'https://192.168.1.100:8080/K3Cloud',
    acctId:    '62f3c9b0xxxxxxxx',
    userName:  'administrator',
    password:  'your-password',
)->insecure();   // 仅自签证书 / 本地部署需要

$created = $api->save('BD_Currency', [
    'NeedUpDateFields' => [],
    'IsDeleteEntry'    => 'true',
    'Model'            => ['FNAME' => '测试币种', 'FNUMBER' => 'TESTCUR', 'FPRECISION' => 2],
])->throwIfError();

echo $created->id(), ' / ', $created->number(), "\n";

$api = K3CloudClient::password($url, $acct, $user, $pwd)
    ->insecure()               // 信任自签证书
    ->withTimeouts(10, 60);    // 连接 / 请求超时,单位秒

use K3Cloud\Config;
use K3Cloud\K3CloudClient;

$api = new K3CloudClient(
    Config::appSignature($url, $acct, $user, $appId, $appSecret)->withTlsVerification(true)
);

$api = K3CloudClient::password($url, $acct, $user, $pwd)
    ->withSessionStorePath('/var/cache/myapp/k3cloud')   // 换目录(仍为文件存储)
    // ->withoutSessionPersistence()                      // 关:回到"仅内存、每进程登录一次"
    // ->withSessionStore($store)                         // 换实现:Redis / 数据库 / 自建缓存

$api->operation('AttachmentDownLoad', [ $payload ]);      // 短名
$api->service('Kingdee.BOS.WebApi.ServicesStub.Some.Service', [ ...$params ]);

$result = $api->query('SAL_SaleOrder', ['FSBILLNO', 'FTOTALAMOUNT'], [
    'FilterString' => "FDate >= '2024-01-01'",
    'OrderString'  => 'FDate DESC',
    'TopRowCount'  => 100,
]);

use K3Cloud\K3CloudClient;

// 通用(无需类)——返回基类 Entity:
$api->bill('SAL_SaleOrder')
    ->ref('FBillTypeID', 'XSDD01_SYS', 'FNUMBER')   // 基础资料引用
    ->ref('FCustId', '16.195.06.0001')
    ->custom('F_PEYC_Decimal', '1710')              // 二开字段,无需 schema
    ->line('FSaleOrderEntry', fn($row) => $row
        ->ref('FMaterialId', '02.04.03.089')
        ->set('FQty', '1.000'))
    ->package(['FSaleOrderFinance' => ['FSettleCurrId' => ['FNumber' => 'PRE001']]])
    ->save()                                        // -> Result
    ->throwIfError();

use K3Cloud\Entity;

final class SaleOrder extends Entity
{
    public const FORM_ID = 'SAL_SaleOrder';
    public function customer(string $n): static { return $this->ref('FCustId', $n); }
}

$order = $api->entity(SaleOrder::class);   // 带类型:$order 就是 SaleOrder
// 或类静态工厂:$order = SaleOrder::for($api);
$order->customer('16.195.06.0001')->save();

// 可选便利:注册后,字符串入口也能自动解析到子类:
Entity::register(SaleOrder::FORM_ID, SaleOrder::class);
$api->bill('SAL_SaleOrder');               // → SaleOrder
bash
php tools/build-metadata.php [.docs/raw/entity.json] [.docs/raw/field.json] [.docs]
bash
php tools/build-metadata.php ... --scaffold --with-custom --namespace=App\\K3Cloud\\Entities