Skip to content
This repository has been archived by the owner on Oct 24, 2020. It is now read-only.

File based Configurations

Owen Voke edited this page Nov 12, 2017 · 8 revisions

Now as we are able to make first call, add configuration, and found different ways to add data to object, this page explains if we could add configurations differently.

In BlockCypher SDK, you can also create a .ini configuration file, and use it to pass configurations to our SDK.

Instructions

First, create a file called third.php in your project root directory. You could alternatively copy the completed file here: third.php and sdk_config.ini

  1. Autoload the SDK Package. This will include all the files and classes to your autoloader. Please note, If your downloaded our SDK using composer, replace php-client with vendor. This applies for all sample code in our SDK.

    // 1. Autoload the SDK Package. This will include all the files and classes to your autoloader
    //require __DIR__  . '/php-client/autoload.php'; // Direct Download
    require __DIR__  . '/vendor/autoload.php'; // Using Composer
  2. Now, instead of creating an instance of $apiContext we did while creating first call, we will define a constant BC_CONFIG_PATH pointing to the directory where we will create sdk_config.ini. Replace DIR with another location as desired.

    // 2. Define BC_CONFIG_PATH directory
    if(!defined("BC_CONFIG_PATH")) {
        define("BC_CONFIG_PATH", __DIR__);
    }
  3. We will repeat step 3 and 4 as before to create a webhook object and make create call.

    // After Step 2
    $webHook = new \BlockCypher\Api\WebHook();
    $webHook->setUrl("https://requestb.in/slmm49sl?uniqid=" . uniqid());
    $webHook->setEvent('unconfirmed-tx');
  4. Make a Create Call and Print the WebHook

    // After Step 3
    try {
        $webHook->create($apiContext);
        echo $webHook;
    }
    catch (\BlockCypher\Exception\BlockCypherConnectionException $ex) {
        echo $ex;
    }
  5. Just before executing the script, we need to create sdk_config.ini. Please download the file in your project root directory.

  6. Run php -f third.php from command line. This will successfully create a webhook. The output would look similar to this:

    > php -f third.php
    {
        "url": "https://requestb.in/slmm49sl?uniqid=554790ee32b4d",
        "event": "unconfirmed-tx",
        "id": "34784394-bbaa-4303-bd3c-b5773e7d6ca8",
        "token": "c0afcccdde5081d6429de37d16166ead",
        "callback_errors": 0,
        "filter": "event=unconfirmed-tx"
    }

Basically, we substituted apiContext object, with sdk_config.ini. This allows you to provide your configurations by file based .ini file. The most important benefit to this is it eliminates the need to crete an apiContext object, and pass it on for each call. You can also use ApiContext::setDefault(...) if you do not want to use the .ini file.

TROUBLESHOOTING
PHP Fatal error:  Uncaught exception 'BlockCypher\Exception\BlockCypherInvalidCr
edentialException' with message 'Credential not found for  default user. Please
make sure your configuration/APIContext has credential information'

Make sure your sdk_conif.ini file contains these lines:

[Account]
acct1.AccessToken = c0afcccdde5081d6429de37d16166ead

AccessToken is case sensitive.

Next Step