at_demo.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. class AtDemo {
  19. const CLIENT_SECRET = "715316bfecf0a6ec87fa65d670e67f31"; // 言灵 // your app secret
  20. const CLIENT_ID = "100218075"; // 言灵 // your appId
  21. const TOKEN_URL = "https://oauth-login.cloud.huawei.com/oauth2/v3/token"; // token url to get the authorization
  22. private static $accessToken = null;
  23. /**
  24. * Gets App Level AccessToken.
  25. *
  26. * @return string the App Level AccessToken
  27. */
  28. public static function getAppAT() {
  29. if (self::$accessToken != null && self::$accessToken != '') {
  30. return self::$accessToken;
  31. }
  32. $grant_type = "client_credentials";
  33. $dataArray = array("grant_type" => $grant_type, "client_id" => AtDemo::CLIENT_ID, "client_secret" => AtDemo::CLIENT_SECRET);
  34. $data = http_build_query($dataArray, '', '&');
  35. $header = array("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
  36. try {
  37. $ret = self::doPost(AtDemo::TOKEN_URL, $data, 5, 5, $header);
  38. $result = $ret[0];
  39. $status_code = $ret[1];
  40. if ($status_code != '200') {
  41. echo "get token error! the oauth server response=", $result;
  42. return null;
  43. }
  44. $array = json_decode($result, true);
  45. self::$accessToken = $array["access_token"];
  46. } catch (Exception $e) {
  47. echo $e->getMessage();
  48. }
  49. // TODO: display the accessToken, you can remove it
  50. // echo self::$accessToken;
  51. return self::$accessToken;
  52. }
  53. /**
  54. * Http post function. when the AppAT is out of time, get AppAT and try again.
  55. *
  56. * @param $httpUrl string the http url
  57. * @param $data string the data
  58. * @param $connectTimeout int the connect timeout
  59. * @param $readTimeout int the read timeout
  60. * @param $headers array the headers
  61. * @return string the result
  62. */
  63. public static function httpPost($httpUrl, $data, $connectTimeout, $readTimeout, $headers) {
  64. $ret = self::doPost($httpUrl, $data, $connectTimeout, $readTimeout, $headers);
  65. $result = $ret[0];
  66. $status_code = $ret[1];
  67. //when statusCode is 401, means AT is expired
  68. if ($status_code == '401') {
  69. //refresh Access Token
  70. self::$accessToken = '';
  71. $appAt = self::getAppAT();
  72. $headers = self::buildAuthorization($appAt);
  73. $ret = self::doPost($httpUrl, $data, $connectTimeout, $readTimeout, $headers);
  74. $result = $ret[0];
  75. }
  76. return $result;
  77. }
  78. /**
  79. * Http post function.
  80. *
  81. * @param $httpUrl string the http url
  82. * @param $data string the data
  83. * @param $connectTimeout int the connect timeout
  84. * @param $readTimeout int the read timeout
  85. * @param $headers array the headers
  86. * @return array the result and status code
  87. */
  88. private static function doPost($httpUrl, $data, $connectTimeout, $readTimeout, $headers) {
  89. $options = array(
  90. CURLOPT_POST => 1,
  91. CURLOPT_URL => $httpUrl,
  92. CURLOPT_HTTPHEADER => $headers,
  93. CURLOPT_RETURNTRANSFER => 1,
  94. CURLOPT_CONNECTTIMEOUT => $connectTimeout,
  95. CURLOPT_TIMEOUT => $readTimeout,
  96. CURLOPT_POSTFIELDS => $data,
  97. CURLOPT_SSL_VERIFYPEER => 0 //disable HTTPS protocol to verify SSL security certificate
  98. );
  99. $ch = curl_init();
  100. curl_setopt_array($ch, $options);
  101. $result = curl_exec($ch);
  102. $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  103. if (curl_error($ch)) {
  104. var_dump($ch);
  105. $result = false;
  106. }
  107. curl_close($ch);
  108. return [$result, $status_code];
  109. }
  110. /**
  111. * build IAP Authorization
  112. *
  113. * @param $appAT string the AppAT
  114. * @return array the header
  115. */
  116. public static function buildAuthorization($appAT) {
  117. $oriString = "APPAT:" . $appAT;
  118. $authHead = "Basic " . base64_encode(utf8_encode($oriString)) . "";
  119. $headers = ["Authorization:" . $authHead, "Content-Type: application/json; charset=UTF-8"];
  120. return $headers;
  121. }
  122. }