Ethereum JSON RPC


JSON is a lightweight data-interchange format. It can represent numbers, strings, ordered sequences of values, and collections of name/value pairs.

JSON-RPC is a stateless, light-weight remote procedure call (RPC) protocol. Primarily this specification defines several data structures and the rules around their processing. It is transport agnostic in that the concepts can be used within the same process, over sockets, over HTTP, or in many various message passing environments. It uses JSON (RFC 4627) as data format.

Source from https://eth.wiki/json-rpc/api.


<?php 

include_once "../libraries/vendor/autoload.php";

$hosts = ["https://mainnet.infura.io"=>"https://mainnet.infura.io","https://ropsten.infura.io"=>"https://ropsten.infura.io", "https://cloudflare-eth.com"=>"https://cloudflare-eth.com", "https://eth-mainnet.alchemyapi.io"=>"https://eth-mainnet.alchemyapi.io", "https://eth-ropsten.alchemyapi.io"=>"https://eth-ropsten.alchemyapi.io", "https://testnet-rpc.anisticnetwork.net"=>"https://testnet-rpc.anisticnetwork.net"];

include_once("html_iframe_header.php");

$apis = ['eth_maxPriorityFeePerGas','eth_feeHistory', 'eth_syncing','eth_coinbase','eth_mining','eth_hashrate','eth_gasPrice','eth_accounts','eth_blockNumber','eth_chainId','eth_getBalance','eth_getStorageAt','eth_getTransactionCount','eth_getBlockTransactionCountByHash','eth_getBlockTransactionCountByNumber','eth_getUncleCountByBlockHash','eth_getUncleCountByBlockNumber','eth_getCode','eth_sign','eth_sendTransaction','eth_sendRawTransaction','eth_call','eth_estimateGas','eth_getBlockByHash','eth_getBlockByNumber','eth_getTransactionByHash','eth_getTransactionByBlockHashAndIndex','eth_getTransactionByBlockNumberAndIndex','eth_getTransactionReceipt','eth_pendingTransactions','eth_getUncleByBlockHashAndIndex','eth_getUncleByBlockNumberAndIndex','eth_getCompilers','eth_compileLLL','eth_compileSolidity','eth_compileSerpent','eth_newFilter','eth_newBlockFilter','eth_newPendingTransactionFilter','eth_uninstallFilter','eth_getFilterChanges','eth_getFilterLogs','eth_getLogs','eth_getWork','eth_submitWork','eth_submitHashrate','eth_getProof'];

unset($_REQUEST);
$_REQUEST = array("method"=>"","params"=>"");
foreach($_REQUEST as $k=>$v) {
	if (isset($_POST[$k])) {
		$_REQUEST[$k] = $_POST[$k];
	} else {
		$_REQUEST[$k] = $_GET[$k];
	}
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	try {
		
		if (!in_array($_POST['host'], array_keys($hosts))) {
			throw new Exception("Please provide valid host.");
		}
		
		$url = $_POST['host'] . "/" . $_POST['path'];
		
		$ch = curl_init();
		$requestId = time();
		$paramsParams = json_decode($_POST['params'], true);
		if (json_last_error() != JSON_ERROR_NONE) {
			throw new Exception("`Params` is not a proper JSON string.");
		}
		
		$params = [];
		$params['jsonrpc']= $_POST['jsonver'];
		$params['method'] = $_POST['method'];
		
		$params['params'] = $paramsParams;
		$params['id'] = $requestId;
		
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS,$req = json_encode($params));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);

		$resp = curl_exec($ch);
		
		if ($resp === false) {
			throw new Exception("curl_exec return false.");
		}
		
		if (strlen($err = @curl_error($ch)) > 0) {
			$errno = @curl_errno($ch);
			throw new Exception( "{$err} ({$errno})." );
		}
		
		$result = json_decode($resp,true); 
		if ($result['id'] != $requestId) {
			throw new Exception("Invalid request id.");
		}
		
		curl_close ($ch);
		
		?>
		<div class="alert alert-success">
			<h6 class="mt-3">Host</h6>
			<textarea class="form-control" rows="1" readonly><?php echo $url;?></textarea>
			
			<h6 class="mt-3">JSON-RPC Request</h6>
			<textarea class="form-control" rows="5" id="comment" readonly><?php echo $req;?></textarea>
			
			<h6 class="mt-3">JSON-RPC Response</h6>
			<textarea class="form-control" rows="5" id="comment" readonly><?php echo $resp;?></textarea>
		</div>
		<?php
	} catch (Exception $e) {
		$errmsg .= "Problem found. " . $e->getMessage();
	}
} 

if ($errmsg) {
?>
    <div class="alert alert-danger">
        <strong>Error!</strong> <?php echo $errmsg?>
    </div>
<?php
}

