PHP code example of fenshenx / php-conflux-sdk

1. Go to this page and download the library: Download fenshenx/php-conflux-sdk 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/ */

    

fenshenx / php-conflux-sdk example snippets




$conflux = new \Fenshenx\PhpConfluxSdk\Conflux("https://test.confluxrpc.com", 1);

$cfx = $conflux->getCfx();

$account = $conflux->getWallet()->addPrivateKey('0x0000000xxxxxxxxx');  //your private key
$options = [
    'from' => $account->getConfluxAddress(),
    'to' => 'cfxtest:aapr2jm67p5myymb51g0caszkred0907eayz84w6v3',
    'value' => \Fenshenx\PhpConfluxSdk\Drip::fromCFX(1)
];

$gasInfo = $cfx->estimateGasAndCollateral($options);
$hash = $cfx->sendTransaction($options);

$balance = $cfx->getBalance('cfxtest:aapr2jm67p5myymb51g0caszkred0907eayz84w6v3');
$balanceDrip = $balance->getDrip(); //string
$balanceDripHex = $balance->getDripHex();
$balanceCfx = $balance->toCFX();

$account = $conflux->getWallet()->addPrivateKey('0x00000xxxxx');    //your private key

$contractAddress = "cfxtest:acgh0vts2ga63dpwrbtzcgbz9m4x01bpkjwu9sufp4";
$abi = file_get_contents($contractAddress.'.json');
$contract = $conflux->getContract($abi, $contractAddress);

// Call contract 'balanceOf' method
$balance = $contract->balanceOf("cfxtest:aapr2jm67p5myymb51g0caszkred0907eayz84w6v3")->send();

// Send contract 'mint' transaction
$mintTransactionGas = $contract->mint($account->getConfluxAddress(), 100)->estimateGasAndCollateral($account);
$mintTransactionHash = $contract->mint($account->getConfluxAddress(), 100)->sendTransaction($account);

$account = $conflux->getWallet()->addRandom();

//Eth address to conflux address
\Fenshenx\PhpConfluxSdk\Utils\SignUtil::address2ConfluxAddress('0xxxx', 1);

//Conflux address to eth address
\Fenshenx\PhpConfluxSdk\Utils\SignUtil::confluxAddress2Address('cfxtest:acgh0vts2ga63dpwrbtzcgbz9m4x01bpkjwu9sufp4');

//Private key to public key
\Fenshenx\PhpConfluxSdk\Utils\SignUtil::privateKey2PublicKey('0x00xxxxxx');

//Add 0x to string start if the string not start with 0x
\Fenshenx\PhpConfluxSdk\Utils\FormatUtil::zeroPrefix('0abcdef');   //zeroPrefix('0x0abcdef') reutrn 0x0abcdef

//Remove 0x from string if the string start with 0x
\Fenshenx\PhpConfluxSdk\Utils\FormatUtil::stripZero('0x123abcd');   //stripZero('123abcd') return  123abcd

//or
//class MyProvider implements IProvider
class MyProvider extends \Fenshenx\PhpConfluxSdk\Providers\BaseProvider {

    protected function request($data)
    {
        //send the request
    }
}

$confluxWithMyProvider = new \Fenshenx\PhpConfluxSdk\Conflux(url: $rpcUrl, networkId: 1, ownProvider: new MyProvider($rpcUrl));
shell
composer