'', 'hbol' => $hbol, 'docTypes' => $docTypes ]; $accessTokenUrl = 'http://dms.kln.com/keycloak/realms/dsms-realm/protocol/openid-connect/token'; //prod 账号 $grant_type = 'client_credentials'; $client_id = 'online'; $client_secret = 'ouuRZ8QbIrcqKZi0NBJLt7PUEiRr5uXd'; $accessToken = utils::getAccessToken($accessTokenUrl,$grant_type,$client_id,$client_secret); if ($accessToken === null) { throw new Exception("Failed to obtain access token"); } $New_Dms_Url = 'http://dms.kln.com/edoc/documents/fileList'; $result = utils::CURLClient($New_Dms_Url,$accessToken,$data); } catch (Exception $e) { error_log("Error in downloadFiles: " . $e->getMessage()); } return $result; } public static function getAccessToken($accessTokenUrl,$grant_type,$client_id,$client_secret){ if (self::$cachedToken !== null && time() * 1000 < self::$tokenExpiryTime) { return self::$cachedToken; } $postData = http_build_query([ 'grant_type' => $grant_type, 'client_id' => $client_id, 'client_secret' => $client_secret ]); $headers = [ 'Content-Type: application/x-www-form-urlencoded', 'Accept: application/json' ]; $curlInit = curl_init(); curl_setopt_array($curlInit, [ CURLOPT_URL => $accessTokenUrl, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $postData, CURLOPT_HTTPHEADER => $headers, CURLOPT_RETURNTRANSFER => true, // 关键!返回字符串而不是输出 CURLOPT_SSL_VERIFYPEER => false, // 生产环境建议开启 CURLOPT_TIMEOUT => 30, CURLOPT_HEADER => false, // 不返回 header ]); $response = curl_exec($curlInit); $httpCode = curl_getinfo($curlInit, CURLINFO_HTTP_CODE); // 关闭 cURL curl_close($curlInit); // 解析响应 if ($httpCode === 200) { $jsonResponse = json_decode($response, true); if (isset($jsonResponse['access_token'])) { self::$cachedToken = $jsonResponse['access_token']; //计算过期时间,但是换算成毫秒*1000 $expiresIn = isset($jsonResponse['expires_in']) ? $jsonResponse['expires_in'] * 1000 : 0; self::$tokenExpiryTime = (time() * 1000) + $expiresIn - 300000; return self::$cachedToken; } } self::$cachedToken = null; self::$tokenExpiryTime = 0; return null; } /** * 使用 cURL */ public static function CURLClient($url,$accessToken,$data){ $headers = [ 'Authorization: Bearer ' . $accessToken, 'Content-Type: application/json', 'Accept: ' . 'application/json' ]; //$url = 'http://dsms-alb-internet-facing-449737170.ap-southeast-1.elb.amazonaws.com/edoc/documents/fileList'; //$url = 'http://dms.kln.com/edoc/documents/fileList'; // 将数据编码为 JSON $jsonData = json_encode($data); try { $curlInit = curl_init(); curl_setopt_array($curlInit, [ CURLOPT_URL => $url, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $jsonData, CURLOPT_HTTPHEADER => $headers, CURLOPT_RETURNTRANSFER => true, // 关键!返回字符串而不是输出 CURLOPT_SSL_VERIFYPEER => false, // 生产环境建议开启 CURLOPT_TIMEOUT => 30, CURLOPT_HEADER => false, // 不返回 header ]); $response = curl_exec($curlInit); // 检查是否有错误 if ($response === false) { $error = curl_error($curlInit); error_log("cURL 请求失败: " . $error); throw new Exception("cURL 请求失败: " . $error); } // 获取 HTTP 状态码 $httpCode = curl_getinfo($curlInit, CURLINFO_HTTP_CODE); // 关闭 cURL curl_close($curlInit); // 解析响应 $result = json_decode($response, true); if ($httpCode == 200 && isset($result['data'])) { return json_encode($result['data'], JSON_UNESCAPED_UNICODE); }else{ return null; } } catch (Exception $e) { error_log($e->getMessage()); } } } ?>