Beispiel #1
0
function bitcoin_list_accounts($minconf = 1)
{
    $bitcoin = maybe_connect_bitcoin();
    $accounts = $bitcoin->listaccounts($minconf);
    if (!INTEGER_BITCOIND) {
        foreach ($accounts as $name => $value) {
            $accounts[$name] = bitcoin_to_internal($value);
        }
    }
    return $accounts;
}
 /**
  * Performs a jsonRCP request and gets the results as an array
  *
  * @param string $method
  * @param array $params
  * @return array
  */
 public function __call($method, $params)
 {
     // check
     if (!is_scalar($method)) {
         throw new Exception('Method name has no scalar value');
     }
     // check
     if (is_array($params)) {
         // no keys
         $params = array_values($params);
     } else {
         throw new Exception('Params must be given as array');
     }
     // sets notification or request task
     if ($this->notification) {
         $currentId = NULL;
     } else {
         $currentId = $this->id;
     }
     // prepares the request
     $request = array('method' => $method, 'params' => $params, 'id' => $currentId);
     $request = json_encode($request);
     if (!INTEGER_BITCOIND) {
         // remove double quotes around strings representing numbers with 8 decimal places, so bitcoind can read them
         $request = preg_replace('/"(-?\\d+[.]\\d{8})"/', '$1', $request);
     }
     $this->debug && ($this->debug .= '***** Request *****' . "\n" . $request . "\n" . '***** End Of request *****' . "\n\n");
     // performs the HTTP POST
     $opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/json', 'ignore_errors' => 'true', 'content' => $request));
     $context = stream_context_create($opts);
     if ($fp = @fopen($this->url, 'r', false, $context)) {
         $response = '';
         while ($row = fgets($fp)) {
             $response .= trim($row) . "\n";
         }
         $this->debug && ($this->debug .= '***** Server response *****' . "\n" . $response . '***** End of server response *****' . "\n");
         if (!INTEGER_BITCOIND) {
             // put double quotes around numbers with 8 decimal places, so they won't be converted to a float
             $response = preg_replace('/:(-?\\d+[.]\\d{8})/', ':"$1"', $response);
         }
         $response = json_decode($response, true);
     } else {
         throw new Exception('Unable to connect.');
     }
     // debug output
     if ($this->debug) {
         echo nl2br($this->debug);
     }
     // final checks and return
     if (!$this->notification) {
         // check
         if ($response['id'] != $currentId) {
             throw new Exception('Incorrect response id (request id: ' . $currentId . ', response id: ' . $response['id'] . ')');
         }
         if (!is_null($response['error'])) {
             throw new Exception('Request error: ' . json_encode($response['error']));
         }
         if (!INTEGER_BITCOIND) {
             return bitcoin_to_internal($response['result']);
         }
         return $response['result'];
     } else {
         return true;
     }
 }