wxcvbn wxcvbn - 3 months ago 12
PHP Question

How to integrate Tierion Hash API?

I want to integrate Tierion Hash API with PHP, but I'm beginner and I don't kwon how to start...

In documentation, there is thisto begin:
enter image description here

But I don't understand, how can I do the POST request? where can I write this ?

{
"username":"xxxxx","password":"xxxx"}


}

Answer

Tom from Tierion here. With the HashAPI, you need to first submit your account credentials to get a temporary access token. That access token is what you will submit with your POST request of your hash to the HashAPI, authenticating the the submission. I'll go over a few main requests with the Hash API and CURL-less PHP implementations:


1) Getting your access token & refresh token via the /token/ endpoint, by submitting your credentials as parameters.

First, you must submit your username and password as request parameters to our /token/ endpoint to get your access token. Your credentials are sent as parameters, not as request headers.

Code:

// Specify your request URL and the parameters you need to send.
$url = 'https://hashapi.tierion.com/v1/auth/token';
$data = array('username' => '_YOUR_USERNAME_', 'password' => '_YOUR_PASSWORD_');

// Use the "HTTP" key even if you're making an HTTPS request.
$options = array(
    'http' => array(
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);

// Create and submit the HTTP request.
$context  = stream_context_create($options);
$response = file_get_contents($url, false, $context);

    // Check for a failed request, handle error according. 
    if ($response === FALSE) { 
        // Handle error
    }

// $response now holds your authorization token, expiration time, and refresh token. 
var_dump($response);

Sample Response:

{
  "access_token": "eyB9eXAiOiJKV1QiLDJhbGciOiJIUzI1NiJ8.eyJpZCI6IjU2ZyyiYzFhNWY5Yjg1MjMyZmRjYWRhNyIsInJsbiI6MjBwMCwicmxpIjoicyIsImlzQWRtaW4iOnRydWUtImlhdCI6MTQ2MTI0NzE2NSwiZXhwIjoxNDYxMjUwNzY1LCJqdGkiOiI1MDUyYmFlZDhkNTM5NjcyNDNiMjkzN2RjNjRjNTcyOTJmNTQwZDZhIn0.KNiG-QHdeaH1jVLJpx0ykov8Kk7ogts69k5OhDkgFVM",
  "expires_in": 3600,
  "refresh_token": "ec71236f77ebd665210912ae8891aa08ee8ec3e4"
}

2) Getting an updated access token via the /refresh/ endpoint, by submitting your refresh token as a parameters.

Your access token is valid for one hour, before it will need to be refreshed. You need to submit your refresh token you received with your authorization token to our /refresh/ endpoint. You'll get a new authorization token which will be valid for another hour Your access token is sent as a parameters, not a request header.

Code:

// Specify your request URL and the parameters you need to send.
$url = 'https://hashapi.tierion.com/v1/auth/refresh';
$data = array('refreshToken' => '_YOUR_VALID_REFRESH_TOKEN_');

// Use the "HTTP" key even if you're making an HTTPS request.
$options = array(
    'http' => array(
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);

// Create and submit the HTTP request.
$context  = stream_context_create($options);
$response = file_get_contents($url, false, $context);

    // Check for a failed request, handle error according. 
    if ($response === FALSE) { 
        // Handle error
    }

// $response now holds your NEW authorization token, expiration time, and refresh token. 
var_dump($response);

Sample Response:

{
  "access_token": "eyoJAxeiOiJKV1QiLCJhbGciOiIUJzI1NiJ9.eyJpZCI6IjU2ZWRiYzFhNWY5Yjg1jMjyZmRjYWRhNyIsInJsbiI6MjAwMCwicmxpIjoicyIsImlzQWRtaW4iOnRydWUsImlhdCI6MTQ2MTI0Nzk5NCwiZXhwIjoxNDYxMjUxNTk0LJCqdGkiOiIyM2M5NjVjMTYwNzM3NWZlMzQ0MWFiNDFjZTZjM2JkODkzODYxNWRiIn0.qFKIpT5q4K0u1P8_jwUsQkxxcCGu3uGsQKi33c-1gEM",
  "expires_in": 3600,                                 
  "refresh_token": "ec32176f77ebd556210912ae8891aa08ff8ec3e4"
}

3) Submitting your hash to the Hash API with your authorization token as a header in the request.

Now that you have your access token, you can submit hashes to the Hash API. You need to make a request with your hash as a request parameter, and your authorization token as a request header.

Code:

// Specify your request URL and the parameters you need to send.
$url = 'https://hashapi.tierion.com/v1/hashitems';
$data = array('hash' => '_YOUR_SHA256_HASH_');

// Use the "HTTP" key even if you're making an HTTPS request.
$options = array(
    'http' => array(
        'header'  => "Authorization: Bearer _YOUR_VALID_ACCESS_TOKEN_",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);

// Create and submit the HTTP request.
$context  = stream_context_create($options);
$response = file_get_contents($url, false, $context);

    // Check for a failed request, handle error according. 
    if ($response === FALSE) { 
        // Handle error
    }

// $response now holds your blockchain receipt ID and timestamp.
var_dump($response);

Sample Response:

{
  "receiptId": "571694dd6b5c7b711861ea67",
  "timestamp": 1461097693
}

4) Receive the corresponding blockchain receipt with a receiptId.

After you send a hash, you'll receive a receiptId. This Id is a unique pointer within the Tierion system to your blockchain receipt. This Id does not correlate to the blockchain itself, the receipt does. To obtain the receipt, you send the receiptId to our /receipts/ endpoint.

You append the receiptId to the end of the request URL, rather than sending it through a request parameter.

Code:

// Specify your request URL and the parameters you need to send.
$url = 'https://hashapi.tierion.com/v1/receipts' + _YOUR_RECEIPT_ID_;

// Use the "HTTP" key even if you're making an HTTPS request.
$options = array(
    'http' => array(
        'header'  => "Authorization: Bearer _YOUR_VALID_ACCESS_TOKEN_",
        'method'  => 'POST',
    )
);

// Create and submit the HTTP request.
$context  = stream_context_create($options);
$response = file_get_contents($url, false, $context);

    // Check for a failed request, handle error according. 
    if ($response === FALSE) { 
        // Handle error
    }

// $response now holds your blockchain receipt. 
var_dump($response);

Sample Response:

  {
  "receipt": "{\"@context\":\"https://w3id.org/chainpoint/v2\",\"type\":\"ChainpointSHA256v2\",\"targetHash\":\"a83a2c5c11a2bc814d0b1dca0a385d71a1f4d662b4e31335ba7562c56cce15b1\",\"merkleRoot\":\"2d21167d2f2f73e309d5ac00ab9faaf8b530478c5b64dcd5755511c8a3eccfa3\",\"proof\":[{\"left\":\"7c6e3b0159f1359d0f9f5a3b923011b7466bdf1423317ca09121b5dc61ad1836\"},{\"right\":\"541c5ae04e83c2880296818978511893ba1b00f1515162cd865f25da54f636d0\"},{\"right\":\"67b7ced55a4db4bb0fbaf2036901888a08ab7d8126556431258017652cf62092\"}],\"anchors\":[{\"type\":\"BTCOpReturn\",\"sourceId\":\"33884d525ca1cc54313fa2f27be4cf3442b35314866851cc7fc5ec3973d80250\"}]}"
}

If you have any further questions, feel free to reach out to our team at team@tierion.com or check out our Hash API documentation. The answer here provides more information about PHP HTTP requests.

Comments