| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- if (!defined('IN_ONLINE')) {
- exit('Access Denied');
- }
- /**
- * Description of dms_edoc
- *
- * @author Administrator
- */
- class dms_edoc {
- /**
- * 获取文件
- */
- public static function getEdocFile($hbol,$docTypes){
- try {
- $result = null;
- $data = [
- 'fromStation' => '',
- '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 = dms_edoc::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 = dms_edoc::CURLClient($New_Dms_Url,$accessToken,$data);
- } catch (Exception $e) {
- error_log("Error getEdocFile: " . $e->getMessage());
- }
- return $result;
-
- }
- public static function getAccessToken($accessTokenUrl,$grant_type,$client_id,$client_secret){
- $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'])) {
- return $jsonResponse['access_token'];
- }
- }
- 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);
- }
- } catch (Exception $e) {
- error_log($e->getMessage());
- }
- return null;
- }
- }
- ?>
|