Repository: nbobtc/bitcoind-php
Branch: 2.x
Commit: 6985d44d5c9a
Files: 27
Total size: 36.0 KB
Directory structure:
gitextract_6fwc20wd/
├── .gitattributes
├── .gitignore
├── .travis.yml
├── Berksfile
├── CHANGELOG.md
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── Vagrantfile
├── composer.json
├── docs/
│ └── .gitkeep
├── phpdoc.dist.xml
├── phpunit.xml.dist
├── src/
│ ├── Command/
│ │ ├── Command.php
│ │ └── CommandInterface.php
│ └── Http/
│ ├── Client.php
│ ├── ClientInterface.php
│ ├── Driver/
│ │ ├── CurlDriver.php
│ │ └── DriverInterface.php
│ └── Message/
│ ├── Message.php
│ ├── Request.php
│ ├── Response.php
│ ├── Streamable.php
│ └── Uri.php
└── tests/
├── Command/
│ └── CommandTest.php
└── Http/
├── ClientTest.php
└── Driver/
└── CurlDriverTest.php
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitattributes
================================================
test/ export-ignore
phpunit.xml.dist export-ignore
================================================
FILE: .gitignore
================================================
bin/
build/logs/
docs/api/
docs/code-coverage/
vendor/
composer.lock
phpdoc.xml
================================================
FILE: .travis.yml
================================================
language: php
php:
- 7
- 5.6
- 5.5
- 5.4
branches:
only:
- 1.x
- 2.x
- master
notifications:
email: false
irc: "chat.freenode.net#dspacelabs"
install:
- composer require "codeclimate/php-test-reporter:*" -n
- composer install
script:
- php bin/phpunit
after_script:
- bin/test-reporter --stdout > codeclimate.json
- "curl -X POST -d @codeclimate.json -H 'Content-Type: application/json' -H 'User-Agent: Code Climate (PHP Test Reporter v0.1.1)' https://codeclimate.com/test_reports"
================================================
FILE: Berksfile
================================================
source "https://supermarket.chef.io"
cookbook "bitcoind"
================================================
FILE: CHANGELOG.md
================================================
Change log
==========
## [Unreleased]
### Added
### Updated
### Fixed
### Changed
## [2.1.0] - 2017-12-22
### Added
- travis notifies irc channel #dspacelabs
### Updated
- [#18] Avoid reliance on own PSR-7 implementation
- [#29] Added Test Case for CurlDriver
## [2.0.2] - 2015-05-27
### Fixed
- [#16] incompatible with psr/http-message current version
## [2.0.1] - 2015-03-27
### Added
- Added Vagrant file to setup and configure nodes
### Fixed
- Small issue with the CurlDriver
## [2.0.0] - 2015-03-22
Initial release of 2.x
### Added
- Message
- Streamable
- Request
- Response
- Command
### Changed
- Complete Rewrite of Library
### Deprecated
- 1.x Releases
[unreleased]: https://github.com/nbobtc/bitcoind-php/compare/2.1.0...2.x
[2.1.0]: https://github.com/nbobtc/bitcoind-php/compare/2.0.2...2.1.0
[2.0.2]: https://github.com/nbobtc/bitcoind-php/compare/2.0.1...2.0.2
[2.0.1]: https://github.com/nbobtc/bitcoind-php/compare/2.0.0...2.0.1
[2.0.0]: https://github.com/nbobtc/bitcoind-php/compare/2d30e2f9ee617f44336581386cd0734613c7353d...2.0.0
[#16]: https://github.com/nbobtc/bitcoind-php/issues/16
[#18]: https://github.com/nbobtc/bitcoind-php/pull/18
[#29]: https://github.com/nbobtc/bitcoind-php/pull/29
================================================
FILE: CONTRIBUTING.md
================================================
Contributing Guidelines
=======================
This project uses some standards such as [PSR-1], [PSR-2], [PSR-3], and [PSR-4]
along with some standards that are being purposed.
## Getting Started
* Fork repository
* Pull requests should be on the develop branch.
* Code must conform to [PSR-1] and [PSR-2] standards.
## Finding Something to Work on
- Check out the [CodeClimate] issues page for this project.
- Check out the [GitHub Issues] page for this project.
- Check out [SensioLabs Insight] page for this project.
## Documentation
Feel free to add your own examples or usage to the documentation. If something
isn't clear, go head and submit a PR with your updated paragraph.
## Donate
Consider donating some [16yRSB46xMeWKfWtqcuqSVV7B2eSjkd92D]
[PSR-1]: http://www.php-fig.org/psr/psr-1/
[PSR-2]: http://www.php-fig.org/psr/psr-2/
[PSR-3]: http://www.php-fig.org/psr/psr-3/
[PSR-4]: http://www.php-fig.org/psr/psr-4/
[CodeClimate]: https://codeclimate.com/github/nbobtc/bitcoind-php/issues
[GitHub Issues]: https://github.com/nbobtc/bitcoind-php/issues
[SensioLabs Insight]: https://insight.sensiolabs.com/projects/c7af9182-f53b-4164-820d-46e7499252f3
[16yRSB46xMeWKfWtqcuqSVV7B2eSjkd92D]: bitcoin://16yRSB46xMeWKfWtqcuqSVV7B2eSjkd92D
================================================
FILE: LICENSE
================================================
Copyright (C) 2012-2015 Joshua Estes
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
================================================
FILE: README.md
================================================
nbobtc/bitcoind-php [](https://travis-ci.org/nbobtc/bitcoind-php) [](https://packagist.org/packages/nbobtc/bitcoind-php) [](https://packagist.org/packages/nbobtc/bitcoind-php)
===================
[](https://codeclimate.com/github/nbobtc/bitcoind-php) [](https://codeclimate.com/github/nbobtc/bitcoind-php) [](https://insight.sensiolabs.com/projects/c7af9182-f53b-4164-820d-46e7499252f3)
This project is used to interact with a headless bitcoin program called
bitcoind. It also contains various utility classes for working with Bitcoin as a
PHP Developer.
## Installation
You can install this library by using [Composer]. You can also view more info
about this on [Packagist].
```bash
composer require nbobtc/bitcoind-php
```
## Usage
To use the project you need to just create a new instance of the class.
```php
<?php
require __DIR__ . '/vendor/autoload.php';
$command = new \Nbobtc\Command\Command('getinfo');
$client = new \Nbobtc\Http\Client('https://username:password@localhost:18332');
/** @var \Nbobtc\Http\Message\Response */
$response = $client->sendCommand($command);
/** @var string */
$contents = $response->getBody()->getContents();
echo $contents;
```
You are able to get the [Request] and [Response] objects back from
the client with the correct getters: `getRequest()` and `getResponse()`.
You can also parse the response however you wish to do so since the result is
returned to you as a string. See below for some ideas!
## Commands
Commands are created in such a way that this will support any future updates the
[Bitcoin API] by providing you with an easy class that sets all the required
information.
You are able to pass into the object the `method` and the `parameters` that are
required. Here are a few examples:
```php
// No Parameters
$command = new Command('getinfo');
// One Parameter
$command = new Command('getblock', '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f');
// Multiple Parameters
$command = new Command('sendfrom', array('fromaccount', 'tobitcoinaddress', 'amount'));
```
The second argument MUST be in the same order as on the [Bitcoin API] wiki page.
There is no need to assign the values any keys.
### Parameters
Parameters are the second argument when creating a new Command. This argument
can either be a string OR an array. For example, both of these are valid.
```php
$command = new Command('getblock', array('000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'));
$command = new Command('getblock', '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f');
```
Most commands in the [Bitcoin API] take one parameter. If it takes MORE than
one, you must pass the parameters in as an array in the ORDER you find them on
that page.
### Extending Commands
If, for any reason, you need to extend a command, it MUST implement
[CommandInterface]. You can find documentation within the interface on how to
implement this.
## Drivers
Drivers are used by the ClientInterface for connecting to a bitcoind service and
sending Requests. The return a Response. If you need to implement a new driver
take a look at the [DriverInterface].
### cURL Driver
This is used by default and allows you a lot of options for customizing it to
your needs.
You can set various [cURL Options] by passing them into the function
`addCurlOption($option, $value)`.
Here's an example of how to configure and use the driver.
```php
$driver = new \Nbobtc\Http\Driver\CurlDriver();
$driver
->addCurlOption(CURLOPT_VERBOSE, true)
->addCurlOption(CURLOPT_STDERR, '/var/logs/curl.err');
$client = new \Nbobtc\Http\Client('https://username:password@localhost:18332');
$client->withDriver($driver);
```
Feel free to take a look at the `CurlDriver` source code.
## Cookbook
### How to enable a Keep-Alive ie Persistent Connection
This example shows how you are able to set the client up to [Persistent
Connection].
```php
$client = new \Nbobtc\Http\Client('https://username:password@localhost:18332');
$client->getRequest()->withHeader('Connection', 'Keep-Alive');
```
### How to set a CA Cert
This library provides some wonderful flexibility that will allow you to
configure the client to use your own CA Cert.
```php
$driver = new \Nbobtc\Http\Driver\CurlDriver();
$driver->addCurlOption(CURLOPT_CAINFO, '/path/to/cert');
$client = new \Nbobtc\Http\Client('https://username:password@localhost:18332');
$client->withDriver($driver);
```
### How to Convert Output to an Array
Some like the arrays
```php
$response = $client->sendCommand($command);
$output = json_decode($response->getBody()->getContents(), true);
```
### How to Convert Output to a stdClass object
Some like the objects
```php
$response = $client->sendCommand($command);
$output = json_decode($response->getBody()->getContents());
```
## Testing
All testing is done using PHPUnit. You should be able to run `phpunit` in the
root directory of this project (the directory where phpunit.xml.dist is located)
and the tests will run.
If submitting a pull request or working on this library, please make sure that
the tests will pass.
## Change log
See [CHANGELOG.md].
Contains information on releases such as what was added, changed, etc. It's good
to look at to see what has changed from release to release.
## Contributing
See [CONTRIBUTING.md].
Various ways on contributing to this project.
## Branching
### master
This is the latest and greatest, it should not be used an is considered
development for testing new features and functionality. This should NOT be used
in a production environment.
### 2.x
Current production branch. All 2.x tags come off of this branch.
### 1.x
Deprecated, only used for bug fixes and for historical records.
## License (MIT) [](https://github.com/nbobtc/bitcoind-php/blob/2.x/LICENSE)
Copyright (C) 2012-2018 Joshua Estes
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
[Composer]: https://getcomposer.org/
[Packagist]: https://packagist.org/packages/nbobtc/bitcoind-php
[CHANGELOG.md]: https://github.com/nbobtc/bitcoind-php/blob/2.x/CHANGELOG.md
[CONTRIBUTING.md]: https://github.com/nbobtc/bitcoind-php/blob/2.x/CONTRIBUTING.md
[Bitcoin API]: https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list
[Persistent Connection]: http://en.wikipedia.org/wiki/HTTP_persistent_connection
[cURL Options]: http://php.net/manual/en/function.curl-setopt.php
[Releases]: https://github.com/nbobtc/bitcoind-php/releases
[CommandInterface]: https://github.com/nbobtc/bitcoind-php/blob/2.x/src/Command/CommandInterface.php
[Request]: https://github.com/nbobtc/bitcoind-php/blob/2.x/src/Http/Message/Request.php
[Response]: https://github.com/nbobtc/bitcoind-php/blob/2.x/src/Http/Message/Response.php
[DriverInterface]: https://github.com/nbobtc/bitcoind-php/blob/2.x/src/Http/Driver/DriverInterface.php
================================================
FILE: Vagrantfile
================================================
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.berkshelf.enabled = true
config.vm.box = "ubuntu/trusty64"
config.vm.define "node01" do |node01|
node01.vm.network "private_network", ip: "192.168.33.10"
node01.vm.network "forwarded_port", guest: 19332, host: 18332
node01.vm.provision "chef_solo" do |chef|
chef.add_recipe "bitcoind"
chef.json = {
"bitcoind" => {
"config" => {
"options" => {
"regtest" => 1,
"gen" => 1,
"dnsseed" => 0,
"unpnp" => 0,
"port" => 19000,
"server" => 1,
"rpcuser" => "username",
"rpcpassword" => "password",
"rpcallowip" => ["0.0.0.0/0"],
"rpcport" => 19332
}
}
}
}
end
end
config.vm.define "node02" do |node02|
node02.vm.network "private_network", ip: "192.168.33.20"
node02.vm.network "forwarded_port", guest: 19332, host: 18333
node02.vm.provision "chef_solo" do |chef|
chef.add_recipe "bitcoind"
chef.json = {
"bitcoind" => {
"config" => {
"options" => {
"regtest" => 1,
"dnsseed" => 0,
"unpnp" => 0,
"port" => 19000,
"server" => 1,
"listen" => 0,
"connect" => "192.168.33.10:19000",
"rpcuser" => "username",
"rpcpassword" => "password",
"rpcallowip" => ["0.0.0.0/0"],
"rpcport" => 19332
}
}
}
}
end
end
end
================================================
FILE: composer.json
================================================
{
"name": "nbobtc/bitcoind-php",
"homepage": "https://github.com/nbobtc/bitcoind-php",
"type": "library",
"description": "PHP Wrapper for a bitcoind daemon",
"keywords": ["bitcoin", "bitcoind", "bitcoins"],
"license": "MIT",
"authors": [
{
"name": "Joshua Estes"
}
],
"support": {
"issues": "https://github.com/nbobtc/bitcoind-php/issues",
"source": "https://github.com/nbobtc/bitcoind-php"
},
"autoload": {
"psr-4": { "Nbobtc\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Tests\\Nbobtc\\": "tests/" }
},
"extra": {
"branch-alias": {
"1.x-dev": "1.3.x-dev",
"2.x-dev": "2.1.x-dev"
}
},
"require": {
"php": ">=5.4",
"ext-curl": "*",
"ext-json": "*",
"psr/log": "~1.0",
"psr/http-message": "~1.0",
"zendframework/zend-diactoros": "^1.3"
},
"require-dev": {
"phpunit/phpunit": "~4.5.0",
"mockery/mockery": "~0.9.0",
"fzaninotto/faker": "~1.4.0"
},
"config": {
"bin-dir": "bin"
},
"non-feature-branches": ["master"]
}
================================================
FILE: docs/.gitkeep
================================================
================================================
FILE: phpdoc.dist.xml
================================================
<?xml version="1.0" encoding="UTF-8" ?>
<phpdoc>
<title>API Documentation</title>
<parser>
<target>docs/api/</target>
</parser>
<transformer>
<target>docs/api/</target>
</transformer>
<files>
<directory>src/</directory>
</files>
<logging>
<paths>
<default>build/logs/phpdoc.log</default>
<errors>build/logs/phpdoc.err</errors>
</paths>
</logging>
</phpdoc>
================================================
FILE: phpunit.xml.dist
================================================
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
verbose="false">
<testsuites>
<testsuite name="Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src/</directory>
</whitelist>
</filter>
<listeners>
<listener class="\Mockery\Adapter\Phpunit\TestListener"></listener>
</listeners>
<logging>
<log type="coverage-html" target="docs/code-coverage" charset="UTF-8" />
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
================================================
FILE: src/Command/Command.php
================================================
<?php
/**
* @author Joshua Estes
* @copyright 2012-2015 Joshua Estes
* @license https://github.com/nbobtc/bitcoind-php/blob/2.x/LICENSE MIT
*/
namespace Nbobtc\Command;
/**
* @since 2.0.0
*/
class Command implements CommandInterface
{
/**
* @var string
*/
protected $method;
/**
* @var array
*/
protected $parameters;
/**
* @var string
*/
protected $id;
/**
* Creates a new Command object
*
* @since 2.0.0
* @param string $method
* @param array|null $parameters
* @param string $id
*/
public function __construct($method = null, $parameters = null, $id = null)
{
if (null !== $method) {
$this->withMethod($method);
}
if (null !== $parameters) {
$this->withParameters($parameters);
}
if (null !== $id) {
$this->withId($id);
}
}
/**
* @since 2.0.0
* {@inheritdoc}
*/
public function getMethod()
{
return $this->method;
}
/**
* @since 2.0.0
* {@inheritdoc}
*/
public function getParameters()
{
return $this->parameters;
}
/**
* @since 2.0.0
* {@inheritdoc}
*/
public function getId()
{
return $this->id;
}
/**
* @since 2.0.0
* {@inheritdoc}
*/
public function withMethod($method)
{
$this->method = $method;
return $this;
}
/**
* @since 2.0.0
* {@inheritdoc}
*/
public function withParameters($parameters)
{
if (is_array($parameters)) {
$this->parameters = $parameters;
} else {
$this->parameters = array($parameters);
}
return $this;
}
/**
* @since 2.0.0
* {@inheritdoc}
*/
public function withId($id)
{
$this->id = $id;
return $this;
}
}
================================================
FILE: src/Command/CommandInterface.php
================================================
<?php
/**
* @author Joshua Estes
* @copyright 2012-2015 Joshua Estes
* @license https://github.com/nbobtc/bitcoind-php/blob/2.x/LICENSE MIT
*/
namespace Nbobtc\Command;
/**
* @since 2.0.0
*/
interface CommandInterface
{
/**
* @since 2.0.0
* @return string
*/
public function getId();
/**
* @since 2.0.0
* @return array
*/
public function getParameters();
/**
* @since 2.0.0
* @return string
*/
public function getMethod();
/**
* @since 2.0.0
* @param string $method
* @return self
*/
public function withMethod($method);
/**
* @since 2.0.0
* @param array|string $parameters
* @return self
*/
public function withParameters($parameters);
/**
* @since 2.0.0
* @param string $id
* @return self
*/
public function withId($id);
}
================================================
FILE: src/Http/Client.php
================================================
<?php
/**
* @author Joshua Estes
* @copyright 2012-2015 Joshua Estes
* @license https://github.com/nbobtc/bitcoind-php/blob/2.x/LICENSE MIT
*/
namespace Nbobtc\Http;
use Nbobtc\Command\CommandInterface;
use Nbobtc\Http\Driver\CurlDriver;
use Nbobtc\Http\Driver\DriverInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Request;
/**
* @since 2.0.0
*/
class Client implements ClientInterface
{
/**
* @var \Psr\Http\Message\RequestInterface
*/
protected $request;
/**
* @var \Psr\Http\Message\ResponseInterface
*/
protected $response;
/**
* @var \Nbobtc\Http\Driver\DriverInterface
*/
protected $driver;
/**
* Creates a new Client object
*
* Currently you MUST pass in a DSN so the client knows where to send
* commands to.
*
* ```php
* $client = new \Nbobtc\Http\Client('https://username:password@localhost:18332');
* ```
*
* @since 2.0.0
* @param string $dsn Data Source Name
*
* @throws \InvalidArgumentException
*/
public function __construct($dsn)
{
$this->driver = new CurlDriver();
$this->request = (new Request($dsn))->withHeader('Content-Type', 'application/json');
}
/**
* @since 2.0.0
* {@inheritdoc}
*/
public function sendCommand(CommandInterface $command)
{
$body = new \Zend\Diactoros\Stream('php://temp', 'w+');
$body->write(json_encode(
array(
'method' => $command->getMethod(),
'params' => $command->getParameters(),
'id' => $command->getId(),
)
));
$request = $this->request->withBody($body);
/** @var \Psr\Http\Message\ResponseInterface */
$this->response = $this->driver->execute($request);
return $this->response;
}
/**
* Configures the Client to use a specific driver
*
* @since 2.0.0
* @param \Nbobtc\Http\Driver\DriverInterface $driver
* @return self
*/
public function withDriver(DriverInterface $driver)
{
$this->driver = $driver;
return $this;
}
/**
* Return the current Request object
*
* @since 2.0.0
* @return \Psr\Http\Message\RequestInterface
*/
public function getRequest()
{
return $this->request;
}
/**
* Returns the current Response object
*
* @since 2.0.0
* @return \Psr\Http\Message\ResponseInterface
*/
public function getResponse()
{
return $this->response;
}
}
================================================
FILE: src/Http/ClientInterface.php
================================================
<?php
/**
* @author Joshua Estes
* @copyright 2012-2015 Joshua Estes
* @license https://github.com/nbobtc/bitcoind-php/blob/2.x/LICENSE MIT
*/
namespace Nbobtc\Http;
use Nbobtc\Command\CommandInterface;
/**
* Client used to send commands to bitcoin servers/nodes
*
* Client is used to send commands to bitcoin servers and receives the
* responses back from those servers. The client does little to no processing
* or minipulation of the Command, Request, and Response objects.
*
* @since 2.0.0
*/
interface ClientInterface
{
/**
* Send Command to configured server
*
* Sends a command to a server and returns a Response object. If there was
* an error it will throw an Exception.
*
* @since 2.0.0
* @params \Nbobtc\Command\CommandInterface $command
* @return \Psr\Http\Message\ResponseInterface
* @throws \Exception
*/
public function sendCommand(CommandInterface $command);
}
================================================
FILE: src/Http/Driver/CurlDriver.php
================================================
<?php
/**
* @author Joshua Estes
* @copyright 2012-2015 Joshua Estes
* @license https://github.com/nbobtc/bitcoind-php/blob/2.x/LICENSE MIT
*/
namespace Nbobtc\Http\Driver;
use Psr\Http\Message\RequestInterface;
use Zend\Diactoros\Response;
/**
* Uses cURL to send Requests
*
* @since 2.0.0
*/
class CurlDriver implements DriverInterface
{
/**
* @var resource
*/
protected static $ch;
/**
* @var array
*/
protected $curlOptions = array();
/**
* @since 2.0.0
*/
public function __destruct()
{
if (null !== self::$ch) {
curl_close(self::$ch);
self::$ch = null; // Nulling to avoid calling curl_close again
}
}
/**
* @since 2.0.0
* {@inheritDoc}
*/
public function execute(RequestInterface $request)
{
$uri = $request->getUri();
if (null === self::$ch || gettype(self::$ch) != 'curl') {
self::$ch = curl_init();
}
curl_setopt_array(self::$ch, $this->getDefaultCurlOptions());
curl_setopt(self::$ch, CURLOPT_URL, sprintf('%s://%s@%s', $uri->getScheme(), $uri->getUserInfo(), $uri->getHost()));
curl_setopt(self::$ch, CURLOPT_PORT, $uri->getPort());
$headers = array();
foreach ($request->getHeaders() as $header => $values) {
$headers[] = $header.': '.implode(', ', $values);
}
curl_setopt(self::$ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt(self::$ch, CURLOPT_POSTFIELDS, (string) $request->getBody());
// Allows user to override any option, may cause errors
curl_setopt_array(self::$ch, $this->curlOptions);
/** @var string|false */
$result = curl_exec(self::$ch);
/** @var array|false */
$info = curl_getinfo(self::$ch);
/** @var string */
$error = curl_error(self::$ch);
if (!empty($error)) {
throw new \Exception($error);
}
$response = new Response();
$response = $response->withStatus($info['http_code']);
$response->getBody()->write($result);
$response->getBody()->rewind(); // empty getContents() without rewind()
return $response;
}
/**
* Add options to use for cURL requests
*
* @since 2.0.0
* @param integer $option
* @param mixed $value
*/
public function addCurlOption($option, $value)
{
$this->curlOptions[$option] = $value;
return $this;
}
/**
* Returns an array of cURL options
*
* @since 2.0.0
* @return array
*/
protected function getDefaultCurlOptions()
{
return array(
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 10,
);
}
}
================================================
FILE: src/Http/Driver/DriverInterface.php
================================================
<?php
/**
* @author Joshua Estes
* @copyright 2012-2015 Joshua Estes
* @license https://github.com/nbobtc/bitcoind-php/blob/2.x/LICENSE MIT
*/
namespace Nbobtc\Http\Driver;
use Psr\Http\Message\RequestInterface;
/**
* @since 2.0.0
*/
interface DriverInterface
{
/**
* Sends Request to server and returns a response.
*
* This will throw an Exception if there was an error
*
* @since 2.0.0
* @param \Psr\Http\Message\RequestInterface $request
* @return \Psr\Http\Message\ResponseInterface;
* @throws \Exception
*/
public function execute(RequestInterface $request);
}
================================================
FILE: src/Http/Message/Message.php
================================================
<?php
/**
* @author Joshua Estes
* @copyright 2012-2015 Joshua Estes
* @license https://github.com/nbobtc/bitcoind-php/blob/2.x/LICENSE MIT
*/
namespace Nbobtc\Http\Message;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamInterface;
/**
* @since 2.0.0
*
* @deprecated - please use a separate PSR-7 message implementation
*/
class Message implements MessageInterface
{
/**
* @var string
*/
protected $version = '1.1';
/**
* @var array
*/
protected $headers = array();
/**
* @var \Psr\Http\Message\StreamInterface
*/
protected $body;
/**
* @since 2.0.0
* {@inheritDoc}
*/
public function getProtocolVersion()
{
return $this->version;
}
/**
* @since 2.0.0
* {@inheritDoc}
*/
public function withProtocolVersion($version)
{
$this->version = (string) $version;
return $this;
}
/**
* @since 2.0.0
* {@inheritDoc}
*/
public function getHeaders()
{
return $this->headers;
}
/**
* @since 2.0.0
* {@inheritDoc}
*/
public function hasHeader($name)
{
foreach ($this->headers as $header => $values) {
if (0 === strcasecmp($header, $name)) {
return true;
}
}
return false;
}
/**
* @since 2.0.0
* {@inheritDoc}
*/
public function getHeader($name)
{
foreach ($this->headers as $header => $values) {
if (0 === strcasecmp($header, $name)) {
return $this->headers[$header];
}
}
return null;
}
/**
* @todo
* @since 2.0.2
* {@inheritDoc}
*/
public function getHeaderLine($name)
{
return '';
}
/**
* @since 2.0.0
* {@inheritDoc}
*/
public function getHeaderLines($name)
{
if ($lines = $this->getHeader($name)) {
return $lines;
}
return array();
}
/**
* @since 2.0.0
* {@inheritDoc}
*/
public function withHeader($name, $value)
{
if (is_array($value)) {
$this->headers[$name] = $value;
} else {
$this->headers[$name] = array($value);
}
return $this;
}
/**
* @since 2.0.0
* {@inheritDoc}
*/
public function withAddedHeader($name, $value)
{
$this->headers[$name] = $value;
return $this;
}
/**
* @since 2.0.0
* {@inheritDoc}
*/
public function withoutHeader($name)
{
foreach ($this->headers as $header => $values) {
if (0 === strcasecmp($header, $name)) {
unset($this->headers[$header]);
}
}
return $this;
}
/**
* @since 2.0.0
* {@inheritDoc}
*/
public function getBody()
{
if (null === $this->body) {
$this->body = new Streamable();
}
return $this->body;
}
/**
* @since 2.0.0
* {@inheritDoc}
*/
public function withBody(StreamInterface $body)
{
$this->body = $body;
return $this;
}
}
================================================
FILE: src/Http/Message/Request.php
================================================
<?php
/**
* @author Joshua Estes
* @copyright 2012-2015 Joshua Estes
* @license https://github.com/nbobtc/bitcoind-php/blob/2.x/LICENSE MIT
*/
namespace Nbobtc\Http\Message;
use Zend\Diactoros\Request as DiactorosRequest;
/**
* @since 2.0.0
*
* @deprecated - please use a separate PSR-7 request implementation
*/
class Request extends DiactorosRequest
{
/**
* HTTP Methods
*
* @var string
*
* @deprecated - constant was kept just for BC compliance
*/
const HTTP_POST = 'POST';
}
================================================
FILE: src/Http/Message/Response.php
================================================
<?php
/**
* @author Joshua Estes
* @copyright 2012-2015 Joshua Estes
* @license https://github.com/nbobtc/bitcoind-php/blob/2.x/LICENSE MIT
*/
namespace Nbobtc\Http\Message;
use Zend\Diactoros\Response as DiactorosResponse;
/**
* @since 2.0.0
*
* @deprecated - please use a separate PSR-7 response implementation
*/
class Response extends DiactorosResponse
{
/**
* Status Code Constants
*
* @var integer
*
* @deprecated - constant was kept just for BC compliance
*/
const HTTP_OK = 200;
}
================================================
FILE: src/Http/Message/Streamable.php
================================================
<?php
/**
* @author Joshua Estes
* @copyright 2012-2015 Joshua Estes
* @license https://github.com/nbobtc/bitcoind-php/blob/2.x/LICENSE MIT
*/
namespace Nbobtc\Http\Message;
use Zend\Diactoros\Stream;
/**
* Represents the body of the request/response
*
* This object does not use every function in the interface. Please be aware of
* this.
*
* @since 2.0.0
*
* @deprecated - please use a separate PSR-7 stream implementation
*/
class Streamable extends Stream
{
public function __construct()
{
parent::__construct('php://memory', 'wb+');
}
}
================================================
FILE: src/Http/Message/Uri.php
================================================
<?php
/**
* @author Joshua Estes
* @copyright 2012-2015 Joshua Estes
* @license https://github.com/nbobtc/bitcoind-php/blob/2.x/LICENSE MIT
*/
namespace Nbobtc\Http\Message;
use Zend\Diactoros\Uri as DiactorosUri;
/**
* @since 2.0.0
*
* @deprecated - please use a separate PSR-7 URI implementation
*/
class Uri extends DiactorosUri
{
}
================================================
FILE: tests/Command/CommandTest.php
================================================
<?php
/**
* @author Joshua Estes
* @copyright 2012-2015 Joshua Estes
* @license https://github.com/nbobtc/bitcoind-php/blob/2.x/LICENSE MIT
*/
namespace Tests\Nbobtc\Command;
use Nbobtc\Command\Command;
/**
*/
class CommandTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider commandDataProvider
*/
public function testCommand($method, $parameters, $id)
{
$command = new Command($method, $parameters, $id);
$this->assertEquals($method, $command->getMethod());
$this->assertEquals($parameters, $command->getParameters());
$this->assertEquals($id, $command->getId());
$command = new Command();
$this->assertNull($command->getMethod());
$this->assertEmpty($command->getParameters());
$this->assertNull($command->getId());
$this->assertInstanceOf('Nbobtc\Command\CommandInterface', $command->withMethod($method));
$this->assertEquals($method, $command->getMethod());
$this->assertInstanceOf('Nbobtc\Command\CommandInterface', $command->withParameters($parameters));
$this->assertEquals($parameters, $command->getParameters());
$this->assertInstanceOf('Nbobtc\Command\CommandInterface', $command->withId($id));
$this->assertEquals($id, $command->getId());
}
public function commandDataProvider()
{
return array(
array('getinfo', array('one'), 1),
);
}
public function testCommandWithParameters()
{
$command = new Command();
$this->assertEmpty($command->getParameters());
$command->withParameters('a');
$this->assertEquals(array('a'), $command->getParameters());
$command = new Command();
$this->assertEmpty($command->getParameters());
$command->withParameters(array('a'));
$this->assertEquals(array('a'), $command->getParameters());
}
}
================================================
FILE: tests/Http/ClientTest.php
================================================
<?php
/**
* @author Joshua Estes
* @copyright 2012-2015 Joshua Estes
* @license https://github.com/nbobtc/bitcoind-php/blob/2.x/LICENSE MIT
*/
namespace Tests\Nbobtc\Http;
use Nbobtc\Http\Client;
use Nbobtc\Command\Command;
/**
*/
class ClientTest extends \PHPUnit_Framework_TestCase
{
public function testClient()
{
$client = new Client('https://username:password@localhost:18332');
$this->assertNull($client->getResponse());
$request = $client->getRequest();
$this->assertInstanceOf('Psr\Http\Message\RequestInterface', $request);
$uri = $request->getUri();
$this->assertInstanceOf('Psr\Http\Message\UriInterface', $uri);
$this->assertEquals('https', $uri->getScheme());
$this->assertEquals('username:password', $uri->getUserInfo());
$this->assertEquals('localhost', $uri->getHost());
$this->assertEquals(18332, $uri->getPort());
}
public function testWithKeepAlive()
{
$client = new Client('https://username:password@localhost:18332');
$client->getRequest()->withHeader('Connection', 'Keep-Alive');
}
public function testSendCommand()
{
$response = \Mockery::mock('\Psr\Http\Message\ResponseInterface');
$driver = \Mockery::mock('\Nbobtc\Http\Driver\DriverInterface');
$driver
->shouldReceive('execute')
->andReturn($response);
$client = new Client('https://username:password@localhost:18332');
$client->withDriver($driver);
$command = new Command('gettransaction', array('transactionId'));
$response = $client->sendCommand($command);
}
}
================================================
FILE: tests/Http/Driver/CurlDriverTest.php
================================================
<?php
/**
* @author Thomas Kerin
* @copyright 2017 Thomas Kerin
* @license https://github.com/nbobtc/bitcoind-php/blob/2.x/LICENSE MIT
*/
namespace Tests\Nbobtc\Http\Driver;
use Nbobtc\Http\Driver\CurlDriver;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Request;
/**
*/
class CurlDriverTest extends \PHPUnit_Framework_TestCase
{
public function testExplodesWithTwo() {
$body = new \Zend\Diactoros\Stream('php://temp', 'w+');
$body->write("test");
$request = (new Request("https://google.com"));
$request = $request->withBody($body);
$driver = new CurlDriver();
/** @var \Psr\Http\Message\ResponseInterface */
$response = $driver->execute($request);
$this->assertInstanceOf("Psr\Http\Message\ResponseInterface", $response);
$driver = new CurlDriver();
/** @var \Psr\Http\Message\ResponseInterface */
$response = $driver->execute($request);
$this->assertInstanceOf("Psr\Http\Message\ResponseInterface", $response);
}
}
gitextract_6fwc20wd/
├── .gitattributes
├── .gitignore
├── .travis.yml
├── Berksfile
├── CHANGELOG.md
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── Vagrantfile
├── composer.json
├── docs/
│ └── .gitkeep
├── phpdoc.dist.xml
├── phpunit.xml.dist
├── src/
│ ├── Command/
│ │ ├── Command.php
│ │ └── CommandInterface.php
│ └── Http/
│ ├── Client.php
│ ├── ClientInterface.php
│ ├── Driver/
│ │ ├── CurlDriver.php
│ │ └── DriverInterface.php
│ └── Message/
│ ├── Message.php
│ ├── Request.php
│ ├── Response.php
│ ├── Streamable.php
│ └── Uri.php
└── tests/
├── Command/
│ └── CommandTest.php
└── Http/
├── ClientTest.php
└── Driver/
└── CurlDriverTest.php
SYMBOL INDEX (58 symbols across 14 files)
FILE: src/Command/Command.php
class Command (line 13) | class Command implements CommandInterface
method __construct (line 38) | public function __construct($method = null, $parameters = null, $id = ...
method getMethod (line 57) | public function getMethod()
method getParameters (line 66) | public function getParameters()
method getId (line 75) | public function getId()
method withMethod (line 84) | public function withMethod($method)
method withParameters (line 95) | public function withParameters($parameters)
method withId (line 110) | public function withId($id)
FILE: src/Command/CommandInterface.php
type CommandInterface (line 13) | interface CommandInterface
method getId (line 19) | public function getId();
method getParameters (line 25) | public function getParameters();
method getMethod (line 31) | public function getMethod();
method withMethod (line 38) | public function withMethod($method);
method withParameters (line 45) | public function withParameters($parameters);
method withId (line 52) | public function withId($id);
FILE: src/Http/Client.php
class Client (line 20) | class Client implements ClientInterface
method __construct (line 52) | public function __construct($dsn)
method sendCommand (line 62) | public function sendCommand(CommandInterface $command)
method withDriver (line 88) | public function withDriver(DriverInterface $driver)
method getRequest (line 101) | public function getRequest()
method getResponse (line 112) | public function getResponse()
FILE: src/Http/ClientInterface.php
type ClientInterface (line 21) | interface ClientInterface
method sendCommand (line 34) | public function sendCommand(CommandInterface $command);
FILE: src/Http/Driver/CurlDriver.php
class CurlDriver (line 18) | class CurlDriver implements DriverInterface
method __destruct (line 33) | public function __destruct()
method execute (line 45) | public function execute(RequestInterface $request)
method addCurlOption (line 96) | public function addCurlOption($option, $value)
method getDefaultCurlOptions (line 109) | protected function getDefaultCurlOptions()
FILE: src/Http/Driver/DriverInterface.php
type DriverInterface (line 15) | interface DriverInterface
method execute (line 27) | public function execute(RequestInterface $request);
FILE: src/Http/Message/Message.php
class Message (line 18) | class Message implements MessageInterface
method getProtocolVersion (line 39) | public function getProtocolVersion()
method withProtocolVersion (line 48) | public function withProtocolVersion($version)
method getHeaders (line 59) | public function getHeaders()
method hasHeader (line 68) | public function hasHeader($name)
method getHeader (line 83) | public function getHeader($name)
method getHeaderLine (line 99) | public function getHeaderLine($name)
method getHeaderLines (line 108) | public function getHeaderLines($name)
method withHeader (line 121) | public function withHeader($name, $value)
method withAddedHeader (line 136) | public function withAddedHeader($name, $value)
method withoutHeader (line 147) | public function withoutHeader($name)
method getBody (line 162) | public function getBody()
method withBody (line 175) | public function withBody(StreamInterface $body)
FILE: src/Http/Message/Request.php
class Request (line 17) | class Request extends DiactorosRequest
FILE: src/Http/Message/Response.php
class Response (line 17) | class Response extends DiactorosResponse
FILE: src/Http/Message/Streamable.php
class Streamable (line 22) | class Streamable extends Stream
method __construct (line 24) | public function __construct()
FILE: src/Http/Message/Uri.php
class Uri (line 17) | class Uri extends DiactorosUri
FILE: tests/Command/CommandTest.php
class CommandTest (line 14) | class CommandTest extends \PHPUnit_Framework_TestCase
method testCommand (line 19) | public function testCommand($method, $parameters, $id)
method commandDataProvider (line 40) | public function commandDataProvider()
method testCommandWithParameters (line 47) | public function testCommandWithParameters()
FILE: tests/Http/ClientTest.php
class ClientTest (line 15) | class ClientTest extends \PHPUnit_Framework_TestCase
method testClient (line 17) | public function testClient()
method testWithKeepAlive (line 33) | public function testWithKeepAlive()
method testSendCommand (line 39) | public function testSendCommand()
FILE: tests/Http/Driver/CurlDriverTest.php
class CurlDriverTest (line 15) | class CurlDriverTest extends \PHPUnit_Framework_TestCase
method testExplodesWithTwo (line 17) | public function testExplodesWithTwo() {
Condensed preview — 27 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (40K chars).
[
{
"path": ".gitattributes",
"chars": 51,
"preview": "test/ export-ignore\nphpunit.xml.dist export-ignore\n"
},
{
"path": ".gitignore",
"chars": 80,
"preview": "bin/\nbuild/logs/\ndocs/api/\ndocs/code-coverage/\nvendor/\ncomposer.lock\nphpdoc.xml\n"
},
{
"path": ".travis.yml",
"chars": 560,
"preview": "language: php\n\nphp:\n - 7\n - 5.6\n - 5.5\n - 5.4\n\nbranches:\n only:\n - 1.x\n - 2.x\n - mas"
},
{
"path": "Berksfile",
"chars": 58,
"preview": "source \"https://supermarket.chef.io\"\n\ncookbook \"bitcoind\"\n"
},
{
"path": "CHANGELOG.md",
"chars": 1242,
"preview": "Change log\n==========\n\n## [Unreleased]\n\n### Added\n### Updated\n### Fixed\n### Changed\n\n\n## [2.1.0] - 2017-12-22\n\n### Added"
},
{
"path": "CONTRIBUTING.md",
"chars": 1255,
"preview": "Contributing Guidelines\n=======================\n\nThis project uses some standards such as [PSR-1], [PSR-2], [PSR-3], and"
},
{
"path": "LICENSE",
"chars": 1061,
"preview": "Copyright (C) 2012-2015 Joshua Estes\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nth"
},
{
"path": "README.md",
"chars": 8386,
"preview": "nbobtc/bitcoind-php [](https://travis-ci.org/"
},
{
"path": "Vagrantfile",
"chars": 1756,
"preview": "# -*- mode: ruby -*-\n# vi: set ft=ruby :\nVagrant.configure(2) do |config|\n config.berkshelf.enabled = true\n config.vm."
},
{
"path": "composer.json",
"chars": 1186,
"preview": "{\n \"name\": \"nbobtc/bitcoind-php\",\n \"homepage\": \"https://github.com/nbobtc/bitcoind-php\",\n \"type\": \"library\",\n "
},
{
"path": "docs/.gitkeep",
"chars": 0,
"preview": ""
},
{
"path": "phpdoc.dist.xml",
"chars": 457,
"preview": "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<phpdoc>\n <title>API Documentation</title>\n <parser>\n <target>docs/"
},
{
"path": "phpunit.xml.dist",
"chars": 979,
"preview": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<phpunit\n backupGlobals=\"false\"\n backupStaticAttributes=\"false\"\n bootstr"
},
{
"path": "src/Command/Command.php",
"chars": 1953,
"preview": "<?php\n/**\n * @author Joshua Estes\n * @copyright 2012-2015 Joshua Estes\n * @license https://github.com/nbobtc/bitcoind-ph"
},
{
"path": "src/Command/CommandInterface.php",
"chars": 891,
"preview": "<?php\n/**\n * @author Joshua Estes\n * @copyright 2012-2015 Joshua Estes\n * @license https://github.com/nbobtc/bitcoind-ph"
},
{
"path": "src/Http/Client.php",
"chars": 2673,
"preview": "<?php\n/**\n * @author Joshua Estes\n * @copyright 2012-2015 Joshua Estes\n * @license https://github.com/nbobtc/bitcoind-ph"
},
{
"path": "src/Http/ClientInterface.php",
"chars": 950,
"preview": "<?php\n/**\n * @author Joshua Estes\n * @copyright 2012-2015 Joshua Estes\n * @license https://github.com/nbobtc/bitcoind-ph"
},
{
"path": "src/Http/Driver/CurlDriver.php",
"chars": 2891,
"preview": "<?php\n/**\n * @author Joshua Estes\n * @copyright 2012-2015 Joshua Estes\n * @license https://github.com/nbobtc/bitcoind-ph"
},
{
"path": "src/Http/Driver/DriverInterface.php",
"chars": 628,
"preview": "<?php\n/**\n * @author Joshua Estes\n * @copyright 2012-2015 Joshua Estes\n * @license https://github.com/nbobtc/bitcoind-ph"
},
{
"path": "src/Http/Message/Message.php",
"chars": 3234,
"preview": "<?php\n/**\n * @author Joshua Estes\n * @copyright 2012-2015 Joshua Estes\n * @license https://github.com/nbobtc/bitcoind-ph"
},
{
"path": "src/Http/Message/Request.php",
"chars": 528,
"preview": "<?php\n/**\n * @author Joshua Estes\n * @copyright 2012-2015 Joshua Estes\n * @license https://github.com/nbobtc/bitcoind-ph"
},
{
"path": "src/Http/Message/Response.php",
"chars": 538,
"preview": "<?php\n/**\n * @author Joshua Estes\n * @copyright 2012-2015 Joshua Estes\n * @license https://github.com/nbobtc/bitcoind-ph"
},
{
"path": "src/Http/Message/Streamable.php",
"chars": 578,
"preview": "<?php\n/**\n * @author Joshua Estes\n * @copyright 2012-2015 Joshua Estes\n * @license https://github.com/nbobtc/bitcoind-ph"
},
{
"path": "src/Http/Message/Uri.php",
"chars": 347,
"preview": "<?php\n/**\n * @author Joshua Estes\n * @copyright 2012-2015 Joshua Estes\n * @license https://github.com/nbobtc/bitcoind-ph"
},
{
"path": "tests/Command/CommandTest.php",
"chars": 1910,
"preview": "<?php\n/**\n * @author Joshua Estes\n * @copyright 2012-2015 Joshua Estes\n * @license https://github.com/nbobtc/bitcoind-ph"
},
{
"path": "tests/Http/ClientTest.php",
"chars": 1670,
"preview": "<?php\n/**\n * @author Joshua Estes\n * @copyright 2012-2015 Joshua Estes\n * @license https://github.com/nbobtc/bitcoind-ph"
},
{
"path": "tests/Http/Driver/CurlDriverTest.php",
"chars": 1045,
"preview": "<?php\n/**\n * @author Thomas Kerin\n * @copyright 2017 Thomas Kerin\n * @license https://github.com/nbobtc/bitcoind-php/blo"
}
]
About this extraction
This page contains the full source code of the nbobtc/bitcoind-php GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 27 files (36.0 KB), approximately 10.7k tokens, and a symbol index with 58 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.