Skip to content

Instantly share code, notes, and snippets.

@abyssluke

abyssluke/monatest.php

Last active Aug 29, 2015
Embed
What would you like to do?
Monacoin RPC test
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8"/>
<title>オマエモナー</title>
</head>
<body>
<h1>Monacoinと通信してみるよ!<a name="top"></a></h1>
<?php
// 一部https://github.com/monacoinproject/transactionsampleのぱくりです
require_once(__DIR__ . '/jsonRPCClient.php');
require_once("Net/GeoIP.php");
$host = 'localhost'; /* monacoind 又は monacoin-qt を実行中のホストのアドレス */
$rpcuser = 'rpcuser'; /* monacoin.conf で指定した rpcユーザー名 */
$rpcpassword = 'rpcpassword'; /* monacoin.conf で指定した rpcパスワード */
$rpcport = '9402'; /* monacoin.conf で指定した rpcポート */
$historyNum = 50; /* 取得するトランザクション数 */
$cntry = array();
$client = array();
$coindaddr = "http://$rpcuser:$rpcpassword@$host:$rpcport/";
$coind = new jsonRPCClient($coindaddr);
$geoip = Net_GeoIP::getInstance("/path/to/GeoIP.dat",Net_GeoIP::MEMORY_CACHE);
try{
/* アドレス取得 */
$ginfo = $coind->getinfo();
$cbln = $ginfo["balance"];
$ccnt = $ginfo["connections"];
$cblk = $ginfo["blocks"];
$cdif = $ginfo["difficulty"];
echo "<table border=\"1\">";
printf("<tr><th>Balance</th><td>%01.8f MONA</td</tr>",$cbln);
echo "<tr><th>Connection</th><td>$ccnt</td></tr>";
echo "<tr><th>Block</th><td>$cblk</td></tr>";
echo "<tr><th>Diff</th><td>$cdif</td></tr>";
echo "</table>";
$peer = $coind->getpeerinfo();
echo "<hr /><a name=\"peer\"></a>Peer Info<hr /><table border=\"1\">";
echo "<tr><th>Type</th><th>Addr</th><th>Ver</th><th>SubVer</th><th>BlkReq</th><th>SentB</th><th>RecvB</th><th>ConnTime</th><th>BanSc</th></tr>";
//echo "</table>";
foreach($peer as $nowpeer) {
echo "<tr>";
if($nowpeer["inbound"]) {
echo "<td>In</td>";
} else {
echo "<td>Out</td>";
}
$iponly = explode(":",$nowpeer["addr"]);
$nowcountry = $geoip->lookupCountryCode($iponly[0]);
echo "<td>".gethostbyaddr($iponly[0]).":".$iponly[1]."<br />(".$nowpeer["addr"].") [".$nowcountry."]</td>";
$cntry[$nowcountry]++;
echo "<td>".$nowpeer["version"]."</td>";
echo "<td>".$nowpeer["subver"]."</td>";
$client[$nowpeer["subver"]]++;
echo "<td>".$nowpeer["blocksrequested"]."</td>";
echo "<td>".$nowpeer["bytessent"]."</td>";
echo "<td>".$nowpeer["bytesrecv"]."</td>";
echo "<td>".date("Y/m/d H:i:s",$nowpeer["conntime"])."</td>";
echo "<td>".$nowpeer["banscore"]."</td>";
echo "</tr>";
}
echo "</table>";
echo "<hr />";
echo "<a name=\"country\"></a>Country<br />";
echo "<table border=\"1\">";
foreach($cntry as $cid => $now){
echo "<tr><th>$cid</th><td>$now</td></tr>";
}
echo "</table>";
echo "<hr />";
echo "<a name=\"client\"></a>Client<br />";
echo "<table border=\"1\">";
foreach($client as $cid => $now){
echo "<tr><th>$cid</th><td>$now</td></tr>";
}
echo "</table>";
} catch (Exception $e) {
echo 'エラー<br />';
}
?>
</body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment