Tutorial to send TRC20 Tokens with PHP
To send or receive TRC20 tokens, you need an address that is compatible with the Tron blockchain. Of course, most people use their wallets to send TRC20 tokens. But what if you need to make hundreds of thousands of transactions? In this case, you require a better way to send TRC20 tokens in an automated way. We have written a detailed tutorial for this purpose. Read more.
Prerequisites
- Basic knowledge of PHP
- Account at Chaingateway.io
- A local or remote machine with PHP7 and installed modules JSON and CURL
The Code
$contractaddress, "from" => $from, "to" => $to, "privatekey" => $privatekey, "amount" => $amount) );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Authorization: " . $apikey));
# Return response instead of printing.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
# Send request.
$result = curl_exec($ch);
curl_close($ch);
# Decode the received JSON string
$resultdecoded = json_decode($result, true);
# Print the transaction id of the transaction
echo $resultdecoded["txid"];
?>
Code Modifications
- Replace “YOURAPIKEY” in line 3 of above code with the API Key in your Chaingateway.io Account Panel.
- Replace “CONTRACTADDRESS” in line 4 with the smart contract address of the Token.
- Replace “SENDERADDRESS” in line 5 with the Tron address you want to send from.
- Replace “RECEIVERADDRESS” in line 6 with the receiving Tron address
- Replace “PASSWORD” in line 7 with your password of the Tron address, which you specified when you created the address.
- Replace “55.89” in line 8 with the amount of Tokens you want to send.
Final Steps
- Upload the code above to your local or remote machine
- Call the script by navigating to your favorite browser and entering the URL. For example, if you upload it to the root directory of your website, you can call it by typing yourwebsite.com/name-of-the-above-code.php
- After a few seconds, the script will return a transaction id (which means the transaction has been sent successfully). You can lookup this transaction id for example on Tronscan
Congratulations! You just sent BEP-20 Tokens using Chaingateway.io. Wasn’t that hard, right?
If you want to learn how to get TRC20 token balance of an Tron address, check out our Tutorial about balance of TRC10 & TRC20 Tokens.