我尝试使用EasyBitcoin-PHP的修改版本连接到比特币节点。以下是我为使其在TOR上工作所做的修改:
我为TOR代理创建了新字段:代码列表1.1
...
public $response;//was there already
// TOR and other SOCK5 proxies
private $using_proxy; #new
private $proxy_host; #new
private $proxy_port; #new
private $id = 0;//was there already
...
然后我创建了两个新方法:代码清单1.2
...
/**
* @param string $host
* @param int $port
*/
public function set_proxy($host, $port)
{
$this->proxy_host = $host;
$this->proxy_port = $port;
}
/**
* @param boolean|TRUE $proxy_usage
*/
public function use_proxy($proxy_usage = TRUE)
{
$this->using_proxy = $proxy_usage;
}
...
最后我在__call()方法中添加了几行:代码清单1.3
// Build the cURL session
$curl = curl_init("{$this->proto}://{$this->host}:{$this->port}/{$this->url}");
$options = array(
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $this->username . ':' . $this->password,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_HTTPHEADER => array('Content-type: application/json'),
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $request
);
//What Follows is new!!!
if($this->using_proxy){
$options[CURLOPT_PROXY] = $this->proxy_host;
$options[CURLOPT_PROXYPORT] = $this->proxy_port;
$options[CURLOPT_PROXYTYPE] = CURLPROXY_SOCKS5_HOSTNAME;
}
我创建的字段和方法,以及我在__call()方法中添加的额外行与我在其他TOR PHP / cURL项目中使用的相同(每当我希望PHP / cURL通过TOR连接到站点时)他们工作得很好。
然后我安装并运行TOR Expert Bundle(在Windows上)并使用以下脚本从比特币节点获取信息。
代码清单2
<?php
require_once('easybitcoin.php');
$bitcoin = new Bitcoin('username','password','6cmgzwu3x57dr6z7.onion');
$bitcoin->set_proxy("127.0.0.1", "9050");
$bitcoin->use_proxy();
if($bitcoin->getinfo()){
} else {
var_dump($bitcoin->status);
var_dump($bitcoin->error);
}
以下是我在命令提示符下运行脚本时得到的结果:
int(0)
string(50) "Can't complete SOCKS5 connection to 0.0.0.0:0. (5)"
有时我得到这个:
int(0)
string(50) "Can't complete SOCKS5 connection to 0.0.0.0:0. (1)"
答案 0 :(得分:0)
问题解决了。脚本很完美。问题在于服务器的设置方式。