sandrokeil/blockchain-wallet-api

区块链钱包api的Zend Framework 2 (ZF2)客户端库。通过对象配置请求、调用服务并访问响应数据。

1.0.0 2014-07-01 21:36 UTC

This package is auto-updated.

Last update: 2024-08-28 22:37:23 UTC


README

您需要一个简单的区块链钱包api客户端吗?

您想通过对象配置请求、调用服务并访问响应数据吗?

您想在mBTC或uBTC中显示比特币吗?

这个模块就是您的救星!

Build Status Scrutinizer Code Quality Coverage Status HHVM Status SensioLabsInsight Latest Stable Version Dependency Status Total Downloads License

区块链钱包api的Zend Framework 2客户端库。blockchain wallet api的用法简单。配置请求,调用服务并通过对象访问响应数据。

  • 适应您的需求。配置此模块有多种可能性。
  • 经过充分测试。除了单元测试和持续集成/检查外,此解决方案也适用于生产使用。
  • 坚实的基础。基于Zend Framework 2Easy Config
  • 每个变更都有记录。想了解新功能?请查看CHANGELOG.md
  • 倾听您的想法。有一个好主意?带来您经过测试的拉取请求或打开一个新问题。请参阅CONTRIBUTING.md

安装

此模块使用composer进行安装。有关composer文档,请参阅getcomposer.org

将以下内容放入您的composer.json文件中

{
    "require": {
        "sandrokeil/blockchain-wallet-api": "~1.0"
    }
}

然后将Sake\BlockchainWalletApi添加到您的./config/application.config.php文件中。

config/blockchainwalletapi.local.php.dist复制到config/blockchainwalletapi.local.php并配置凭证。请勿将此文件提交到公共仓库!

文档

有关请求详细信息,请参阅blockchain wallet api文档

这些请求类与api方法匹配

  • Send => payment
  • SendMany => sendmany
  • WalletBalance => balance
  • ListAddresses => list
  • AddressBalance => address_balance
  • NewAddress => new_address
  • AddressArchive => archive_address
  • AddressUnarchive => unarchive_address
  • AutoConsolidateAddresses => auto_consolidate

配置

连接参数可以在应用程序配置中定义

<?php

return array(
    'sake_bwa' => array(
        'connection' => array(
            'default' => array(
                'options' => array(
                    // see \Sake\BlockchainWalletApi\Service\BlockchainWalletOptions for all configurations
                    'url' => 'https://blockchain.info/de/merchant/', // note on your country
                    'guid' => 'your My Wallet identifier (found on the login page)',
                    'main_password' => 'Your Main My wallet password',
                    'second_password' => 'Your second My Wallet password if double encryption is enabled',
                ),
                'client' => 'Service factory name for Http Client, Lazy-loads a Zend\Http\Client instance if none registered'
            )
        )
    )
);

注册的服务名称

  • sake_bwa.service.default:一个用于发送请求到api的\Sake\BlockchainWalletApi\Service\BlockchainWallet实例
  • sake_bwa.service.response:一个用于通过api方法名称创建响应的\Sake\BlockchainWalletApi\Service\ResponsePluginManager服务插件管理器
  • sake_bwa.service.request:一个用于通过api方法名称创建请求的\Sake\BlockchainWalletApi\Service\RequestPluginManager服务插件管理器
  • sake_bwa.service.input_filter:一个用于通过api方法名称创建输入过滤器的\Sake\BlockchainWalletApi\Service\InputFilterPluginManager服务插件管理器
  • sake_bwa.service.hydrator:一个带有请求/响应策略和过滤器的\Zend\Stdlib\Hydrator\ClassMethods实例

注册的视图助手

要使用此视图助手,您必须将zendframework/zend-view添加到您的composer依赖项中。

  • satoshi:一个将satashi转换为其他单位(例如比特币)的\Zend\View\Helper\AbstractHelper实例

示例

本模块非常易于使用。然而,以下代码片段可以帮助您开始。

发送比特币

以下是如何将交易发送到比特币地址的示例

<?php
use Sake\BlockchainWalletApi;

// $sl is the service locator
$blockchain = $sl->get('sake_bwa.service.default');

/* @var $request BlockchainWalletApi\Request\Send */
$request = $sl->get('sake_bwa.service.request')->get('payment');
// or
$request = new BlockchainWalletApi\Request\Send();

$request->setAmount(100000); // in satoshi
$request->setTo('1KwbP2sRHW7uDsxnW8sBbymVwnSsm8cFXC');

try {
    // validate request
    if ($blockchain->isValid($request)) {
        /* @var $response BlockchainWalletApi\Response\Send */
        $response = $service->send($request);
        // access to response data
        $transactionHash = $response->getTxHash();
    }
} catch (BlockchainWalletApi\Exception\ExceptionInterface $exception) {
    // error handling
}

向多个地址发送比特币

以下是如何在同一交易中向多个收款人发送交易的示例

<?php
use Sake\BlockchainWalletApi;

// $sl is the service locator
$blockchain = $sl->get('sake_bwa.service.default');

/* @var $request BlockchainWalletApi\Request\SendMany */
$request = $sl->get('sake_bwa.service.request')->get('sendmany');
// or
$request = new BlockchainWalletApi\Request\SendMany();

$request->setRecipients(
    array(
        new BlockchainWalletApi\Request\Recipient('1BzHqGWhdpXyLqiYkAT7sasfCoffYo79tT', 10000),
        new BlockchainWalletApi\Request\Recipient('1NqH4QkkjDErD9TNC7arDQVMv4zKgfCzmb', 10000),
    )
);

try {
    // validate request
    if ($blockchain->isValid($request)) {
        /* @var $response BlockchainWalletApi\Response\SendMany */
        $response = $service->send($request);
        // access to response data
        $transactionHash = $response->getTxHash();
    }
} catch (BlockchainWalletApi\Exception\ExceptionInterface $exception) {
    // error handling
}

获取钱包余额

以下是如何检索钱包余额的示例

<?php
use Sake\BlockchainWalletApi;

// $sl is the service locator
/* @var $blockchain BlockchainWalletApi\Service\BlockchainWallet */
$blockchain = $sl->get('sake_bwa.service.default');

/* @var $request BlockchainWalletApi\Request\WalletBalance */
$request = $sl->get('sake_bwa.service.request')->get('balance');
// or
$request = new BlockchainWalletApi\Request\WalletBalance();

try {
    // validate request
    if ($blockchain->isValid($request)) {
        /* @var $response BlockchainWalletApi\Response\WalletBalance */
        $response = $blockchain->send($request);
        // access to response data
        $balance = $response->getBalance(); // in satoshi
    }
} catch (BlockchainWalletApi\Exception\ExceptionInterface $exception) {
    // error handling
}

使用视图助手将satosh转换为其他单位,例如比特币

以下是如何使用satosh视图助手将satosh转换为其他单位的示例

<?php
// assume we are in a template

/* @var $response \Sake\BlockchainWalletApi\Response\WalletBalance */
echo $this->satoshi($response->getBalanace(), 'BTC'); // Bitcoin
// or
echo $this->satoshi($response->getBalanace(), 'mBTC'); // Milli Bits
// or
echo $this->satoshi($response->getBalanace(), 'uBTC'); // Micro Bitcoin