dms_edoc.class.php 4.5 KB

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