-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bitcoin.php
68 lines (61 loc) · 1.53 KB
/
Bitcoin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* Bitcoin组件,扩展自CComponent
* @author greedying
* https://github.com/greedying/yii-bitcoin-component
* 2014.02.08
*/
class Bitcoin extends CApplicationComponent
{
/***
* 比特币服务器 rpcuser
* 需要和比特币配置文件中rpcuser相同
* ***/
public $rpcuser = null;
/***
* 比特币服务器 rpcpassword
* 需要和比特币配置文件中rpcpassword相同
**/
public $rpcpassword = null;
/***
* 比特币服务器ip
* **/
public $ip = null;
/***
* 比特币服务器端口,默认是8332
*/
public $port = 8332;
/***jsonrpcclient ***/
public $rpcclient = null;
public function init()
{
$this->rpcclient = new jsonRPCClient("http://{$this->rpcuser}:{$this->rpcpassword}@{$this->ip}:{$this->port}");
}
public function invoke($function, $args=array())
{
//避免json_decode精度问题,
ini_set('precision', 14);
$function = strtolower($function);//bitcoin里function都是小写
try{
if($args==array()){
return $this->rpcclient->$function();
}else{
//参数顺序需要与比特币服务器接口一样
$php = 'return $this->rpcclient->$function(';
for($i=0; $i<count($args); $i++){
$php .= '$args[' . $i . '],';
}
$php = rtrim($php, ',');
$php .= ');';
return eval($php);//调用jsonrpc
}
}catch(Exception $e){
echo 'wrong';
}
ini_set('precision', 20);
}
public function __call($name,$args=array())
{
return $this->invoke($name, $args);
}
}