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.
<?php
include_once "../libraries/vendor/autoload.php";
$hosts = ["https://mainnet.infura.io"=>"https://mainnet.infura.io","https://goerli.infura.io"=>"https://goerli.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", "https://data-seed-prebsc-1-s2.binance.org:8545"=>"https://data-seed-prebsc-1-s2.binance.org:8545"];
$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'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$ajaxData = [];
if ($_GET['action'] == 'add_json_request') {
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.");
} else if (isset($result['error'])) {
throw new Exception(json_encode($result['error']));
}
curl_close ($ch);
$ajaxData['data'] = ["method"=>$_POST['method'], "params"=>$paramsParams, "jsonrpc"=>$_POST['jsonver'], "id"=>$requestId];
} catch (Exception $e) {
$errmsg .= "Problem found. " . $e->getMessage();
}
if ($_GET['ajax'] == '1') {
if ($errmsg) {
$ajaxData['error'] = $errmsg;
}
die(json_encode($ajaxData));
}
}
}
include_once("html_iframe_header.php");
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();
$requests = [];
foreach($_POST["requests"] as $request) {
$requests[] = json_decode($request, true);
if (json_last_error() != JSON_ERROR_NONE) {
throw new Exception("Some request is not a proper JSON string.");
}
}
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$payload = json_encode($requests));
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);
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 $payload;?></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']: "2.0"?>' readonly>
</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=''>
<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>
<div class="input-group mb-3">
<input class="form-control" name='params' id='params'/>
<div class="input-group-append">
<input class="btn btn-success" type="button" value=" + " onclick="
var self = $(this);
self.val('...');
var form = self.closest('form');
$.ajax({
url: '?ajax=1&action=add_json_request',
type: 'post',
data: $('#this_form :input'),
success:function(result){
try {
j = eval('(' + result + ')');
if ('error' in j && j.error.length>0) {
var error = true;
} else {
var error = false;
}
if (!error) {
$('<input>').attr({
type: 'text',
name: 'requests[]',
value: JSON.stringify(j.data),
readonly: 'readonly',
class: 'request-item',
style: 'border-radius:5px;border:1px solid black;margin-right:5px;margin-top:25px;width:300px;'
}).appendTo(form);
var num = $('input[class=request-item]',form).length;
$('input[type=submit]').val('Submit ('+num+' Request)');
} else {
alert(j.error);
}
} catch(e) {
alert('Invalid Json Format.');
}
},
complete:function() {
self.val(' + ');
}
});
"/>
</div>
</div>
<small>
Must be JSON-ARRAY encoded string, start with '[' and end with ']'.
</small>
</div>
<input type='submit' class="btn btn-success btn-block" value="Submit (0 Requests)"/>
</form>
<?php
include_once("html_iframe_footer.php");