jacky50737/bsc-php

支持 Binance 的 BNB 和 BEP20,包括地址创建、余额查询、交易转账、查询最新区块链、基于区块链查询信息以及基于交易哈希查询信息等功能

1.0 2023-12-24 19:35 UTC

This package is auto-updated.

Last update: 2024-09-24 21:22:18 UTC


README

英文 | 繁体中文 | 简体中文

BSC-PHP

Stable Version Php Version bsc-php License Total Downloads

简介

支持 Binance 的 BNB 和 BEP20,包括地址创建、余额查询、交易转账、查询最新区块链、基于区块链查询信息以及基于交易哈希查询信息等功能。

优势

  1. 一套脚本即可兼容 BSC 网络中所有 BNB 货币和 BEP20 认证
  2. 接口方法可以灵活增减

支持方式

钱包

  • *使用私钥创建账户 newAccountByPrivateKey()
  • *使用助记词创建账户 newAccountByMnemonic()
  • 使用助记词恢复账户 revertAccountByMnemonic(string $mnemonic)
  • 根据私钥获取地址 revertAccountByPrivateKey(string $privateKey)

Bnb & BEP20

  • *检查余额(BNB) bnbBalance(string $address)
  • *检查余额(BEP20) balance(string $address)
  • 交易转账(离线签名) transfer(string $from, string $to, float $amount)
  • 查询最新区块 blockNumber()
  • 根据区块链查询信息 getBlockByNumber(int $blockID)
  • 根据交易哈希返回交易收据 getTransactionReceipt(string $txHash)
  • 根据交易哈希返回交易信息 getTransactionByHash(string $txHash)
  • 根据交易哈希查询交易状态 receiptStatus(string $txHash)

快速开始

安装

PHP8

composer require jacky50737/bsc-php

或 PHP7

composer require jacky50737/bsc-php ~1.0

接口

钱包

$wallet = new \Binance\Wallet();

// Generate a private key to create an account
$wallet->newAccountByPrivateKey();

// Generate mnemonic and create an account
$wallet->newAccountByMnemonic();

// Restore account using mnemonic
$mnemonic = 'elite link code extra twist autumn flower purse excuse harsh kitchen whip';
$wallet->revertAccountByMnemonic($mnemonic);

// Get the address according to the private key
$privateKey = '5e9340935f4c02628cec5d04cc281012537cafa8dae0e27ff56563b8dffab368';
$wallet->revertAccountByPrivateKey($privateKey);

Bnb & BEP20

## Method 1 : BSC RPC Nodes
$uri = 'https://bsc-dataseed1.defibit.io/';// Mainnet
// $uri = 'https://data-seed-prebsc-1-s1.binance.org:8545/';// Testnet
$api = new \Binance\NodeApi($uri);

## Method 2 : Bscscan Api
$apiKey = 'QVG2GK41ASNSD21KJTXUAQ4JTRQ4XUQZCX';
$api = new \Binance\BscscanApi($apiKey);

$bnb = new \Binance\Bnb($api);

$config = [
    'contract_address' => '0x55d398326f99059fF775485246999027B3197955',// USDT BEP20
    'decimals' => 18,
];
$bep20 = new \Binance\BEP20($api, $config);

// *Check balances
$address = '0x1667ca2c72d8699f0c34c55ea00b60eef021be3a';
$bnb->bnbBalance($address);
$bep20->balance($address);

// Transaction transfer (offline signature)
$from = '0x1667ca2c72d8699f0c34c55ea00b60eef021be3a';
$to = '0x1667ca2c72d8699f0c34c55ea00b60eef021****';
$amount = 0.1;
$bnb->transfer($from, $to, $amount);
$bep20->transfer($from, $to, $amount);

// Query the latest block
$bnb->blockNumber();
$bep20->blockNumber();

// Query information according to the blockchain
$blockID = 24631027;
$bnb->getBlockByNumber($blockID);
$bep20->getBlockByNumber($blockID);

// Returns the receipt of a transaction by transaction hash
$txHash = '0x4dd20d01af4c621d2fc293dff17a8fd8403ea3577988bfb245a18bfb6f50604b';
$bnb->getTransactionReceipt($txHash);
$bep20->getTransactionReceipt($txHash);

// Returns the information about a transaction requested by transaction hash
$txHash = '0x4dd20d01af4c621d2fc293dff17a8fd8403ea3577988bfb245a18bfb6f50604b';
$bnb->getTransactionByHash($txHash);
$bep20->getTransactionByHash($txHash);

// Query transaction status based on transaction hash
$txHash = '0x4dd20d01af4c621d2fc293dff17a8fd8403ea3577988bfb245a18bfb6f50604b';
$bnb->receiptStatus($txHash);
$bep20->receiptStatus($txHash);

计划

  • 支持 ERC721|ERC-1155
  • 智能合约

🌟🌟

Stargazers over time