dms_edoc.class.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. if (!defined('IN_ONLINE')) {
  3. exit('Access Denied');
  4. }
  5. /**
  6. * Description of utilsclass
  7. *
  8. * @author Administrator
  9. */
  10. class utils {
  11. private static $cachedToken = null;
  12. private static $tokenExpiryTime = 0;
  13. /**
  14. * 获取文件
  15. */
  16. public static function getEdocFile($hbol,$docTypes){
  17. try {
  18. $result = null;
  19. $data = [
  20. 'fromStation' => '',
  21. 'hbol' => $hbol,
  22. 'docTypes' => $docTypes
  23. ];
  24. $accessTokenUrl = 'http://dms.kln.com/keycloak/realms/dsms-realm/protocol/openid-connect/token';
  25. //prod 账号
  26. $grant_type = 'client_credentials';
  27. $client_id = 'online';
  28. $client_secret = 'ouuRZ8QbIrcqKZi0NBJLt7PUEiRr5uXd';
  29. $accessToken = utils::getAccessToken($accessTokenUrl,$grant_type,$client_id,$client_secret);
  30. if ($accessToken === null) {
  31. throw new Exception("Failed to obtain access token");
  32. }
  33. $New_Dms_Url = 'http://dms.kln.com/edoc/documents/fileList';
  34. $result = utils::CURLClient($New_Dms_Url,$accessToken,$data);
  35. } catch (Exception $e) {
  36. error_log("Error in downloadFiles: " . $e->getMessage());
  37. }
  38. return $result;
  39. }
  40. public static function getAccessToken($accessTokenUrl,$grant_type,$client_id,$client_secret){
  41. if (self::$cachedToken !== null && time() * 1000 < self::$tokenExpiryTime) {
  42. return self::$cachedToken;
  43. }
  44. $postData = http_build_query([
  45. 'grant_type' => $grant_type,
  46. 'client_id' => $client_id,
  47. 'client_secret' => $client_secret
  48. ]);
  49. $headers = [
  50. 'Content-Type: application/x-www-form-urlencoded',
  51. 'Accept: application/json'
  52. ];
  53. $curlInit = curl_init();
  54. curl_setopt_array($curlInit, [
  55. CURLOPT_URL => $accessTokenUrl,
  56. CURLOPT_POST => true,
  57. CURLOPT_POSTFIELDS => $postData,
  58. CURLOPT_HTTPHEADER => $headers,
  59. CURLOPT_RETURNTRANSFER => true, // 关键!返回字符串而不是输出
  60. CURLOPT_SSL_VERIFYPEER => false, // 生产环境建议开启
  61. CURLOPT_TIMEOUT => 30,
  62. CURLOPT_HEADER => false, // 不返回 header
  63. ]);
  64. $response = curl_exec($curlInit);
  65. $httpCode = curl_getinfo($curlInit, CURLINFO_HTTP_CODE);
  66. // 关闭 cURL
  67. curl_close($curlInit);
  68. // 解析响应
  69. if ($httpCode === 200) {
  70. $jsonResponse = json_decode($response, true);
  71. if (isset($jsonResponse['access_token'])) {
  72. self::$cachedToken = $jsonResponse['access_token'];
  73. //计算过期时间,但是换算成毫秒*1000
  74. $expiresIn = isset($jsonResponse['expires_in']) ? $jsonResponse['expires_in'] * 1000 : 0;
  75. self::$tokenExpiryTime = (time() * 1000) + $expiresIn - 300000;
  76. return self::$cachedToken;
  77. }
  78. }
  79. self::$cachedToken = null;
  80. self::$tokenExpiryTime = 0;
  81. return null;
  82. }
  83. /**
  84. * 使用 cURL
  85. */
  86. public static function CURLClient($url,$accessToken,$data){
  87. $headers = [
  88. 'Authorization: Bearer ' . $accessToken,
  89. 'Content-Type: application/json',
  90. 'Accept: ' . 'application/json'
  91. ];
  92. //$url = 'http://dsms-alb-internet-facing-449737170.ap-southeast-1.elb.amazonaws.com/edoc/documents/fileList';
  93. //$url = 'http://dms.kln.com/edoc/documents/fileList';
  94. // 将数据编码为 JSON
  95. $jsonData = json_encode($data);
  96. try {
  97. $curlInit = curl_init();
  98. curl_setopt_array($curlInit, [
  99. CURLOPT_URL => $url,
  100. CURLOPT_POST => true,
  101. CURLOPT_POSTFIELDS => $jsonData,
  102. CURLOPT_HTTPHEADER => $headers,
  103. CURLOPT_RETURNTRANSFER => true, // 关键!返回字符串而不是输出
  104. CURLOPT_SSL_VERIFYPEER => false, // 生产环境建议开启
  105. CURLOPT_TIMEOUT => 30,
  106. CURLOPT_HEADER => false, // 不返回 header
  107. ]);
  108. $response = curl_exec($curlInit);
  109. // 检查是否有错误
  110. if ($response === false) {
  111. $error = curl_error($curlInit);
  112. error_log("cURL 请求失败: " . $error);
  113. throw new Exception("cURL 请求失败: " . $error);
  114. }
  115. // 获取 HTTP 状态码
  116. $httpCode = curl_getinfo($curlInit, CURLINFO_HTTP_CODE);
  117. // 关闭 cURL
  118. curl_close($curlInit);
  119. // 解析响应
  120. $result = json_decode($response, true);
  121. if ($httpCode == 200 && isset($result['data'])) {
  122. return json_encode($result['data'], JSON_UNESCAPED_UNICODE);
  123. }else{
  124. return null;
  125. }
  126. } catch (Exception $e) {
  127. error_log($e->getMessage());
  128. }
  129. }
  130. }
  131. ?>