// <a href="http://dev.blockcypher.com/#creating-transactions">http://dev.blockcypher.com/#creating-transactions</a>
//
// API used: POST /v1/btc/main/txs/new
// Source wallet:
// <a href="http://api.blockcypher.com/v1/btc/main/addrs/5596926E976A1149871172?token=c0afcccdde5081d6429de37d16166ead">5596926E976A1149871172</a>
// Destination address:
// <a href="https://live.blockcypher.com/btc-testnet/address/mvwhcFDFjmbDWCwVJ73b8DcG6bso3CZXDj/">mvwhcFDFjmbDWCwVJ73b8DcG6bso3CZXDj</a>
require __DIR__ . '/../bootstrap.php';
/// Tx inputs
$input = new \BlockCypher\Api\TXInput();
$input->setWalletName("5596926E976A1149871172");
//$input->setWalletName("AC33394F28292099183");
$input->setWalletToken("c0afcccdde5081d6429de37d16166ead");
/// Tx outputs
$output = new \BlockCypher\Api\TXOutput();
$output->addAddress("mvwhcFDFjmbDWCwVJ73b8DcG6bso3CZXDj");
$output->setValue(1000);
// Satoshis
/// Tx
$tx = new \BlockCypher\Api\TX();
$tx->addInput($input);
$tx->addOutput($output);
/// For Sample Purposes Only.
$request = clone $tx;
$txClient = new \BlockCypher\Client\TXClient($apiContexts['BTC.test3']);
try {
    $output = $txClient->create($tx);
} catch (\BlockCypher\Exception\BlockCypherConnectionException $ex) {
    $data = $ex->getData();
    $txSkeleton = new \BlockCypher\Api\TXSkeleton($data);
    ResultPrinter::printError("Created TX", "TXSkeleton", null, $request, $ex);
<?php

// Run on console:
// php -f .\sample\transaction-api\NewTransactionEndpoint.php
require __DIR__ . '/../bootstrap.php';
use BlockCypher\Api\TX;
use BlockCypher\Auth\SimpleTokenCredential;
use BlockCypher\Client\TXClient;
use BlockCypher\Rest\ApiContext;
$apiContext = ApiContext::create('test', 'bcy', 'v1', new SimpleTokenCredential('c0afcccdde5081d6429de37d16166ead'), array('mode' => 'sandbox', 'log.LogEnabled' => true, 'log.FileName' => 'BlockCypher.log', 'log.LogLevel' => 'DEBUG'));
// Create a new instance of TX object
$tx = new TX();
// Tx inputs
$input = new \BlockCypher\Api\TXInput();
$input->addAddress("C5vqMGme4FThKnCY44gx1PLgWr86uxRbDm");
$tx->addInput($input);
// Tx outputs
$output = new \BlockCypher\Api\TXOutput();
$output->addAddress("C4MYFr4EAdqEeUKxTnPUF3d3whWcPMz1Fi");
$tx->addOutput($output);
// Tx amount
$output->setValue(1000);
// Satoshis
// For Sample Purposes Only.
$request = clone $tx;
$txClient = new TXClient($apiContext);
$txSkeleton = $txClient->create($tx);
ResultPrinter::printResult("New TX Endpoint", "TXSkeleton", $txSkeleton->getTx()->getHash(), $request, $txSkeleton);
// # Create TX Sample (without sending it)
//
// This sample code demonstrate how you can create a new transaction, as documented at <a href="http://dev.blockcypher.com/?#multisig-transactions">docs</a>.
// Source address is a multisig address.
//
// API used: POST /v1/btc/main/txs/new
//
require __DIR__ . '/../bootstrap.php';
$tx = new \BlockCypher\Api\TX();
// Source address: <a href="https://live.blockcypher.com/btc-testnet/address/2Mu7dJvawNdhshTkKRXGAVLKdr2VA7Rs1wZ/">2Mu7dJvawNdhshTkKRXGAVLKdr2VA7Rs1wZ</a>
$input = new \BlockCypher\Api\TXInput();
$input->setAddresses(array("033e88a5503dc09243e58d9e7a53831c2b77cac014415ad8c29cabab5d933894c1", "02087f346641256d4ba19cc0473afaa8d3d1b903761b9220a915e1af65a12e613c", "03051fa1586ff8d509125d3e25308b4c66fcf656b377bf60bfdb296a4898d42efd"));
$input->setScriptType("multisig-2-of-3");
// Destination address: <a href="https://live.blockcypher.com/btc-testnet/address/n3D2YXwvpoPg8FhcWpzJiS3SvKKGD8AXZ4/">n3D2YXwvpoPg8FhcWpzJiS3SvKKGD8AXZ4</a>
$output = new \BlockCypher\Api\TXOutput();
$output->addAddress("n3D2YXwvpoPg8FhcWpzJiS3SvKKGD8AXZ4");
$output->setValue(1000);
// Satoshis
$tx->addInput($input);
$tx->addOutput($output);
/// For Sample Purposes Only.
$request = clone $tx;
$txClient = new \BlockCypher\Client\TXClient($apiContexts['BTC.test3']);
/// Create New TX
try {
    $txSkeleton = $txClient->create($tx);
} catch (Exception $ex) {
    ResultPrinter::printError("Created Multisig TX (spend multisig fund)", "TXSkeleton", null, $request, $ex);
    exit(1);
}
ResultPrinter::printResult("Created Multisig TX (spend multisig fund)", "TXSkeleton", $txSkeleton->getTx()->getHash(), $request, $txSkeleton);