Tutorial to send Ethereum or ERC20 Tokens with PHP

To send or receive ERC-20 tokens, you need an address that is compatible with the Ethereum blockchain. Of course, most people use their wallets to send ERC20 tokens. But what if you need to make hundreds of thousands of transactions? In this case, you need a better way to send ERC20 tokens in an automated way. We have written a detailed tutorial for this purpose. Read more.

Prerequisites

  1. Basic knowledge of PHP
  2. Account at Chaingateway.io
  3. A local or remote machine with PHP7 and installed modules JSON and CURL

The Code

				
					
<?php
# ----- REPLACE THE VARIABLES BELOW WITH YOUR DATA -----
$apikey = "YOURAPIKEY"; // API Key in your account panel
$contractaddress = "CONTRACTADDRESS"; // Smart contract address of the Token
$from = "SENDERADDRESS"; // Ethereum address you want to send from (must have been created with Chaingateway.io)
$to = "RECEIVERADDRESS"; // Receiving ethereum address
$password = "PASSWORD"; // Password of the ethereum address (which you specified when you created the address)
$amount = 55.89; // Amount of Tokens to send
# -------------------------------------------------------

# Define function endpoint
$ch = curl_init("https://eu.eth.chaingateway.io/v1/sendToken");

# Setup request to send json via POST. This is where all parameters should be entered.
$payload = json_encode( array("contractaddress" => $contractaddress, "from" => $from, "to" => $to, "password" => $password, "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

  1. Replace “YOURAPIKEY” in line 3 of above code with the API Key in your Chaingateway.io Account Panel.
  2. Replace “CONTRACTADDRESS” in line 4 with the smart contract address of the Token.
  3. Replace “SENDERADDRESS” in line 5 with the ethereum address you want to send from.
  4. Replace “RECEIVERADDRESS” in line 6 with the receiving ethereum address
  5. Replace “PASSWORD” in line 7 with your password of the ethereum address, which you specified when you created the address.
  6. Replace “55.89” in line 8 with the amount of Tokens you want to send.

Final Steps

  1. Upload the code above to your local or remote machine
  2. 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
  3. 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 Etherscan.

Congratulations! You just sent ERC20 Tokens using Chaingateway.io. Wasn’t that hard, right?

If you want to learn how to get ERC20 token balance of an address, check out our Tutorial about balance of ERC20 Tokens.