Created
September 7, 2017 14:25
-
-
Save nmmmnu/d328a1c81ee0fa14374d5f10b9c49344 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
class BitcoindRPC{ | |
private $url; | |
private $user; | |
private $pass; | |
function __construct($url, $user, $pass){ | |
$this->url = $url; | |
$this->user = $user; | |
$this->pass = $pass; | |
} | |
private static function post__($url, $user, $pass, $data){ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: text/plain']); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | |
curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $pass); | |
$r = curl_exec($ch); | |
return $r; | |
} | |
private function post_($data){ | |
return self::post__($this->url, $this->user, $this->pass, $data); | |
} | |
private static function json_prepare__($method, $params){ | |
$m = [ | |
"jsonrpc" => "1.0" , | |
"id" => 1 , | |
"method" => $method , | |
"params" => $params | |
]; | |
return json_encode($m); | |
} | |
private function json_exec_($method, $params = []){ | |
$r = json_decode($this->post_(self::json_prepare__($method, $params)), true); | |
/* | |
Array | |
( | |
[result] => Array | |
( | |
) | |
[error] => | |
[id] => 1 | |
) | |
*/ | |
if ($r["error"]) | |
return false; | |
return $r["result"]; | |
} | |
function info(){ | |
return $this->json_exec_("getinfo"); | |
} | |
function lastBlock(){ | |
return $this->json_exec_("getbestblockhash"); | |
} | |
function getBlock($hash){ | |
return $this->json_exec_("getblock", [ $hash ]); | |
} | |
function getTx($hash, $keep_hex = false){ | |
$r = $this->json_exec_("getrawtransaction", [ $hash, 1 ]); | |
if ($r === false) | |
return false; | |
if ($keep_hex == false) | |
unset($r["hex"]); | |
return $r; | |
} | |
} | |
$rpc = new BitcoindRPC("http://127.0.0.1:8332/", "niki", "123"); | |
//print_r($rpc->info()); | |
//print_r($rpc->lastBlock()); | |
$BLOCK_ZERO = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"; | |
$TX_ZERO = "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"; | |
$bl = $rpc->getBlock($BLOCK_ZERO); | |
//print_r($bl); | |
$bl = $rpc->getBlock($bl["nextblockhash"]); | |
$txhash = $bl["tx"][0]; | |
echo $txhash; | |
$tx = $rpc->getTx($txhash); | |
$tx = $rpc->getTx("d5e53b84d6fdb7db91b946061bf692a999a89327fd6238e79bd831d154c51fd3"); | |
print_r($tx); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment