MuseumServer.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Servers\Wenlv;
  3. use \Exception;
  4. /**
  5. * 获取博物馆信息API接口
  6. * @author 唐远望
  7. * @version 1.0
  8. * @date 2026-01-06
  9. */
  10. class MuseumServer
  11. {
  12. private $header;
  13. private $baseUrl = 'http://nb.ncha.gov.cn';
  14. /**
  15. * 构造函数
  16. * @throws Exception 当API密钥未设置时抛出异常
  17. *
  18. */
  19. public function __construct()
  20. {
  21. $this->header = [
  22. 'content-type:application/json;charset=UTF-8',
  23. 'accept:*/*',
  24. 'accept-encoding:gzip, deflate',
  25. 'accept-language:zh-CN,zh;q=0.9,en;q=0.8',
  26. 'cookie:Hm_lvt_352975963075889e3e294c05c9b5b506=1775198593; HMACCOUNT=958D4906DE24C8E1; Hm_lpvt_352975963075889e3e294c05c9b5b506=1775203189; JSESSIONID=F3391B6142667B4DEE48F9FD47B90056',
  27. 'host:nb.ncha.gov.cn',
  28. 'proxy-connection:keep-alive',
  29. 'referer:http://nb.ncha.gov.cn/museum.html?page=1&nianfen=2024&level=%E4%BA%8C%E7%BA%A7&showModel=list&order=default',
  30. 'user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36',
  31. 'x-requested-with:XMLHttpRequest',
  32. ];
  33. }
  34. /**
  35. * 尝试将接口返回内容转成 UTF-8,避免 GBK/GB18030 导致的乱码
  36. * @param string $str
  37. * @return string
  38. */
  39. private function ensureUtf8($str)
  40. {
  41. if (!is_string($str) || $str === '') {
  42. return $str;
  43. }
  44. // 强制检测:优先判断是否已经是 UTF-8
  45. $enc = mb_detect_encoding($str, ['UTF-8', 'GB18030', 'GBK', 'GB2312'], true);
  46. if ($enc && strtoupper($enc) !== 'UTF-8') {
  47. return mb_convert_encoding($str, 'UTF-8', $enc);
  48. }
  49. // 如果明显出现替换字符(常见于编码不匹配),尝试兜底从 GB18030 转码
  50. if (strpos($str, '�') !== false) {
  51. $converted = @mb_convert_encoding($str, 'UTF-8', 'GB18030');
  52. if (is_string($converted) && $converted !== '') {
  53. return $converted;
  54. }
  55. }
  56. return $str;
  57. }
  58. /**
  59. * 博物馆分页数据
  60. * @author 唐远望
  61. * @version 1.0
  62. * @date 2026-01-08
  63. */
  64. public function getMuseumPageData()
  65. {
  66. $url = $this->baseUrl . '/je/api/loadDataNBLNList?current=1&size=15&NB_ND=2024';
  67. $response = self::curl_get($url, $this->header);
  68. $response_data = json_decode($response, true);
  69. if (isset($response_data['rows']) && !empty($response_data['rows'])) {
  70. $page_data = [
  71. 'totle' => $response_data['rows']['total'],
  72. 'current' => $response_data['rows']['current'],
  73. 'pages' => $response_data['rows']['pages'],
  74. 'size' => $response_data['rows']['size'],
  75. ];
  76. } else {
  77. $page_data = [
  78. 'totle' => 0,
  79. 'current' => 0,
  80. 'pages' => 0,
  81. 'size' => 0,
  82. ];
  83. }
  84. return $page_data;
  85. }
  86. /**
  87. * 博物馆数据信息列表
  88. * @author 唐远望
  89. * @version 1.0
  90. * @date 2026-01-08
  91. */
  92. public function getMuseumList($page)
  93. {
  94. $url = $this->baseUrl . '/je/api/loadDataNBLNList?current='.$page.'&size=100&NB_ND=2024&NB_BWGXZ_NAME=&NB_ZLDJ_NAME=&NB_MFKF=&NB_S_NAME=&NB_SS_NAME=&NB_BWGMC=';
  95. $response = self::curl_get($url, $this->header);
  96. $response_data = json_decode($response, true);
  97. return $response_data;
  98. }
  99. /**
  100. * 获取博物馆详情
  101. * @author 唐远望
  102. * @version 1.0
  103. * @date 2026-01-08
  104. */
  105. public function getMuseumDetail($BWG_NB_ID)
  106. {
  107. $url = $this->baseUrl . '/je/api/loadDataNBLN?parameter={"current":1,"size":-1,"BWG_NB_ID":"' . $BWG_NB_ID . '"}';
  108. $response = self::curl_get($url, $this->header);
  109. $response_data = json_decode($response, true);
  110. return $response_data;
  111. }
  112. /**
  113. * 发送http post请求
  114. * @param string $url 请求地址
  115. * @param array $headers 请求头
  116. * @param json $postdata 请求数据
  117. * @param array $options curl配置项
  118. */
  119. public static function curl_post($url = '', $headers = '', $postdata = '', $options = array())
  120. {
  121. $ch = curl_init($url);
  122. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  123. curl_setopt($ch, CURLOPT_POST, 1);
  124. curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  125. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  126. curl_setopt($ch, CURLOPT_ENCODING, ''); // 添加这一行,让 cURL 自动处理压缩
  127. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  128. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  129. if (!empty($headers)) {
  130. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  131. }
  132. if (!empty($options)) {
  133. curl_setopt_array($ch, $options);
  134. }
  135. $data = curl_exec($ch);
  136. return $data;
  137. }
  138. /**
  139. * 发送http get请求
  140. */
  141. public static function curl_get($url = '', $headers = '', $options = array())
  142. {
  143. $ch = curl_init($url);
  144. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  145. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  146. curl_setopt($ch, CURLOPT_ENCODING, ''); // 添加这一行,让 cURL 自动处理压缩
  147. if (!empty($headers)) {
  148. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  149. }
  150. if (!empty($options)) {
  151. curl_setopt_array($ch, $options);
  152. }
  153. $data = curl_exec($ch);
  154. return $data;
  155. }
  156. }