<?php

$url = 'http://x:m3lch1s3d3k@127.0.0.1:8332';

$url = 'http://bitcoinrpc:EF/iNKFWzQkpoTB+VFaSGk1+qZPckHsLfKu+3HZddpRr@77.159.16.114:8332';

$data = array("method" => "getbestblockhash");
$json_data = json_encode($data);

$ch = curl_init($url);                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($json_data))                                                                       
);                                                                                                                   
$result = curl_exec($ch);
$obj_result = json_decode($result);
$block_hash = $obj_result->result;

$data = array("method" => "getblock", "params" => array($block_hash, 1, 1));
$json_data = json_encode($data);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($json_data))
);
$result = curl_exec($ch);
$obj_result = json_decode($result);
$obj_result = $obj_result->result;

//var_dump($obj_result);die();

//echo $result.PHP_EOL;
echo "----------------------------".PHP_EOL;
echo "##".$obj_result->hash.PHP_EOL;
echo "##".$obj_result->previousblockhash.PHP_EOL;
echo "##".$obj_result->nextblockhash.PHP_EOL;
echo "##".$obj_result->height.PHP_EOL;
$obj_result->topisto_inputs  = 0;
$obj_result->topisto_outputs = 0;
$obj_result->topisto_fees = 0;
foreach($obj_result->tx as $transaction)
{
  echo '>>>>'.$transaction->hash.PHP_EOL;
  $transaction->topisto_inputs = 0;
  foreach($transaction->vin as $tx_in)
  {
    if (isset($tx_in->txid))
    {
      echo '======'.$tx_in->txid.PHP_EOL;
      // {
      //   "method": "gettxout",
      //   "params": [ "'$txhash'", "'$index'", "'$includemempool'" ]
      // }
      $data = array("method" => "gettxout", "params" => array($tx_in->txid, $tx_in->vout, 0));
      $json_data = json_encode($data);

      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
      curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($json_data))
      );
      $result = curl_exec($ch);
      $objet1 = json_decode($result);
      if (isset($objet1->value)) $transaction->topisto_inputs += $objet1->value;
    }
  }

  $transaction->topisto_outputs = 0;
  foreach($transaction->vout as $tx_out)
    if (isset($tx_out->value)) $transaction->topisto_outputs += $tx_out->value;
  
  if (count($transaction->vin) == 0) $obj_result->topisto_fees += $transaction->topisto_outputs;

  $obj_result->topisto_inputs += $transaction->topisto_inputs;
  $obj_result->topisto_outputs += $transaction->topisto_outputs;
}
echo '##'.$obj_result->topisto_inputs.PHP_EOL;
echo '##'.$obj_result->topisto_outputs.PHP_EOL;
echo '##'.$obj_result->topisto_fees.PHP_EOL;
?>
