Commit d311c983 authored by bandi's avatar bandi

Import blockchain.info api php

parent 92566167
<?php
if(!function_exists('curl_init')) {
throw new Exception('The Blockchain client library requires the CURL PHP extension.');
}
require_once(dirname(__FILE__) . '/Blockchain/Create/Create.php');
require_once(dirname(__FILE__) . '/Blockchain/Create/WalletResponse.php');
require_once(dirname(__FILE__) . '/Blockchain/Explorer/Input.php');
require_once(dirname(__FILE__) . '/Blockchain/Explorer/UnspentOutput.php');
require_once(dirname(__FILE__) . '/Blockchain/Explorer/InventoryData.php');
require_once(dirname(__FILE__) . '/Blockchain/Explorer/SimpleBlock.php');
require_once(dirname(__FILE__) . '/Blockchain/Explorer/Address.php');
require_once(dirname(__FILE__) . '/Blockchain/Explorer/Output.php');
require_once(dirname(__FILE__) . '/Blockchain/Explorer/Explorer.php');
require_once(dirname(__FILE__) . '/Blockchain/Explorer/Block.php');
require_once(dirname(__FILE__) . '/Blockchain/Explorer/LatestBlock.php');
require_once(dirname(__FILE__) . '/Blockchain/Explorer/Transaction.php');
require_once(dirname(__FILE__) . '/Blockchain/Exception/FormatError.php');
require_once(dirname(__FILE__) . '/Blockchain/Exception/CredentialsError.php');
require_once(dirname(__FILE__) . '/Blockchain/Exception/ParameterError.php');
require_once(dirname(__FILE__) . '/Blockchain/Exception/HttpError.php');
require_once(dirname(__FILE__) . '/Blockchain/Exception/Error.php');
require_once(dirname(__FILE__) . '/Blockchain/Exception/ApiError.php');
require_once(dirname(__FILE__) . '/Blockchain/Conversion/Conversion.php');
require_once(dirname(__FILE__) . '/Blockchain/Wallet/Wallet.php');
require_once(dirname(__FILE__) . '/Blockchain/Wallet/WalletAddress.php');
require_once(dirname(__FILE__) . '/Blockchain/Wallet/PaymentResponse.php');
require_once(dirname(__FILE__) . '/Blockchain/Rates/Rates.php');
require_once(dirname(__FILE__) . '/Blockchain/Rates/Ticker.php');
require_once(dirname(__FILE__) . '/Blockchain/Stats/Stats.php');
require_once(dirname(__FILE__) . '/Blockchain/Stats/StatsResponse.php');
require_once(dirname(__FILE__) . '/Blockchain/Receive/Receive.php');
require_once(dirname(__FILE__) . '/Blockchain/Receive/ReceiveResponse.php');
require_once(dirname(__FILE__) . '/Blockchain/Blockchain.php');
require_once(dirname(__FILE__) . '/Blockchain/PushTX/Push.php');
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Short File Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
namespace Blockchain;
use \Blockchain\Exception\Error;
use \Blockchain\Exception\ApiError;
use \Blockchain\Exception\HttpError;
use \Blockchain\Create\Create;
use \Blockchain\Explorer\Explorer;
use \Blockchain\PushTX\Push;
use \Blockchain\Rates\Rates;
use \Blockchain\Receive\Receive;
use \Blockchain\Stats\Stats;
use \Blockchain\Wallet\Wallet;
// Check if BCMath module installed
if(!function_exists('bcscale')) {
throw new Error("BC Math module not installed.");
}
// Check if curl module installed
if(!function_exists('curl_init')) {
throw new Error("cURL module not installed.");
}
class Blockchain {
const URL = 'https://blockchain.info/';
private $ch;
private $api_code = null;
const DEBUG = true;
public $log = Array();
public function __construct($api_code=null) {
if(!is_null($api_code)) {
$this->api_code = $api_code;
}
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_USERAGENT, 'Blockchain-PHP/1.0');
curl_setopt($this->ch, CURLOPT_HEADER, false);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($this->ch, CURLOPT_TIMEOUT, 60);
curl_setopt($this->ch, CURLOPT_CAINFO, dirname(__FILE__).'/Blockchain/ca-bundle.crt');
$this->Create = new Create($this);
$this->Explorer = new Explorer($this);
$this->Push = new Push($this);
$this->Rates = new Rates($this);
$this->Receive = new Receive($this);
$this->Stats = new Stats($this);
$this->Wallet = new Wallet($this);
}
public function __destruct() {
curl_close($this->ch);
}
public function setTimeout($timeout) {
curl_setopt($this->ch, CURLOPT_TIMEOUT, intval($timeout));
}
public function post($resource, $data=null) {
curl_setopt($this->ch, CURLOPT_URL, self::URL.$resource);
curl_setopt($this->ch, CURLOPT_POST, true);
curl_setopt($this->ch, CURLOPT_HTTPHEADER,
array("Content-Type: application/x-www-form-urlencoded"));
if(!is_null($this->api_code)) {
$data['api_code'] = $this->api_code;
}
curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($data));
$json = $this->_call();
// throw ApiError if we get an 'error' field in the JSON
if(array_key_exists('error', $json)) {
throw new ApiError($json['error']);
}
return $json;
}
public function get($resource, $params=null) {
curl_setopt($this->ch, CURLOPT_POST, false);
if(!is_null($this->api_code)) {
$params['api_code'] = $this->api_code;
}
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array());
$query = http_build_query($params);
curl_setopt($this->ch, CURLOPT_URL, self::URL.$resource.'?'.$query);
return $this->_call();
}
private function _call() {
$t0 = microtime(true);
$response = curl_exec($this->ch);
$dt = microtime(true) - $t0;
if(curl_error($this->ch)) {
$info = curl_getinfo($this->ch);
throw new HttpError("Call to " . $info['url'] . " failed: " . curl_error($this->ch));
}
$json = json_decode($response, true);
if(is_null($json)) {
throw new Error("Unable to decode JSON response from Blockchain: " . $response);
}
if(self::DEBUG) {
$info = curl_getinfo($this->ch);
$this->log[] = array(
'curl_info' => $info,
'elapsed_ms' => round(1000*$dt)
);
}
return $json;
}
}
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Short File Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
namespace Blockchain\Conversion;
/**
* Short Class Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
class Conversion
{
/**
* Properties
*/
/**
* Methods
*/
/**
* Convert an incoming integer to a BTC string value
*/
static function BTC_int2str($val)
{
$a = bcmul($val, "1.0", 1);
return bcdiv($a, "100000000", 8);
}
/**
* Convert a float value to BTC satoshi integer string
*/
static function BTC_float2int($val)
{
return bcmul($val, "100000000", 0);
}
/**
* From comment on http://php.net/manual/en/ref.bc.php
*/
static function bcconv($fNumber)
{
$sAppend = '';
$iDecimals = ini_get('precision') - floor(log10(abs($fNumber)));
if (0 > $iDecimals) {
$fNumber *= pow(10, $iDecimals);
$sAppend = str_repeat('0', -$iDecimals);
$iDecimals = 0;
}
return number_format($fNumber, $iDecimals, '.', '').$sAppend;
}
}
\ No newline at end of file
<?php
namespace Blockchain\Create;
use \Blockchain\Blockchain;
use \Blockchain\Exception\ParameterError;
class Create {
public function __construct(Blockchain $blockchain) {
$this->blockchain = $blockchain;
}
public function create($password, $email=null, $label=null) {
return $this->doCreate($password, null, $email, $label);
}
public function createWithKey($password, $privKey, $email=null, $label=null) {
if(!isset($privKey) || is_null($privKey))
throw new ParameterError("Private Key required.");
return $this->doCreate($password, $privKey, $email, $label);
}
public function doCreate($password, $priv=null, $email=null, $label=null) {
if(!isset($password) || is_null($password))
throw new ParameterError("Password required.");
$params = array(
'password'=>$password,
'format'=>'json'
);
if(!is_null($priv))
$params['priv'] = $priv;
if(!is_null($email))
$params['email'] = $email;
if(!is_null($label))
$params['label'] = $label;
return new WalletResponse($this->blockchain->post('api/v2/create_wallet', $params));
}
}
\ No newline at end of file
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Short File Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
namespace Blockchain\Create;
/**
* Short Class Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
class WalletResponse
{
/**
* Properties
*/
/**
* @var string
*/
public $guid;
/**
* @var string
*/
public $address;
/**
* @var string
*/
public $link;
/**
* Methods
*/
/**
* __construct
*
* @param $json
*/
public function __construct($json)
{
if (array_key_exists('guid', $json)) {
$this->guid = $json['guid'];
}
if (array_key_exists('address', $json)) {
$this->address = $json['address'];
}
if (array_key_exists('link', $json)) {
$this->link = $json['link'];
}
}
}
\ No newline at end of file
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Short File Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
namespace Blockchain\Exception;
/**
* Short Class Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
class ApiError extends \Exception
{
/**
* Properties
*/
/**
* Methods
*/
}
\ No newline at end of file
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Short File Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
namespace Blockchain\Exception;
/**
* Short Class Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
class CredentialsError extends \Exception
{
/**
* Properties
*/
/**
* Methods
*/
}
\ No newline at end of file
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Short File Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
namespace Blockchain\Exception;
/**
* Short Class Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
class Error extends \Exception
{
/**
* Properties
*/
/**
* Methods
*/
}
\ No newline at end of file
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Short File Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
namespace Blockchain\Exception;
/**
* Short Class Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
class FormatError extends \Exception
{
/**
* Properties
*/
/**
* Methods
*/
}
\ No newline at end of file
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Short File Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
namespace Blockchain\Exception;
/**
* Short Class Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
class HttpError extends \Exception
{
/**
* Properties
*/
/**
* Methods
*/
}
\ No newline at end of file
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Short File Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
namespace Blockchain\Exception;
/**
* Short Class Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
class ParameterError extends \Exception
{
/**
* Properties
*/
/**
* Methods
*/
}
\ No newline at end of file
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Short File Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
namespace Blockchain\Explorer;
/**
* Short Class Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage
* @author anAuthor
* @copyright 2014 a Copyright
* @license a License
* @link http://www.aLink.com
*/
class Address
{
/**
* Properties
*/
public $hash160; // string
public $address; // string
public $n_tx; // int
public $total_received; // string, e.g. "12.64952835"
public $total_sent; // string, e.g. "12.64952835"
public $final_balance; // string, e.g. "12.64952835"
public $transactions = array(); // Array of Transaction objects
/**
* Methods
*/
public function __construct($json) {
if(array_key_exists('hash160', $json))
$this->hash160 = $json['hash160'];
if(array_key_exists('address', $json))
$this->address = $json['address'];
if(array_key_exists('n_tx', $json))
$this->n_tx = $json['n_tx'];
if(array_key_exists('total_received', $json))
$this->total_received = \Blockchain\Conversion\Conversion::BTC_int2str($json['total_received']);
if(array_key_exists('total_sent', $json))
$this->total_sent = \Blockchain\Conversion\Conversion::BTC_int2str($json['total_sent']);
if(array_key_exists('final_balance', $json))
$this->final_balance = \Blockchain\Conversion\Conversion::BTC_int2str($json['final_balance']);
if(array_key_exists('txs', $json)) {
foreach ($json['txs'] as $txn) {
$this->transactions[] = new Transaction($txn);
}
}
}
}
\ No newline at end of file
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Short File Description
*
* PHP version 5
*
* @category aCategory
* @package aPackage
* @subpackage aSubPackage