gpt4 book ai didi

php - 如何集成Tierion Hash API?

转载 作者:行者123 更新时间:2023-12-02 14:30:40 25 4
gpt4 key购买 nike

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

在文档中,有这样的开头: enter image description here

但是我不明白,我该如何进行POST请求?我可以在哪里写这个? {
“用户名”:“xxxxx”,“密码”:“xxxx”}

}

最佳答案

汤姆来自Tierion这里。使用 HashAPI,您需要首先提交您的帐户凭据以获取临时访问 token 。该访问 token 是您将通过哈希的 POST 请求提交给 HashAPI 的内容,从而对提交进行身份验证。我将使用 Hash API 和无 CURL PHP 实现来介绍一些主要请求:


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

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

代码:

// 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/端点获取更新的访问 token ,将刷新 token 作为参数提交。

您的访问 token 的有效期为一小时,之后需要刷新。您需要将收到的刷新 token 和授权 token 一起提交到我们的/refresh/端点。您将获得一个新的授权 token ,该 token 将在另外一个小时内有效。您的访问 token 将作为参数而不是请求 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,并使用您的授权 token 作为请求中的 header 。

现在您已经有了访问 token ,您可以将哈希值提交到哈希 API。您需要使用您的哈希值作为请求参数,并将您的授权 token 作为请求 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的对应区 block 链收据。

发送哈希值后,您将收到一个receiptId。此 ID 是 Tierion 系统中指向您的区 block 链收据的唯一指针。该 ID 与区 block 链本身无关,但与收据相关。要获取收据,您可以将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\"}]}"
}

如果您有任何其他问题,请随时联系我们的团队: [email protected] 或查看我们的 Hash API documentation 。答案here提供有关 PHP HTTP 请求的更多信息。

关于php - 如何集成Tierion Hash API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39036811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com