博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BTC功能类
阅读量:5307 次
发布时间:2019-06-14

本文共 5602 字,大约阅读时间需要 18 分钟。

setSSL('/full/path/to/mycertificate.cert'); // Make calls to bitcoind as methods for your object. Responses are returned as an array. // Examples: $bitcoin->getinfo(); $bitcoin->getrawtransaction('0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098',1); $bitcoin->getblock('000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'); // The full response (not usually needed) is stored in $this->response // while the raw JSON is stored in $this->raw_response // When a call fails for any reason, it will return FALSE and put the error message in $this->error // Example: echo $bitcoin->error; // The HTTP status code can be found in $this->status and will either be a valid HTTP status code // or will be 0 if cURL was unable to connect. // Example: echo $bitcoin->status; */ namespace org; class Bitcoin {
// Configuration options private $username; private $password; private $proto; private $host; private $port; private $url; private $CACertificate; // Information and debugging public $status; public $error; public $raw_response; public $response; private $id = 0; /** * @param string $username * @param string $password * @param string $host * @param int $port * @param string $proto * @param string $url */ public function __construct($username, $password, $host = 'localhost', $port = 8332, $url = null) {
$this->username = $username; $this->password = $password; $this->host = $host; $this->port = $port; $this->url = $url; // Set some defaults $this->proto = 'http'; $this->CACertificate = null; } /** * @param string|null $certificate */ public function setSSL($certificate = null) {
$this->proto = 'https'; // force HTTPS $this->CACertificate = $certificate; } public function __call($method, $params) {
$this->status = null; $this->error = null; $this->raw_response = null; $this->response = null; // If no parameters are passed, this will be an empty array $params = array_values($params); // The ID should be unique for each call $this->id++; // Build the request, it's ok that params might have any empty array $request = json_encode(array( 'method' => $method, 'params' => $params, 'id' => $this->id )); // 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 ); // This prevents users from getting the following warning when open_basedir is set: // Warning: curl_setopt() [function.curl-setopt]: // CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set if (ini_get('open_basedir')) {
unset($options[CURLOPT_FOLLOWLOCATION]); } if ($this->proto == 'https') {
// If the CA Certificate was specified we change CURL to look for it if (!empty($this->CACertificate)) {
$options[CURLOPT_CAINFO] = $this->CACertificate; $options[CURLOPT_CAPATH] = DIRNAME($this->CACertificate); } else {
// If not we need to assume the SSL cannot be verified // so we set this flag to FALSE to allow the connection $options[CURLOPT_SSL_VERIFYPEER] = false; } } curl_setopt_array($curl, $options); // Execute the request and decode to an array $this->raw_response = curl_exec($curl); $this->response = json_decode($this->raw_response, true); // If the status is not 200, something is wrong $this->status = curl_getinfo($curl, CURLINFO_HTTP_CODE); // If there was no error, this will be an empty string $curl_error = curl_error($curl); curl_close($curl); if (!empty($curl_error)) {
$this->error = $curl_error; } if ($this->response['error']) {
// If bitcoind returned an error, put that in $this->error $this->error = $this->response['error']['message']; } elseif ($this->status != 200) {
// If bitcoind didn't return a nice error message, we need to make our own switch ($this->status) {
case 400: $this->error = 'HTTP_BAD_REQUEST'; break; case 401: $this->error = 'HTTP_UNAUTHORIZED'; break; case 403: $this->error = 'HTTP_FORBIDDEN'; break; case 404: $this->error = 'HTTP_NOT_FOUND'; break; } } if ($this->error) {
return false; } return $this->response['result']; } }

转载于:https://www.cnblogs.com/wzjwffg/p/11277304.html

你可能感兴趣的文章
sed对指定行添加或删除注释
查看>>
C#矩形框沿直线移动
查看>>
springboot中访问jsp文件方式
查看>>
树的直径新求法、codeforces 690C3 Brain Network (hard)
查看>>
五子棋游戏SRS文档
查看>>
Hdu 2476 String painter (区间DP)
查看>>
找路径
查看>>
js、jquery获取当前url中各个参数
查看>>
Android webView解析URL参数
查看>>
一个汇编的HelloWorld!
查看>>
文科学生思维与理科学生思维对比
查看>>
一台电脑如何管理多个ssh key
查看>>
C# 定时关机小程序
查看>>
【blog】推荐一个博客系统后台管理模板 - pinghsu
查看>>
说说MySQL索引
查看>>
zabbix发送邮件脚本
查看>>
生成随机的数字和字母组合
查看>>
File类
查看>>
java学习-1
查看>>
unigui的菜单树补习【2】treeview
查看>>