?>
<form id='this_form' action='?action=submit' method='post'>

	<div class="form-group">
		<label for="host">Host To Receive RPC:</label>
		
		<div class="input-group mb-3">
			<select id="host" name="host" class="form-control" >
			<?php
			foreach($hosts as $k=>$v) {
				echo "<option value='{$k}'".($k == $_POST['host'] ? " selected": "").">{$v}</option>";
			}
			?>
			</select>
			<div class="input-group-append">
				<span class="input-group-text">
					/
				</span>
			</div>
			
			<input class="form-control" type='text' name='path' id='path' value='<?php echo $_POST['path']?>' placeholder="Put extra path or blank if it does not.">
			
		</div>
	</div>
	
	<div class="form-group">
        <label for="jsonver">Json RPC version:</label>
        <input class="form-control" type='text' name='jsonver' id='jsonver' value='<?php echo $_POST['jsonver']?$_POST['jsonver']: ""?>'>
    </div>

	<div class="form-group">
	<label for="jsonver">Method:</label>
		<div class="input-group">
			<input class="form-control" type='text' name='method' id='method' value='<?php echo $_REQUEST['method']?>'>
			<div class="input-group-append">
				<button class="btn btn-outline-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Select</button>
				<ul class="dropdown-menu w-100 shadow p-0" id="navbarDropdownMega">
					<li class="dropdown-item">
						<?php 
						$itemsPerRow = 4;
						foreach($apis as $k=> $api) {
							if ($k%$itemsPerRow == 0) {
							?>
							<div class="row">
							<?php
							}
							?>
								<div class="col-sm-3">
									<?php 
									if ($api) {
									?>
									<a href="#" onclick="$('input#method').val(this.innerHTML);return false;"><?php echo $api?></a>
									<?php
									}
									?>
								</div>
							<?php
							if (($k+1)%$itemsPerRow == 0) {
							?>
							</div>
							<?Php
							}
						}
						?>
					</li>
				</ul>
			</div>
		</div>
    </div>
	
	<div class="form-group">
        <label for="params">Params:</label>
		<textarea class="form-control" rows="10" name='params' id='params' placeholder='Please refer to sample of value below'><?php echo $_REQUEST['params']?></textarea>
		<small>
			<ol style="padding-left: 0; list-style: inside decimal;">
				<li>
					Must be JSON-ARRAY encoded string, start with '[' and end with ']'. You can generate it from <a href="https://www.functions-online.com/json_encode.html" target="_blank">here</a> or <a href="http://php.fnlist.com/php/json_encode" target="_blank">here</a>.
				</li>
				
				<li>
					<a href="https://eth.wiki/json-rpc/api" target="_blank">Refer to</a> <span class='grey_info'>Parameters</span> part under your selected method about required value.
				</li>
				
				<li>
					Sample of `Params` value.
					<div class="table-responsive">
						<table class="table table-sm table-bordered">
							<tr><th>Params</th><th>Description</th></tr>
							<tr>
								<td>[]</td>
								<td>Empty params array.</td>
							</tr>
							
							<tr>
								<td>["0x5cf1bdA8757b9c501190B0FcbC6B4ab8a4Bd04a5"]</td>
								<td>Params array with 1 address item.</td>
							</tr>
							
							<tr>
								<td>["0x5cf1bdA8757b9c501190B0FcbC6B4ab8a4Bd04a5","latest"]</td>
								<td>Params array with 1 address item & 1 block param.</td>
							</tr>
							
							<tr>
								<td>[{"from":"0x5cf1bdA8757b9c501190B0FcbC6B4ab8a4Bd04a5","to":"0xd46e8dd67c5d32be8058bb8eb970870f07244567"}]</td>
								<td>Params array with 1 object item.</td>
							</tr>
						</table>
					</div>
				</li>
			</ol>
		</small>
    </div>
	
    <input type='submit' class="btn btn-success btn-block"/>
</form>
<?php
include_once("html_iframe_footer.php");
go/eth_json_rpc.go

package main

import(
    _ "fmt"
	"net/http"
	"bytes"
	"io"
	"encoding/json"	
)

func callJsonRpc(urlStr string, jsonRpcVerStr string, methodStr string, paramsStr string) (map[string]string,error) {
	
	var err error
	response := make(map[string]string)

	reqBody := bytes.NewBufferString(
        `{"jsonrpc":"2.0","id":1,"method":"`+methodStr+`","params":`+paramsStr+`}`)
	response["req"] = reqBody.String()
	
	req, err := http.NewRequest(http.MethodPost, urlStr, reqBody)
	
	if err!=nil {
		return response, err
	}
	req.Header.Set("User-Agent", "btcschools.net(Go)")
	req.Header.Set("Content-Type", "application/json; charset=utf-8")
	
	c := http.Client{}
	
	res, err := c.Do(req)
	
	if err!=nil {
		return response,err
	}
	defer res.Body.Close()
	
	var content bytes.Buffer
	_, err = io.Copy(&content, res.Body)
	
	if err!=nil {
		return response,err
	}
	
	var jsonResp map[string]interface{}
	if err = json.Unmarshal(content.Bytes(), &jsonResp); err != nil {
		return response,err
	}
	//access result by jsonResp["result"]
	
	response["url"] = urlStr
	response["resp"] = string(content.Bytes())
	
	return response, nil
}

