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

Making First Call

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

Once you have completed the Installation, we could make the first call. To write an app that uses the SDK, we will create our first PHP script that will create a WebHook.

Instructions

First, Create file first.php in our project root location. You could alternatively copy the completed file here: first.php

  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';
  2. Provide the Token. Optionally, Replace the given one with your own Token

    // After Step 1
    $token = 'c0afcccdde5081d6429de37d16166ead';
    $apiContext = new \BlockCypher\Rest\ApiContext(new \BlockCypher\Auth\SimpleTokenCredential($token));
  3. Lets try to create a webhook using WebHook API mentioned here

    // 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) {
            // This will print the detailed information on the exception. 
            //REALLY HELPFUL FOR DEBUGGING
            echo $ex->getData();
    }
  5. Run php -f first.php from command line. This will successfully create a webhook. The output would look similar to this:

    > php -f first.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"
    }
NOTE
  • To reduce redundant code and minimize security risks, you could create a file bootstrap.php and move step 1 and 2 there, and just include bootstrap.php.
TROUBLESHOOTING
Fatal error: require(): Failed opening required 'D:\project-composer/php-client/autoload.php' (include_path='.;C:\php\pear') in D:\project\first.php on line 4

Remember, if your downloaded our SDK using composer, replace php-client with vendor in this line:

//require __DIR__  . '/php-client/autoload.php'; // Direct Download
require __DIR__  . '/vendor/autoload.php'; // Using Composer

Next Step