如何集成Tierion Hash API?

时间:2016-08-19 10:32:32

标签: php integration

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

在文档中,有这个开始: enter image description here

但我不明白,我该如何处理POST请求?我在哪里可以写这个?  { "username":"xxxxx","password":"xxxx"}

}

1 个答案:

答案 0 :(得分:5)

汤姆来自Tierion。使用HashAPI,您需要先提交帐户凭据以获取临时访问令牌。您将使用您的哈希的POST请求向HashAPI提交该访问令牌,对提交进行身份验证。我将使用Hash API和无CURL的PHP​​实现来回顾一些主要请求:

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

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

代码:

// 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 / endpoint获取更新的访问令牌。

您的访问令牌有效期为一小时,然后才需要刷新。您需要将使用授权令牌收到的刷新令牌提交到/ refresh / endpoint。您将获得一个新的授权令牌,该令牌将在另一个小时内有效您的访问令牌将作为参数发送,而不是请求头。

代码:

// 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)使用您的授权令牌作为请求中的标头,将您的哈希值提交给Hash API。

现在您已拥有访问令牌,您可以向哈希API提交哈希值。您需要使用哈希作为请求参数发出请求,并将您的授权令牌作为请求标头。

代码:

// 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 / endpoint。

将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。答案here提供了有关PHP HTTP请求的更多信息。