Been a few servers ago and client iterations since I have used this but what once pulled stats from my home node, no longer works.
But I know RPC is working with something else right now so not sure what to make it it. But ya, the last time I used this code, maybe a yr ago it worked fine.
include "jsonRPCClient.php";
$username = 'you';
$password = 'wish';
$ip = 'id';
$port = tell;
$info = rpc_command( 'getinfo' );
if( is_array( $info) ) {
$required = array( 'difficulty', 'blocks' );
$numeric_data = get_numeric_data( $info, $required );
if( !$numeric_data ) {
exit( "get_info() failed to acquire needed data." );
}
extract( $numeric_data );
$write_arr[ 'timestamp' ] = time();
$write_arr[ 'estimated_difficulty' ] = $difficulty;
$write_arr += compact( 'difficulty', 'blocks' );
$blocks_since_difficulty_change = $blocks % 2016;
if( $blocks_since_difficulty_change ) {
$previous_difficulty_change = $blocks - $blocks_since_difficulty_change;
$current_block_timestamp = get_block_timestamp( $blocks );
$previous_block_timestamp = get_block_timestamp( $previous_difficulty_change );
if( ! $current_block_timestamp || ! $previous_block_timestamp ) {
exit( 'Could not get block timestamps' );
}
$average_block_time = ($current_block_timestamp - $previous_block_timestamp) / $blocks_since_difficulty_change;
$estimated_difficulty = $difficulty * 600 / $average_block_time;
$write_arr[ 'estimated_difficulty' ] = $estimated_difficulty;
}
write_file( $write_arr );
} else {
exit( 'Could not getinfo(). getinfo() output is not an array.' );
}
function rpc_command( $command, $arg = '' ) {
global $username, $password, $ip, $port;
static $rpc;
if( ! $rpc ) {
$rpc = new jsonRPCClient( "http://$username:$password@$ip:$port" );
}
try {
if( ! empty( $arg ) ) {
$ret = $rpc->$command( $arg );
} else {
$ret = $rpc->$command();
}
} catch ( Exception $e ) {
trigger_error( "Exception in rpc_command(). Command is $command. Error is: " . $e->getMessage() );
$ret = false;
}
return $ret;
}
function get_numeric_data( $info, $keys ) {
$ret = array();
foreach( $keys as $key ) {
if( isset( $info[ $key ] ) && is_numeric( $info[ $key ] ) ) {
$ret[ $key ] = $info[ $key ];
} else {
$ret = false;
break;
}
}
return $ret;
}
function get_block_timestamp( $block ) {
if( !is_numeric( $block ) ) {
return false;
}
$block_hash = rpc_command( 'getblockhash', $block );
if( $block_hash !== false && preg_match( '/^[0-9a-f]+$/i', $block_hash ) ) {
$block_data = rpc_command( 'getblock', $block_hash );
if( is_array( $block_data ) && isset( $block_data[ 'time' ] ) && is_numeric( $block_data[ 'time' ] ) ) {
return $block_data[ 'time' ];
} else {
trigger_error( "Error in get_block_timestamp(). Time in blockdata not found or not numeric. Block is $block" );
}
} else {
trigger_error( "Error in get_block_timestamp() could not getblockhash for block $block. Hash is $block_hash" );
}
return false;
}
function write_file( $data ) {
if( is_array( $data ) ) {
$h = fopen( 'getinfo.php', 'w' );
fwrite( $h, ". var_export( $data, true ) . ";" );
fclose( $h );
return true;
} else {
trigger_error( 'Error in write_file(). Data is not an array.' );
return false;
}
}
Out puts this when manually run, normally set on a cron...
[not@here]$ php getdata.php
Could not getinfo(). getinfo() output is not an array.
[not@here]$
I think I copied the entire files code ok, looks good. But ya,the last time I used this code about a yr ago it was / seemed fine and saved the required data to the secondary file I would then pull from to display on my personal sites.
Ty.