更新时间:2025-06-06 GMT+08:00
SDK配置和调用示例
SDK在不同环境下支持的实例安全机制如下:
- Windows环境:ECDSA
- Linux环境:ECDSA
- 环境需要安装openssl
- 目前仅支持共享版数字资产链的调用
SDK配置
SDK中相关方法的使用示例,可参考应用程序的示例Samples/sample1.php(dac-php-sdk下载地址)。
- 项目安装依赖。
composer install
- 配置时区
php.ini中设置时区为国内
date.timezone = asia/shanghai
- 配置openssl Config(可选)
在DAC 配置文件conf.yaml中增加 # windows opensslCnf: D:\xampp\php\extras\ssl\openssl.cnf # linux opensslCnf: /root/openssl.cnf
调用示例
php-sdk 调用示例:
<?php use Dac\DACSDK; if (file_exists('Dac/vendor/autoload.php')) { require 'Dac/vendor/autoload.php'; } else { require '../Dac/vendor/autoload.php'; } $filePath = 'D:\\temp\\w3shared\\conf.yaml'; // 初始化SDK $tdcsdk = new DACSDK($filePath); // 创建名为“tiger01”的集合 $collectionIndex = "tiger01"; $txid = $tdcsdk->nft721->createNFTCollection($collectionIndex); echo "Create collection txid: " . $txid . "\n"; echo '<br>'; // 生成数字资产meta信息 $md = $tdcsdk->toolManager->newImageMetadata("image bytes", "tigerImage", "http://obs.huawei.com/1/tiger", "description", 1); // 铸造数字资产 $mintResp = $tdcsdk->nft721->mint($collectionIndex, "1", $md); echo "Mint nft:" . $mintResp->getNft()->getTokenURI(); echo '<br>'; echo "Mint txid:" . $mintResp->getTxid() . "\n"; echo '<br>'; // 生成新的用户信息,包括证书,私钥和地址 $receiver = $tdcsdk->toolManager->generateIdentity(\Dac\Manager\ToolManager::ECDSAAlgorithm, "test1"); echo "cert: " . $receiver->getCert() . "\n"; echo '<br>'; echo "sk: " . $receiver->getKey() . "\n"; echo '<br>'; echo "addr: " . $receiver->getAddress() . "\n"; echo '<br>'; sleep(2); // 查询数字资产所有者 $owner = $tdcsdk->nft721->ownerOf($collectionIndex, "1"); echo "collection:" . $collectionIndex . ", token:1 owner is " . $owner . "\n"; echo '<br>'; // 数字资产转移 $transferTxid = $tdcsdk->nft721->transferFrom($collectionIndex, $owner, $receiver->getAddress(), "1"); echo "Transfer txid:" . $transferTxid . '\n'; echo '<br>'; // 批量铸造数字资产 $bmd = new \Dac\Model\BatchMintData("10000", "50", $md); $tx = $tdcsdk->nft1155->batchMint($collectionIndex, $bmd); echo "BatchMint txid: " . $tx; echo '<br>'; // 铸造数字资产 $mintResp = $tdcsdk->nft721->mint($collectionIndex, "2", $md); echo "Mint nft: " . $mintResp->getNft()->getTokenURI(); echo '<br>'; echo "Mint txid: " . $mintResp->getTxid() . "\n"; echo '<br>'; // 铸造可组合资产 $mintResp = $tdcsdk->nft998->mint($collectionIndex, "10", $md); echo "Mint of composable asset nft: " . $mintResp->getNft()->getTokenURI(); echo '<br>'; echo "Mint of composable asset txid: " . $mintResp->getTxid() . "\n"; echo '<br>'; // 加入可组合资产 $getChildTxid = $tdcsdk->nft998->getChild($owner, $collectionIndex, "10", $collectionIndex, "2"); echo "GetChildTxid txid: " . $getChildTxid; echo '<br>'; // 切换成新的用户身份 $tdcsdk->toolManager->switchIdentityWithPem(\Dac\Manager\ToolManager::ECDSAAlgorithm, $receiver->getCert(), $receiver->getKey());
父主题: PHP