-
Notifications
You must be signed in to change notification settings - Fork 85
Making First Call
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.
First, Create file first.php
in our project root location. You could alternatively copy the completed file here: first.php
-
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
withvendor
. 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 | https://accounts.blockcypher.com/dashboard]]
```php
// After Step 1
$token = 'c0afcccdde5081d6429de37d16166ead';
$apiContext = new \BlockCypher\Rest\ApiContext(new \BlockCypher\Auth\SimpleTokenCredential($token));
- 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
```php
// 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:
````sh
> 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"
}
- To reduce redundant code and minimize security risks, you could create a file
bootstrap.php
and move step 1 and 2 there, and justinclude
bootstrap.php.
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 Donwload
require __DIR__ . '/vendor/autoload.php'; // Using Composer
Getting Started
Using Our SDK
Configurations
Extras
External Links