如何整合Tierion Hash API?

How to integrate Tierion Hash API?

我想将 Tierion Hash API 与 PHP 集成,但我是初学者,不知道如何开始...

在文档中,开头是这样的:

但是我不明白,POST请求怎么办?我在哪里可以写这个? { "username":"xxxxx","password":"xxxx"}

}

Tom 来自 Tierion 这里。使用 HashAPI,您需要先提交您的帐户凭据以获取临时访问令牌。该访问令牌是您将与您的哈希 POST 请求一起提交给哈希 API 的内容,以验证提交。我将通过 Hash API 和 CURL-less PHP 实现来讨论几个主要请求:


1) 通过 /token/ 端点获取访问令牌和刷新令牌,方法是将您的凭据作为参数提交。

首先,您必须将您的用户名和密码作为请求参数提交到我们的 /token/ 端点以获取您的访问令牌。您的凭据作为参数发送,而不是作为请求 headers。

代码:

// 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);

响应示例:

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

2) 通过 /refresh/ 端点获取更新的访问令牌,方法是提交您的刷新令牌作为参数。

您的访问令牌有效期为一小时,之后需要刷新。您需要将随授权令牌一起收到的刷新令牌提交到我们的 /refresh/ 端点。您将获得一个新的授权令牌,该令牌将在另一个小时内有效 您的访问令牌作为参数发送,而不是请求 header。

代码:

// 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);

响应示例:

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

3) 将您的哈希提交到哈希 API,并将您的授权令牌作为请求中的 header。

现在您有了访问令牌,您可以将哈希提交到哈希 API。您需要使用您的哈希作为请求参数发出请求,并将您的授权令牌作为请求 header。

代码:

// 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);

响应示例:

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

4) 通过receiptId接收对应的区块链收据

发送哈希后,您将收到一个 receiptId。此 ID 是 Tierion 系统中指向您的区块链收据的唯一指针。此 Id 与区块链本身无关,收据相关。要获取收据,请将 receiptId 发送到我们的 /receipts/ 端点。

您将 receiptId 附加到请求的末尾 URL,而不是通过请求参数发送它。

代码:

// 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);

响应示例:

  {
  "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\"}]}"
}

如果您有任何其他问题,请随时通过 team@tierion.com 与我们的团队联系,或查看我们的 Hash API documentation. The answer here 提供的更多信息关于 PHP 个 HTTP 请求。