go/templates/eth_json_rpc.html

{{ template "html_iframe_header.html" .}}

{{if .error}}
	<div class="alert alert-danger">
        <strong>Error!</strong> {{ .error }}
    </div>
{{else if .url}}

	<div class="alert alert-success">
		<h6 class="mt-3">Host</h6>
		<textarea class="form-control" rows="1" readonly>{{.url}}</textarea>
		
		<h6 class="mt-3">JSON-RPC Request</h6>
		<textarea class="form-control" rows="5" id="comment" readonly>{{.req}}</textarea>
		
		<h6 class="mt-3">JSON-RPC Response</h6>
		<textarea class="form-control" rows="1" id="comment" readonly>{{.resp}}</textarea>
		
	</div>
{{end}}

<form id='this_form' action='?action=submit' method='post'>

	<div class="form-group">
		<label for="host">Host To Receive RPC:</label>
		
		<div class="input-group mb-3">
			<select id="host" name="host" class="form-control" >
			
			{{ $host := .host }}
				
			{{if not $host}}
				{{$host = ""}}
			{{end}}
			
			{{ range $key, $value := .hosts }}
				<option value='{{$key}}'
				{{if eq $host $key}}
					selected
				{{end}}
				>{{$value}}</option>
			{{end}}
			</select>
			<div class="input-group-append">
				<span class="input-group-text">
					/
				</span>
			</div>
			
			<input class="form-control" type='text' name='path' id='path' value='{{.path}}' placeholder="Put extra path or blank if it does not.">
			
		</div>
	</div>

	<div class="form-group">
        <label for="jsonver">Json RPC version:</label>
        <input class="form-control" type='text' name='jsonver' id='jsonver' value='{{if .jsonver}}{{.jsonver}}{{else}}2.0{{end}}'>
    </div>
	
	<div class="form-group">
        <label for="method">Method:</label>
        <input class="form-control" type='text' name='method' id='method' value='{{.method}}'>
    </div>

	<div class="form-group">
        <label for="params">Params:</label>
		<textarea class="form-control" rows="10" name='params' id='params' placeholder='Please refer to sample of value below'>{{.params}}</textarea>
		<small>
			<ol style="padding-left: 0; list-style: inside decimal;">
				<li>
					Must be JSON-ARRAY encoded string, start with '[' and end with ']'.
				</li>
				
				<li>
					<a href="https://eth.wiki/json-rpc/api" target="_blank">Refer to</a> <span class='grey_info'>Parameters</span> part under your selected method about required value.
				</li>
				
				<li>
					Sample of `Params` value.
					<div class="table-responsive">
						<table class="table table-sm table-bordered">
							<tr><th>Params</th><th>Description</th></tr>
							<tr>
								<td>[]</td>
								<td>Empty params array.</td>
							</tr>
							
							<tr>
								<td>["0x5cf1bdA8757b9c501190B0FcbC6B4ab8a4Bd04a5"]</td>
								<td>Params array with 1 address item.</td>
							</tr>
							
							<tr>
								<td>["0x5cf1bdA8757b9c501190B0FcbC6B4ab8a4Bd04a5","latest"]</td>
								<td>Params array with 1 address item & 1 block param.</td>
							</tr>
							
							<tr>
								<td>[{"from":"0x5cf1bdA8757b9c501190B0FcbC6B4ab8a4Bd04a5","to":"0xd46e8dd67c5d32be8058bb8eb970870f07244567"}]</td>
								<td>Params array with 1 object item.</td>
							</tr>
						</table>
					</div>
				</li>
			</ol>
		</small>
    </div>
		
    <input type='submit' class="btn btn-success btn-block"/>
</form>

{{ template "html_iframe_footer.html" .}}








Tutorials
About Us
Contents have been open source in GITHUB. Please give me a ⭐ if you found this helpful :)
Community
Problem? Raise me a new issue.
Support Us
Buy me a coffee. so i can spend more nights for this :)

BTCSCHOOLS would like to present you with more pratical but little theory throughout our tutorials. Pages' content are constantly keep reviewed to avoid mistakes, but we cannot warrant correctness of all contents. While using this site, you agree to accept our terms of use, cookie & privacy policy. Copyright 2019 by BTCSCHOOLS. All Rights Reserved.